Commit 732ee25d authored by Sietse Ringers's avatar Sietse Ringers
Browse files

fix: changed JSON representation of inner conjunctions to always keep track of order

Within a session request, inner conjunctions requesting specific attribute values
used to be serialized to a map. JSON and Go maps are however unordered, and the
order matters: if the requested attributes are received in a different order then
the session will fail. Inner conjunctions using values are now serialized using
the default Go serializer, resulting in a list of requested attributes with their values.
parent 45b21dd9
......@@ -311,9 +311,9 @@ func TestSessionRequests(t *testing.T) {
]
],
[
{
"irma-demo.MijnOverheid.fullName.firstname": "hello"
}
[
{ "type": "irma-demo.MijnOverheid.fullName.firstname", "value": "hello" }
]
]
],
"labels": {
......@@ -366,9 +366,9 @@ func TestSessionRequests(t *testing.T) {
]
],
[
{
"irma-demo.MijnOverheid.fullName.firstname": "hello"
}
[
{ "type": "irma-demo.MijnOverheid.fullName.firstname", "value": "hello" }
]
]
],
"labels": {
......@@ -438,9 +438,9 @@ func TestSessionRequests(t *testing.T) {
]
],
[
{
"irma-demo.MijnOverheid.fullName.firstname": "hello"
}
[
{ "type": "irma-demo.MijnOverheid.fullName.firstname", "value": "hello" }
]
]
],
"labels": {
......@@ -478,19 +478,19 @@ func TestConDisconSingletons(t *testing.T) {
AttributeConDisCon{
AttributeDisCon{
AttributeCon{
NewAttributeRequest("irma-demo.RU.studentCard.studentID"), // singleton
NewAttributeRequest("test.test.email.email"), // singleton
NewAttributeRequest("irma-demo.RU.studentCard.studentID"), // non singleton
NewAttributeRequest("test.test.email.email"), // non singleton
},
},
},
false, // multiple singletons in one inner conjunction is not allowed
false, // multiple non-singletons in one inner conjunction is not allowed
},
{
AttributeConDisCon{
AttributeDisCon{
AttributeCon{
NewAttributeRequest("irma-demo.RU.studentCard.studentID"), // non singleton
NewAttributeRequest("test.test.mijnirma.email"), // non singleton
NewAttributeRequest("test.test.mijnirma.email"), // singleton
},
},
},
......@@ -501,7 +501,7 @@ func TestConDisconSingletons(t *testing.T) {
AttributeDisCon{
AttributeCon{
NewAttributeRequest("irma-demo.MijnOverheid.root.BSN"), // singleton
NewAttributeRequest("test.test.mijnirma.email"), // non singleton
NewAttributeRequest("test.test.mijnirma.email"), // singleton
},
},
},
......
......@@ -54,12 +54,24 @@ func checkType(typ, expected Action) error {
// Reuses AttributeCon.UnmarshalJSON()
func (l *legacyAttributeDisjunction) UnmarshalJSON(bts []byte) error {
var con AttributeCon
if err := json.Unmarshal(bts, &con); err != nil {
return err
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
}
*l = legacyAttributeDisjunction(con)
return nil
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")
}
func (dr *DisclosureRequest) UnmarshalJSON(bts []byte) (err error) {
......
......@@ -167,8 +167,8 @@ type DisclosureChoice struct {
// An AttributeRequest asks for an instance of an attribute type, possibly requiring it to have
// a specified value, in a session request.
type AttributeRequest struct {
Type AttributeTypeIdentifier
Value *string
Type AttributeTypeIdentifier `json:"type"`
Value *string `json:"value"`
}
var (
......@@ -224,20 +224,19 @@ func (c AttributeCon) Validate() error {
func (c *AttributeCon) MarshalJSON() ([]byte, error) {
var vals bool
m := map[AttributeTypeIdentifier]*string{}
l := make([]AttributeTypeIdentifier, 0, len(*c))
for _, attr := range *c {
m[attr.Type] = attr.Value
if attr.Value == nil {
l = append(l, attr.Type)
} else {
vals = true
break
}
}
if vals {
return json.Marshal(m)
return json.Marshal((*[]AttributeRequest)(c))
} else {
return json.Marshal(l)
}
......@@ -254,11 +253,7 @@ func (c *AttributeCon) UnmarshalJSON(bts []byte) error {
return nil
}
m := map[AttributeTypeIdentifier]*string{}
if err = json.Unmarshal(bts, &m); err == nil {
for id, val := range m {
*c = append(*c, AttributeRequest{Type: id, Value: val})
}
if err = json.Unmarshal(bts, (*[]AttributeRequest)(c)); err == nil {
return nil
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment