identifiers.go 4.71 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
}

Sietse Ringers's avatar
Sietse Ringers committed
41
// Parent returns the parent object of this identifier.
Sietse Ringers's avatar
Sietse Ringers committed
42
func (oi metaObjectIdentifier) Parent() string {
43
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
44
	return str[:strings.LastIndex(str, ".")]
45
46
}

Sietse Ringers's avatar
Sietse Ringers committed
47
// Name returns the last part of this identifier.
Sietse Ringers's avatar
Sietse Ringers committed
48
func (oi metaObjectIdentifier) Name() string {
49
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
50
	return str[strings.LastIndex(str, ".")+1:]
51
52
}

Sietse Ringers's avatar
Sietse Ringers committed
53
// String returns this identifier as a string.
Sietse Ringers's avatar
Sietse Ringers committed
54
func (oi metaObjectIdentifier) String() string {
55
56
57
58
59
	return string(oi)
}

// NewSchemeManagerIdentifier converts the specified identifier to a SchemeManagerIdentifier.
func NewSchemeManagerIdentifier(id string) SchemeManagerIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
60
	return SchemeManagerIdentifier{metaObjectIdentifier(id)}
61
62
63
64
}

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

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

73
74
// NewAttributeTypeIdentifier converts the specified identifier to a AttributeTypeIdentifier.
func NewAttributeTypeIdentifier(id string) AttributeTypeIdentifier {
Sietse Ringers's avatar
Sietse Ringers committed
75
	return AttributeTypeIdentifier{metaObjectIdentifier(id)}
76
77
78
79
80
81
82
83
}

// 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.
84
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
85
86
87
	return NewIssuerIdentifier(id.Parent())
}

88
// CredentialTypeIdentifier returns the CredentialTypeIdentifier of the attribute identifier.
89
func (id AttributeTypeIdentifier) CredentialTypeIdentifier() CredentialTypeIdentifier {
90
	return NewCredentialTypeIdentifier(id.Parent())
91
}
92

Sietse Ringers's avatar
Sietse Ringers committed
93
94
// IsCredential returns true if this attribute refers to its containing credential
// (i.e., it consists of only 3 parts).
95
96
97
func (id AttributeTypeIdentifier) IsCredential() bool {
	return strings.Count(id.String(), ".") == 2
}
98

Sietse Ringers's avatar
Sietse Ringers committed
99
// CredentialIdentifier returns the credential identifier of this attribute.
100
101
102
func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
	return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Index: ai.Index, Count: ai.Count}
}
103

104
105
106
// MarshalText implements encoding.TextMarshaler.
func (id SchemeManagerIdentifier) MarshalText() ([]byte, error) {
	return []byte(id.String()), nil
107
108
}

109
110
111
112
113
114
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *SchemeManagerIdentifier) UnmarshalText(text []byte) error {
	*id = NewSchemeManagerIdentifier(string(text))
	return nil
}

115
116
117
118
119
120
121
122
123
124
125
// TODO enable this when updating protocol
//// MarshalText implements encoding.TextMarshaler.
//func (id IssuerIdentifier) MarshalText() ([]byte, error) {
//	return []byte(id.String()), nil
//}
//
//// UnmarshalText implements encoding.TextUnmarshaler.
//func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
//	*id = NewIssuerIdentifier(string(text))
//	return nil
//}
126

127
128
129
130
131
132
133
134
// 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))
135
136
	return nil
}
Sietse Ringers's avatar
Sietse Ringers committed
137

138
139
140
141
142
143
144
145
// 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
146
147
	return nil
}