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

import (
	"encoding/xml"
Sietse Ringers's avatar
Sietse Ringers committed
5
	"fmt"
6
	"path/filepath"
7

Sietse Ringers's avatar
Sietse Ringers committed
8
	"github.com/go-errors/errors"
Sietse Ringers's avatar
Sietse Ringers committed
9
	"github.com/privacybydesign/irmago/internal/fs"
10
11
)

12
13
14
// This file contains data types for scheme managers, issuers, credential types
// matching the XML files in irma_configuration.

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

29
30
	Status SchemeManagerStatus `xml:"-"`
	Valid  bool                `xml:"-"` // true iff Status == SchemeManagerStatusValid
31

32
33
	Timestamp Timestamp

34
	index SchemeManagerIndex
35
36
}

37
38
39
40
41
type SchemeAppVersion struct {
	Android int `xml:"Android"`
	IOS     int `xml:"iOS"`
}

42
43
// Issuer describes an issuer.
type Issuer struct {
44
45
46
47
48
49
	ID              string           `xml:"ID"`
	Name            TranslatedString `xml:"Name"`
	ShortName       TranslatedString `xml:"ShortName"`
	SchemeManagerID string           `xml:"SchemeManager"`
	ContactAddress  string
	ContactEMail    string
50
	XMLVersion      int `xml:"version,attr"`
51
52

	Valid bool `xml:"-"`
53
54
55
56
}

// CredentialType is a description of a credential type, specifying (a.o.) its name, issuer, and attributes.
type CredentialType struct {
57
58
59
60
61
62
63
	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
64
	AttributeTypes  []*AttributeType `xml:"Attributes>Attribute" json:"-"`
65
66
	XMLVersion      int              `xml:"version,attr"`
	XMLName         xml.Name         `xml:"IssueSpecification"`
67
	IssueURL        TranslatedString `xml:"IssueURL"`
68
69

	Valid bool `xml:"-"`
70
71
}

72
73
// AttributeType is a description of an attribute within a credential type.
type AttributeType struct {
74
	ID          string `xml:"id,attr"`
75
	Optional    string `xml:"optional,attr"  json:",omitempty"`
76
77
	Name        TranslatedString
	Description TranslatedString
78
79
80
81
82
83
84
85

	Index        int  `xml:"-"`
	DisplayIndex *int `xml:"displayIndex,attr" json:",omitempty"`

	// Taken from containing CredentialType
	CredentialTypeID string `xml:"-"`
	IssuerID         string `xml:"-"`
	SchemeManagerID  string `xml:"-"`
86
87
}

88
89
func (ad AttributeType) GetAttributeTypeIdentifier() AttributeTypeIdentifier {
	return NewAttributeTypeIdentifier(fmt.Sprintf("%s.%s.%s.%s", ad.SchemeManagerID, ad.IssuerID, ad.CredentialTypeID, ad.ID))
90
91
}

92
func (ad AttributeType) IsOptional() bool {
93
94
95
	return ad.Optional == "true"
}

96
97
// ContainsAttribute tests whether the specified attribute is contained in this
// credentialtype.
98
func (ct *CredentialType) ContainsAttribute(ai AttributeTypeIdentifier) bool {
99
	if ai.CredentialTypeIdentifier().String() != ct.Identifier().String() {
100
101
		return false
	}
102
	for _, desc := range ct.AttributeTypes {
103
104
105
106
107
108
109
		if desc.ID == ai.Name() {
			return true
		}
	}
	return false
}

Sietse Ringers's avatar
Sietse Ringers committed
110
111
// IndexOf returns the index of the specified attribute if present,
// or an error (and -1) if not present.
112
113
114
115
func (ct CredentialType) IndexOf(ai AttributeTypeIdentifier) (int, error) {
	if ai.CredentialTypeIdentifier() != ct.Identifier() {
		return -1, errors.New("Wrong credential type")
	}
116
	for i, description := range ct.AttributeTypes {
117
118
119
120
121
122
123
		if description.ID == ai.Name() {
			return i, nil
		}
	}
	return -1, errors.New("Attribute identifier not found")
}

124
func (ct CredentialType) AttributeType(ai AttributeTypeIdentifier) *AttributeType {
125
126
127
128
	i, _ := ct.IndexOf(ai)
	if i == -1 {
		return nil
	}
129
	return ct.AttributeTypes[i]
130
131
}

Sietse Ringers's avatar
Sietse Ringers committed
132
133
// TranslatedString is a map of translated strings.
type TranslatedString map[string]string
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
155
156
157
158
159
160
161
// 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))
	}
162
	temp := &xmlTranslatedString{}
Sietse Ringers's avatar
Sietse Ringers committed
163
164
165
166
167
	if err := d.DecodeElement(temp, &start); err != nil {
		return err
	}
	for _, translation := range temp.Translations {
		(*ts)[translation.XMLName.Local] = translation.Text
168
	}
Sietse Ringers's avatar
Sietse Ringers committed
169
	return nil
170
171
172
}

// Identifier returns the identifier of the specified credential type.
173
174
func (ct *CredentialType) Identifier() CredentialTypeIdentifier {
	return NewCredentialTypeIdentifier(ct.SchemeManagerID + "." + ct.IssuerID + "." + ct.ID)
175
176
177
}

// IssuerIdentifier returns the issuer identifier of the specified credential type.
178
179
func (ct *CredentialType) IssuerIdentifier() IssuerIdentifier {
	return NewIssuerIdentifier(ct.SchemeManagerID + "." + ct.IssuerID)
180
181
}

182
183
184
185
func (ct *CredentialType) SchemeManagerIdentifier() SchemeManagerIdentifier {
	return NewSchemeManagerIdentifier(ct.SchemeManagerID)
}

Sietse Ringers's avatar
Sietse Ringers committed
186
func (ct *CredentialType) Logo(conf *Configuration) string {
187
	path := filepath.Join(conf.Path, ct.SchemeManagerID, ct.IssuerID, "Issues", ct.ID, "logo.png")
Sietse Ringers's avatar
Sietse Ringers committed
188
189
190
191
192
193
194
	exists, err := fs.PathExists(path)
	if err != nil || !exists {
		return ""
	}
	return path
}

195
// Identifier returns the identifier of the specified issuer description.
196
197
198
199
func (id *Issuer) Identifier() IssuerIdentifier {
	return NewIssuerIdentifier(id.SchemeManagerID + "." + id.ID)
}

200
201
202
203
func (id *Issuer) SchemeManagerIdentifier() SchemeManagerIdentifier {
	return NewSchemeManagerIdentifier(id.SchemeManagerID)
}

204
205
206
207
func NewSchemeManager(name string) *SchemeManager {
	return &SchemeManager{ID: name, Status: SchemeManagerStatusUnprocessed, Valid: false}
}

208
209
210
// Identifier returns the identifier of the specified scheme manager.
func (sm *SchemeManager) Identifier() SchemeManagerIdentifier {
	return NewSchemeManagerIdentifier(sm.ID)
211
212
}

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