descriptions.go 4.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package irmago

import (
	"encoding/xml"

	"github.com/mhe/gabi"
)

// SchemeManager describes a scheme manager.
type SchemeManager struct {
	Name              string           `xml:"Id"`
	URL               string           `xml:"Contact"`
	HRName            TranslatedString `xml:"Name"`
	Description       TranslatedString
	KeyshareServer    string
	KeyshareWebsite   string
	KeyshareAttribute string
	XMLVersion        int      `xml:"version,attr"`
	XMLName           xml.Name `xml:"SchemeManager"`
}

// Issuer describes an issuer.
type Issuer struct {
	HRName            TranslatedString `xml:"Name"`
	HRShortName       TranslatedString `xml:"ShortName"`
	Name              string           `xml:"ID"`
	SchemeManagerName string           `xml:"SchemeManager"`
	ContactAddress    string
	ContactEMail      string
	URL               string `xml:"baseURL"`
	XMLVersion        int    `xml:"version,attr"`
}

// CredentialType is a description of a credential type, specifying (a.o.) its name, issuer, and attributes.
type CredentialType struct {
	HRName            TranslatedString `xml:"Name"`
	HRShortName       TranslatedString `xml:"ShortName"`
	IssuerName        string           `xml:"IssuerID"`
	SchemeManagerName string           `xml:"SchemeManager"`
	Name              string           `xml:"CredentialID"`
	IsSingleton       bool             `xml:"ShouldBeSingleton"`
	Description       TranslatedString
	Attributes        []AttributeDescription `xml:"Attributes>Attribute"`
	XMLVersion        int                    `xml:"version,attr"`
	XMLName           xml.Name               `xml:"IssueSpecification"`
}

// AttributeDescription is a description of an attribute within a credential type.
type AttributeDescription struct {
	ID          string `xml:"id,attr"`
	Name        TranslatedString
	Description TranslatedString
}

// TranslatedString represents an XML tag containing a string translated to multiple languages.
// For example: <Foo id="bla"><Translation lang="en">Hello world</Translation><Translation lang="nl">Hallo wereld</Translation></Foo>
// type TranslatedString struct {
// 	Translations []struct {
// 		Language string `xml:"lang,attr"`
// 		Value    string `xml:",chardata"`
// 	} `xml:"Translation"`
// 	ID string `xml:"id,attr"`
// }
//
// // Get returns the specified translation
// func (ts TranslatedString) Get(lang string) string {
// 	for _, l := range ts.Translations {
// 		if l.Language == lang {
// 			return l.Value
// 		}
// 	}
// 	return ""
// }

// TranslatedString represents an XML tag containing a string translated to multiple languages.
// For example: <Foo id="bla"><en>Hello world</en><nl>Hallo wereld</nl></Foo>
type TranslatedString struct {
	Translations []struct {
		XMLName xml.Name
		Text    string `xml:",chardata"`
	} `xml:",any"`
}

// Translation returns the specified translation.
func (ts *TranslatedString) Translation(lang string) string {
	for _, translation := range ts.Translations {
		if translation.XMLName.Local == lang {
			return translation.Text
		}
	}
	return ""
}

// Identifier returns the identifier of the specified credential type.
func (cd *CredentialType) Identifier() string {
	return cd.SchemeManagerName + "." + cd.IssuerName + "." + cd.Name
}

// IssuerIdentifier returns the issuer identifier of the specified credential type.
func (cd *CredentialType) IssuerIdentifier() string {
	return cd.SchemeManagerName + "." + cd.IssuerName
}

// Identifier returns the identifier of the specified issuer description.
func (id *Issuer) Identifier() string {
	return id.SchemeManagerName + "." + id.Name
}

// CurrentPublicKey returns the latest known public key of the issuer identified by this instance.
func (id *Issuer) CurrentPublicKey() *gabi.PublicKey {
	keys := MetaStore.PublicKeys[id.Identifier()]
	if keys == nil || len(keys) == 0 {
		return nil
	}
	return keys[len(keys)-1]
}

// PublicKey returns the specified public key of the issuer identified by this instance.
func (id *Issuer) PublicKey(index int) *gabi.PublicKey {
	keys := MetaStore.PublicKeys[id.Identifier()]
	if keys == nil || index >= len(keys) {
		return nil
	}
	return keys[index]
}