descriptions.go 4.94 KB
Newer Older
1
package irma
2
3
4
5

import (
	"encoding/xml"

Sietse Ringers's avatar
Sietse Ringers committed
6
	"github.com/go-errors/errors"
7
8
)

9
10
11
// This file contains data types for scheme managers, issuers, credential types
// matching the XML files in irma_configuration.

12
13
// SchemeManager describes a scheme manager.
type SchemeManager struct {
14
15
	ID                string           `xml:"Id"`
	Name              TranslatedString `xml:"Name"`
16
17
	URL               string           `xml:"Url"`
	Contact           string           `xml:"contact"`
18
19
20
21
22
23
	Description       TranslatedString
	KeyshareServer    string
	KeyshareWebsite   string
	KeyshareAttribute string
	XMLVersion        int      `xml:"version,attr"`
	XMLName           xml.Name `xml:"SchemeManager"`
24
25

	index SchemeManagerIndex
26
27
28
29
}

// Issuer describes an issuer.
type Issuer struct {
30
31
32
33
34
35
36
37
	ID              string           `xml:"ID"`
	Name            TranslatedString `xml:"Name"`
	ShortName       TranslatedString `xml:"ShortName"`
	SchemeManagerID string           `xml:"SchemeManager"`
	ContactAddress  string
	ContactEMail    string
	URL             string `xml:"baseURL"`
	XMLVersion      int    `xml:"version,attr"`
38
39
40
41
}

// CredentialType is a description of a credential type, specifying (a.o.) its name, issuer, and attributes.
type CredentialType struct {
42
43
44
45
46
47
48
49
50
51
	ID              string           `xml:"CredentialID"`
	Name            TranslatedString `xml:"Name"`
	ShortName       TranslatedString `xml:"ShortName"`
	IssuerID        string           `xml:"IssuerID"`
	SchemeManagerID string           `xml:"SchemeManager"`
	IsSingleton     bool             `xml:"ShouldBeSingleton"`
	Description     TranslatedString
	Attributes      []AttributeDescription `xml:"Attributes>Attribute"`
	XMLVersion      int                    `xml:"version,attr"`
	XMLName         xml.Name               `xml:"IssueSpecification"`
52
53
}

54
55
// ContainsAttribute tests whether the specified attribute is contained in this
// credentialtype.
56
func (ct *CredentialType) ContainsAttribute(ai AttributeTypeIdentifier) bool {
57
	if ai.CredentialTypeIdentifier().String() != ct.Identifier().String() {
58
59
60
61
62
63
64
65
66
67
		return false
	}
	for _, desc := range ct.Attributes {
		if desc.ID == ai.Name() {
			return true
		}
	}
	return false
}

68
69
70
71
72
73
74
// AttributeDescription is a description of an attribute within a credential type.
type AttributeDescription struct {
	ID          string `xml:"id,attr"`
	Name        TranslatedString
	Description TranslatedString
}

Sietse Ringers's avatar
Sietse Ringers committed
75
76
// IndexOf returns the index of the specified attribute if present,
// or an error (and -1) if not present.
77
78
79
80
81
82
83
84
85
86
87
88
func (ct CredentialType) IndexOf(ai AttributeTypeIdentifier) (int, error) {
	if ai.CredentialTypeIdentifier() != ct.Identifier() {
		return -1, errors.New("Wrong credential type")
	}
	for i, description := range ct.Attributes {
		if description.ID == ai.Name() {
			return i, nil
		}
	}
	return -1, errors.New("Attribute identifier not found")
}

Sietse Ringers's avatar
Sietse Ringers committed
89
90
// TranslatedString is a map of translated strings.
type TranslatedString map[string]string
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
type xmlTranslation struct {
	XMLName xml.Name
	Text    string `xml:",chardata"`
}

type xmlTranslatedString struct {
	Translations []xmlTranslation `xml:",any"`
}

// MarshalXML implements xml.Marshaler.
func (ts *TranslatedString) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	temp := &xmlTranslatedString{}
	for lang, text := range *ts {
		temp.Translations = append(temp.Translations,
			xmlTranslation{XMLName: xml.Name{Local: lang}, Text: text},
		)
	}
	return e.EncodeElement(temp, start)
}

Sietse Ringers's avatar
Sietse Ringers committed
112
113
114
115
116
117
118
// UnmarshalXML unmarshals an XML tag containing a string translated to multiple languages,
// for example: <Foo><en>Hello world</en><nl>Hallo wereld</nl></Foo>
// into a TranslatedString: { "en": "Hello world" , "nl": "Hallo wereld" }
func (ts *TranslatedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	if map[string]string(*ts) == nil {
		*ts = TranslatedString(make(map[string]string))
	}
119
	temp := &xmlTranslatedString{}
Sietse Ringers's avatar
Sietse Ringers committed
120
121
122
123
124
	if err := d.DecodeElement(temp, &start); err != nil {
		return err
	}
	for _, translation := range temp.Translations {
		(*ts)[translation.XMLName.Local] = translation.Text
125
	}
Sietse Ringers's avatar
Sietse Ringers committed
126
	return nil
127
128
129
}

// Identifier returns the identifier of the specified credential type.
130
131
func (ct *CredentialType) Identifier() CredentialTypeIdentifier {
	return NewCredentialTypeIdentifier(ct.SchemeManagerID + "." + ct.IssuerID + "." + ct.ID)
132
133
134
}

// IssuerIdentifier returns the issuer identifier of the specified credential type.
135
136
func (ct *CredentialType) IssuerIdentifier() IssuerIdentifier {
	return NewIssuerIdentifier(ct.SchemeManagerID + "." + ct.IssuerID)
137
138
139
}

// Identifier returns the identifier of the specified issuer description.
140
141
142
143
144
145
146
func (id *Issuer) Identifier() IssuerIdentifier {
	return NewIssuerIdentifier(id.SchemeManagerID + "." + id.ID)
}

// Identifier returns the identifier of the specified scheme manager.
func (sm *SchemeManager) Identifier() SchemeManagerIdentifier {
	return NewSchemeManagerIdentifier(sm.ID)
147
148
}

149
// Distributed indicates if this scheme manager uses a keyshare server.
Sietse Ringers's avatar
Sietse Ringers committed
150
151
152
func (sm *SchemeManager) Distributed() bool {
	return len(sm.KeyshareServer) > 0
}