identifiers.go 5.44 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
type CredentialIdentifier struct {
29
30
	Type CredentialTypeIdentifier
	Hash string
31
32
}

Sietse Ringers's avatar
Sietse Ringers committed
33
// AttributeIdentifier identifies an attribute instance.
34
type AttributeIdentifier struct {
35
36
	Type AttributeTypeIdentifier
	Hash string
37
38
}

39
40
41
42
43
44
45
46
47
// 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
48
// Parent returns the parent object of this identifier.
Sietse Ringers's avatar
Sietse Ringers committed
49
func (oi metaObjectIdentifier) Parent() string {
50
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
51
	return str[:strings.LastIndex(str, ".")]
52
53
}

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

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

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

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

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

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

// 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.
91
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
92
93
94
	return NewIssuerIdentifier(id.Parent())
}

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

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

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

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

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

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

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

134
135
136
137
138
139
140
141
// 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))
142
143
	return nil
}
Sietse Ringers's avatar
Sietse Ringers committed
144

145
146
147
148
149
150
151
152
// 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
153
154
	return nil
}
155
156
157
158
159
160
161
162
163

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

func (set *IrmaIdentifierSet) Empty() bool {
	return len(set.SchemeManagers) == 0 && len(set.Issuers) == 0 && len(set.CredentialTypes) == 0 && len(set.PublicKeys) == 0
}