identifiers.go 2.88 KB
Newer Older
1
2
3
4
package irmago

import "strings"

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
12
13
}

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

27
28
29
30
31
32
33
34
35
36
37
38
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
39
func (oi metaObjectIdentifier) Parent() string {
40
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
41
	return str[:strings.LastIndex(str, ".")]
42
43
}

Sietse Ringers's avatar
Sietse Ringers committed
44
func (oi metaObjectIdentifier) Name() string {
45
	str := string(oi)
Sietse Ringers's avatar
Sietse Ringers committed
46
	return str[strings.LastIndex(str, ".")+1:]
47
48
}

Sietse Ringers's avatar
Sietse Ringers committed
49
func (oi metaObjectIdentifier) String() string {
50
51
52
53
54
	return string(oi)
}

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

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

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

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

// 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.
79
func (id CredentialTypeIdentifier) IssuerIdentifier() IssuerIdentifier {
80
81
82
	return NewIssuerIdentifier(id.Parent())
}

83
// CredentialTypeIdentifier returns the CredentialTypeIdentifier of the attribute identifier.
84
func (id AttributeTypeIdentifier) CredentialTypeIdentifier() CredentialTypeIdentifier {
85
	return NewCredentialTypeIdentifier(id.Parent())
86
}
87
88
89
90

func (id AttributeTypeIdentifier) IsCredential() bool {
	return strings.Count(id.String(), ".") == 2
}
91
92
93
94

func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
	return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Index: ai.Index, Count: ai.Count}
}