legacy.go 8.93 KB
Newer Older
1
2
3
4
5
6
7
8
package irma

import (
	"encoding/json"

	"github.com/go-errors/errors"
)

9
10
11
12
13
14
15
16
17
18
19
20
// This file contains compatibility code for the legacy, pre-condiscon session requests,
// which supported only condis requests.
//
// Old requests can always be converted to new requests, and this is automatically done by the JSON
// unmarshaler when it encounters a legacy session request.
// New requests can be converted to old requests only if all inner conjunctions contain exactly
// 1 attribute. This can be done using the Legacy() method.
//
// Droppping compatibility with pre-condiscon session requests should thus be more or less:
// 1. delete this file
// 2. solve all compiler errors by removing the lines on which they occur
// 3. adjust the minimal supported protocol version in irmaclient and server
21

22
23
24
25
26
27
28
29
30
// LegacyDisjunction is a disjunction of attributes from before the condiscon feature,
// representing a list of attribute types one of which must be given by the user,
// possibly requiring specific values. (C.f. AttributeCon, also defined as []AttributeRequest,
// which is only satisfied if all listed attributes are given by the user.)
type LegacyDisjunction []AttributeRequest

type LegacyLabeledDisjunction struct {
	Label      string            `json:"label"`
	Attributes LegacyDisjunction `json:"attributes"`
31
32
}

33
type LegacyDisclosureRequest struct {
34
	BaseRequest
35
	Content []LegacyLabeledDisjunction `json:"content"`
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
func (dr *LegacyDisclosureRequest) Validate() error                 { panic("not implemented") }
func (dr *LegacyDisclosureRequest) Disclosure() *DisclosureRequest  { panic("not implemented") }
func (dr *LegacyDisclosureRequest) Identifiers() *IrmaIdentifierSet { panic("not implemented") }
func (dr *LegacyDisclosureRequest) Base() *BaseRequest              { return &dr.BaseRequest }
func (dr *LegacyDisclosureRequest) Action() Action                  { return dr.Type }
func (dr *LegacyDisclosureRequest) Legacy() (SessionRequest, error) { return dr, nil }

type LegacySignatureRequest struct {
	LegacyDisclosureRequest
	Message string `json:"message"`
}

type LegacyIssuanceRequest struct {
	BaseRequest
	Credentials []*CredentialRequest       `json:"credentials"`
	Disclose    []LegacyLabeledDisjunction `json:"disclose"`
}

func (ir *LegacyIssuanceRequest) Validate() error                 { panic("not implemented") }
func (ir *LegacyIssuanceRequest) Disclosure() *DisclosureRequest  { panic("not implemented") }
func (ir *LegacyIssuanceRequest) Identifiers() *IrmaIdentifierSet { panic("not implemented") }
func (ir *LegacyIssuanceRequest) Base() *BaseRequest              { return &ir.BaseRequest }
func (ir *LegacyIssuanceRequest) Action() Action                  { return ir.Type }
func (ir *LegacyIssuanceRequest) Legacy() (SessionRequest, error) { return ir, nil }

func convertConDisCon(cdc AttributeConDisCon, labels map[int]TranslatedString) ([]LegacyLabeledDisjunction, error) {
	var disjunctions []LegacyLabeledDisjunction
	for i, dis := range cdc {
		l := LegacyLabeledDisjunction{}
		for _, con := range dis {
			if len(con) != 1 {
				return nil, errors.New("request not convertible to legacy request")
			}
			l.Attributes = append(l.Attributes, AttributeRequest{Type: con[0].Type, Value: con[0].Value})
		}
		l.Label = labels[i]["en"]
		if l.Label == "" {
			l.Label = l.Attributes[0].Type.Name()
		}
		disjunctions = append(disjunctions, l)
	}
	return disjunctions, nil
}

func convertDisjunctions(disjunctions []LegacyLabeledDisjunction) (
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
	condiscon AttributeConDisCon, labels map[int]TranslatedString,
) {
	labels = make(map[int]TranslatedString)
	condiscon = make(AttributeConDisCon, len(disjunctions))

	for i, dis := range disjunctions {
		condiscon[i] = AttributeDisCon{}
		for _, attr := range dis.Attributes {
			condiscon[i] = append(condiscon[i], AttributeCon{{Type: attr.Type, Value: attr.Value}})
		}
		labels[i] = TranslatedString{"en": dis.Label, "nl": dis.Label}
	}

	return
}

func parseVersion(bts []byte) (int, error) {
	var v struct {
		Version int `json:"v"`
	}
	if err := json.Unmarshal(bts, &v); err != nil {
		return 0, err
	}
	return v.Version, nil
}

func checkType(typ, expected Action) error {
	if typ != expected {
		return errors.New("not a " + expected + " session request")
	}
	return nil
}

116
func (l *LegacyDisjunction) UnmarshalJSON(bts []byte) error {
117
118
119
120
121
122
123
	var err error
	var lst []AttributeTypeIdentifier
	if err = json.Unmarshal(bts, &lst); err == nil {
		for _, id := range lst {
			*l = append(*l, AttributeRequest{Type: id})
		}
		return nil
124
	}
125
126
127
128
129
130
131
132
133
134

	m := map[AttributeTypeIdentifier]*string{}
	if err = json.Unmarshal(bts, &m); err == nil {
		for id, val := range m {
			*l = append(*l, AttributeRequest{Type: id, Value: val})
		}
		return nil
	}

	return errors.New("Failed to unmarshal legacy attribute conjunction")
135
136
}

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
func (l *LegacyDisjunction) MarshalJSON() ([]byte, error) {
	hasvalues := false
	for _, r := range *l {
		if r.Value != nil {
			hasvalues = true
			break
		}
	}

	var tmp interface{}
	if hasvalues {
		m := map[AttributeTypeIdentifier]*string{}
		for _, r := range *l {
			m[r.Type] = r.Value
		}
		tmp = m
	} else {
		var m []AttributeTypeIdentifier
		for _, r := range *l {
			m = append(m, r.Type)
		}
		tmp = m
	}

	return json.Marshal(tmp)
}

func (dr *DisclosureRequest) Legacy() (SessionRequest, error) {
	disjunctions, err := convertConDisCon(dr.Disclose, dr.Labels)
	if err != nil {
		return nil, err
	}
	return &LegacyDisclosureRequest{
		BaseRequest: BaseRequest{
			Type:            dr.Type,
			Context:         dr.Context,
			Nonce:           dr.Nonce,
			ProtocolVersion: dr.ProtocolVersion,
		},
		Content: disjunctions,
	}, nil
}

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
func (dr *DisclosureRequest) UnmarshalJSON(bts []byte) (err error) {
	var version int
	if version, err = parseVersion(bts); err != nil {
		return err
	}

	if version >= 2 {
		type newDisclosureRequest DisclosureRequest // Same type with default JSON unmarshaler
		var req newDisclosureRequest
		if err = json.Unmarshal(bts, &req); err != nil {
			return err
		}
		*dr = DisclosureRequest(req)
		return nil
	}

196
	var legacy LegacyDisclosureRequest
197
198
199
200
201
202
203
204
205
206
	if err = json.Unmarshal(bts, &legacy); err != nil {
		return err
	}
	dr.BaseRequest = legacy.BaseRequest
	dr.Version = 2
	dr.Disclose, dr.Labels = convertDisjunctions(legacy.Content)

	return checkType(legacy.Type, ActionDisclosing)
}

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
func (sr *SignatureRequest) Legacy() (SessionRequest, error) {
	disjunctions, err := convertConDisCon(sr.Disclose, sr.Labels)
	if err != nil {
		return nil, err
	}
	return &LegacySignatureRequest{
		Message: sr.Message,
		LegacyDisclosureRequest: LegacyDisclosureRequest{
			BaseRequest: BaseRequest{
				Type:            sr.Type,
				Context:         sr.Context,
				Nonce:           sr.Nonce,
				ProtocolVersion: sr.ProtocolVersion,
			},
			Content: disjunctions,
		},
	}, nil
}

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
func (sr *SignatureRequest) UnmarshalJSON(bts []byte) (err error) {
	var version int
	if version, err = parseVersion(bts); err != nil {
		return err
	}

	if version >= 2 {
		var req struct { // Identical type with default JSON unmarshaler
			BaseRequest
			Disclose AttributeConDisCon       `json:"disclose"`
			Labels   map[int]TranslatedString `json:"labels"`
			Message  string                   `json"string"`
		}
		if err = json.Unmarshal(bts, &req); err != nil {
			return err
		}
		*sr = SignatureRequest{
			DisclosureRequest{
				req.BaseRequest,
				req.Disclose,
				req.Labels,
			},
			req.Message,
		}
		return nil
	}

253
	var legacy LegacySignatureRequest
254
255
256
257
258
259
260
261
262
263
264
	if err = json.Unmarshal(bts, &legacy); err != nil {
		return err
	}
	sr.BaseRequest = legacy.BaseRequest
	sr.Version = 2
	sr.Disclose, sr.Labels = convertDisjunctions(legacy.Content)
	sr.Message = legacy.Message

	return checkType(legacy.Type, ActionSigning)
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
func (ir *IssuanceRequest) Legacy() (SessionRequest, error) {
	disjunctions, err := convertConDisCon(ir.Disclose, ir.Labels)
	if err != nil {
		return nil, err
	}
	return &LegacyIssuanceRequest{
		BaseRequest: BaseRequest{
			Type:            ir.Type,
			Context:         ir.Context,
			Nonce:           ir.Nonce,
			ProtocolVersion: ir.ProtocolVersion,
		},
		Credentials: ir.Credentials,
		Disclose:    disjunctions,
	}, nil
}

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
func (ir *IssuanceRequest) UnmarshalJSON(bts []byte) (err error) {
	var version int
	if version, err = parseVersion(bts); err != nil {
		return err
	}

	if version >= 2 {
		var req struct { // Identical type with default JSON unmarshaler
			BaseRequest
			Disclose    AttributeConDisCon       `json:"disclose"`
			Labels      map[int]TranslatedString `json:"labels"`
			Credentials []*CredentialRequest     `json:"credentials"`
		}
		if err = json.Unmarshal(bts, &req); err != nil {
			return err
		}
		*ir = IssuanceRequest{
			DisclosureRequest: DisclosureRequest{req.BaseRequest, req.Disclose, req.Labels},
			Credentials:       req.Credentials,
		}
		return nil
	}

305
	var legacy LegacyIssuanceRequest
306
307
308
309
310
311
312
313
314
315
	if err = json.Unmarshal(bts, &legacy); err != nil {
		return err
	}
	ir.BaseRequest = legacy.BaseRequest
	ir.Version = 2
	ir.Credentials = legacy.Credentials
	ir.Disclose, ir.Labels = convertDisjunctions(legacy.Disclose)

	return checkType(legacy.Type, ActionIssuing)
}