identifiers.go 4.37 KB
Newer Older
1
2
package irmago

3
4
5
6
import (
	"encoding/json"
	"strings"
)
7

Sietse Ringers's avatar
Sietse Ringers committed
8
type metaObjectIdentifier string
9
10
11

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

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

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

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

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

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

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

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

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

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

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

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

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

// 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.
87
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
88
89
90
	return NewIssuerIdentifier(id.Parent())
}

91
// CredentialTypeIdentifier returns the CredentialTypeIdentifier of the attribute identifier.
92
func (id AttributeTypeIdentifier) CredentialTypeIdentifier() CredentialTypeIdentifier {
93
	return NewCredentialTypeIdentifier(id.Parent())
94
}
95

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

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

Sietse Ringers's avatar
Sietse Ringers committed
107
// MarshalJSON marshals this instance to JSON as a string.
108
109
110
111
func (id AttributeTypeIdentifier) MarshalJSON() ([]byte, error) {
	return json.Marshal(id.String())
}

Sietse Ringers's avatar
Sietse Ringers committed
112
// MarshalJSON marshals this instance to JSON as a string.
113
114
115
func (id CredentialTypeIdentifier) MarshalJSON() ([]byte, error) {
	return json.Marshal(id.String())
}
116

Sietse Ringers's avatar
Sietse Ringers committed
117
118
// UnmarshalJSON unmarshals this instance from JSON.
func (id *AttributeTypeIdentifier) UnmarshalJSON(b []byte) error {
119
120
121
122
123
124
125
126
127
	var val string
	err := json.Unmarshal(b, &val)
	if err != nil {
		return err
	}
	id.metaObjectIdentifier = metaObjectIdentifier(val)
	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
128
// UnmarshalJSON unmarshals this instance from JSON.
Sietse Ringers's avatar
Sietse Ringers committed
129
func (id *CredentialTypeIdentifier) UnmarshalJSON(b []byte) error {
130
131
132
133
134
135
136
137
	var val string
	err := json.Unmarshal(b, &val)
	if err != nil {
		return err
	}
	id.metaObjectIdentifier = metaObjectIdentifier(val)
	return nil
}
Sietse Ringers's avatar
Sietse Ringers committed
138
139
140
141
142
143

// TODO this also for the other identifiers
func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
	*id = NewIssuerIdentifier(string(text))
	return nil
}