identifiers.go 5.31 KB
Newer Older
1
2
package irmago

3
import "strings"
4

Sietse Ringers's avatar
Sietse Ringers committed
5
type metaObjectIdentifier string
6
7
8

// SchemeManagerIdentifier identifies a scheme manager. Equal to its ID. For example "irma-demo".
type SchemeManagerIdentifier struct {
Sietse Ringers's avatar
Sietse Ringers committed
9
	metaObjectIdentifier
10
11
}

Sietse Ringers's avatar
Sietse Ringers committed
12
// IssuerIdentifier identifies an issuer. For example "irma-demo.RU".
13
type IssuerIdentifier struct {
Sietse Ringers's avatar
Sietse Ringers committed
14
	metaObjectIdentifier
15
16
}

17
18
// CredentialTypeIdentifier identifies a credentialtype. For example "irma-demo.RU.studentCard".
type CredentialTypeIdentifier struct {
Sietse Ringers's avatar
Sietse Ringers committed
19
	metaObjectIdentifier
20
21
}

22
23
// AttributeTypeIdentifier identifies an attribute. For example "irma-demo.RU.studentCard.studentID".
type AttributeTypeIdentifier struct {
Sietse Ringers's avatar
Sietse Ringers committed
24
	metaObjectIdentifier
25
26
}

Sietse Ringers's avatar
Sietse Ringers committed
27
// CredentialIdentifier identifies a credential instance.
28
29
30
31
32
33
type CredentialIdentifier struct {
	Type  CredentialTypeIdentifier
	Index int
	Count int
}

Sietse Ringers's avatar
Sietse Ringers committed
34
// AttributeIdentifier identifies an attribute instance.
35
36
37
38
39
40
type AttributeIdentifier struct {
	Type  AttributeTypeIdentifier
	Index int
	Count int
}

41
42
43
44
45
46
47
48
49
// IrmaIdentifierSet contains a set (ensured by using map[...]struct{}) of all scheme managers,
// all issuers, all credential types and all public keys that are involved in an IRMA session.
type IrmaIdentifierSet struct {
	SchemeManagers  map[SchemeManagerIdentifier]struct{}
	Issuers         map[IssuerIdentifier]struct{}
	CredentialTypes map[CredentialTypeIdentifier]struct{}
	PublicKeys      map[IssuerIdentifier][]int
}

Sietse Ringers's avatar
Sietse Ringers committed
50
// Parent returns the parent object of this identifier.
Sietse Ringers's avatar
Sietse Ringers committed
51
func (oi metaObjectIdentifier) Parent() string {
52
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
53
	return str[:strings.LastIndex(str, ".")]
54
55
}

Sietse Ringers's avatar
Sietse Ringers committed
56
// Name returns the last part of this identifier.
Sietse Ringers's avatar
Sietse Ringers committed
57
func (oi metaObjectIdentifier) Name() string {
58
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
59
	return str[strings.LastIndex(str, ".")+1:]
60
61
}

Sietse Ringers's avatar
Sietse Ringers committed
62
// String returns this identifier as a string.
Sietse Ringers's avatar
Sietse Ringers committed
63
func (oi metaObjectIdentifier) String() string {
64
65
66
67
68
	return string(oi)
}

// NewSchemeManagerIdentifier converts the specified identifier to a SchemeManagerIdentifier.
func NewSchemeManagerIdentifier(id string) SchemeManagerIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
69
	return SchemeManagerIdentifier{metaObjectIdentifier(id)}
70
71
72
73
}

// NewIssuerIdentifier converts the specified identifier to a IssuerIdentifier.
func NewIssuerIdentifier(id string) IssuerIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
74
	return IssuerIdentifier{metaObjectIdentifier(id)}
75
76
}

77
78
// NewCredentialTypeIdentifier converts the specified identifier to a CredentialTypeIdentifier.
func NewCredentialTypeIdentifier(id string) CredentialTypeIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
79
	return CredentialTypeIdentifier{metaObjectIdentifier(id)}
80
81
}

82
83
// NewAttributeTypeIdentifier converts the specified identifier to a AttributeTypeIdentifier.
func NewAttributeTypeIdentifier(id string) AttributeTypeIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
84
	return AttributeTypeIdentifier{metaObjectIdentifier(id)}
85
86
87
88
89
90
91
92
}

// SchemeManagerIdentifier returns the scheme manager identifer of the issuer.
func (id IssuerIdentifier) SchemeManagerIdentifier() SchemeManagerIdentifier {
	return NewSchemeManagerIdentifier(id.Parent())
}

// IssuerIdentifier returns the IssuerIdentifier of the credential identifier.
93
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
94
95
96
	return NewIssuerIdentifier(id.Parent())
}

97
// CredentialTypeIdentifier returns the CredentialTypeIdentifier of the attribute identifier.
98
func (id AttributeTypeIdentifier) CredentialTypeIdentifier() CredentialTypeIdentifier {
99
	return NewCredentialTypeIdentifier(id.Parent())
100
}
101

Sietse Ringers's avatar
Sietse Ringers committed
102
103
// IsCredential returns true if this attribute refers to its containing credential
// (i.e., it consists of only 3 parts).
104
105
106
func (id AttributeTypeIdentifier) IsCredential() bool {
	return strings.Count(id.String(), ".") == 2
}
107

Sietse Ringers's avatar
Sietse Ringers committed
108
// CredentialIdentifier returns the credential identifier of this attribute.
109
110
111
func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
	return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Index: ai.Index, Count: ai.Count}
}
112

113
114
115
// MarshalText implements encoding.TextMarshaler.
func (id SchemeManagerIdentifier) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
116
117
}

118
119
120
121
122
123
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *SchemeManagerIdentifier) UnmarshalText(text []byte) error {
	*id = NewSchemeManagerIdentifier(string(text))
	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
124
125
126
127
128
// MarshalText implements encoding.TextMarshaler.
func (id IssuerIdentifier) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
}

129
130
131
132
133
134
// TODO enable this when updating protocol
//// UnmarshalText implements encoding.TextUnmarshaler.
//func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
//	*id = NewIssuerIdentifier(string(text))
//	return nil
//}
135

136
137
138
139
140
141
142
143
// MarshalText implements encoding.TextMarshaler.
func (id CredentialTypeIdentifier) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (id *CredentialTypeIdentifier) UnmarshalText(text []byte) error {
	*id = NewCredentialTypeIdentifier(string(text))
144
145
	return nil
}
Sietse Ringers's avatar
Sietse Ringers committed
146

147
148
149
150
151
152
153
154
// MarshalText implements encoding.TextMarshaler.
func (id AttributeTypeIdentifier) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
}

// UnmarshalText implements encoding.TextUnmarshaler.
func (id *AttributeTypeIdentifier) UnmarshalText(text []byte) error {
	*id = NewAttributeTypeIdentifier(string(text))
Sietse Ringers's avatar
Sietse Ringers committed
155
156
	return nil
}
157
158
159
160
161
162
163
164
165

func (set *IrmaIdentifierSet) Distributed(store *ConfigurationStore) bool {
	for id := range set.SchemeManagers {
		if store.SchemeManagers[id].Distributed() {
			return true
		}
	}
	return false
}