identifiers.go 3.11 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
15
16
}

// IssuerIdentifier identifies an inssuer. For example "irma-demo.RU".
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
}

30
31
32
33
34
35
36
37
38
39
40
41
type CredentialIdentifier struct {
	Type  CredentialTypeIdentifier
	Index int
	Count int
}

type AttributeIdentifier struct {
	Type  AttributeTypeIdentifier
	Index int
	Count int
}

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
func (oi metaObjectIdentifier) Name() string {
48
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
49
	return str[strings.LastIndex(str, ".")+1:]
50
51
}

Sietse Ringers's avatar
Sietse Ringers committed
52
func (oi metaObjectIdentifier) String() string {
53
54
55
56
57
	return string(oi)
}

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

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

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

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

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

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

func (id AttributeTypeIdentifier) IsCredential() bool {
	return strings.Count(id.String(), ".") == 2
}
94
95
96
97

func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
	return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Index: ai.Index, Count: ai.Count}
}
98
99
100
101
102
103
104
105

func (id AttributeTypeIdentifier) MarshalJSON() ([]byte, error) {
	return json.Marshal(id.String())
}

func (id CredentialTypeIdentifier) MarshalJSON() ([]byte, error) {
	return json.Marshal(id.String())
}