From 7326285c8af5c42e5c0c2d729ab224cf33ac3a1f Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Wed, 10 Apr 2024 09:30:53 +0200 Subject: [PATCH] feat: add array attribute mapping for SAML (#1526) By adding the `"array": true` option in the JSON SAML attribute mapping document for a key, the SAML attribute(s) for that key will be represented as an array in the user identity claims. --- internal/api/samlassertion.go | 13 +++++++++-- internal/api/samlassertion_test.go | 36 ++++++++++++++++++++++++++++++ internal/models/sso.go | 5 +++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/internal/api/samlassertion.go b/internal/api/samlassertion.go index 0d7c363bb..75cdfdb4f 100644 --- a/internal/api/samlassertion.go +++ b/internal/api/samlassertion.go @@ -128,9 +128,18 @@ func (a *SAMLAssertion) Process(mapping models.SAMLAttributeMapping) map[string] for _, name := range names { for _, attr := range a.Attribute(name) { if attr.Value != "" { - ret[key] = attr.Value setKey = true - break + + if mapper.Array { + if ret[key] == nil { + ret[key] = []string{} + } + + ret[key] = append(ret[key].([]string), attr.Value) + } else { + ret[key] = attr.Value + break + } } } diff --git a/internal/api/samlassertion_test.go b/internal/api/samlassertion_test.go index 9ec061f49..47992a214 100644 --- a/internal/api/samlassertion_test.go +++ b/internal/api/samlassertion_test.go @@ -204,6 +204,42 @@ func TestSAMLAssertionProcessing(t *tst.T) { "email": "soap@example.com", }, }, + { + xml: ` + + + + group1 + group2 + + + soap@example.com + + + +`, + mapping: models.SAMLAttributeMapping{ + Keys: map[string]models.SAMLAttribute{ + "email": { + Names: []string{ + "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", + "http://schemas.xmlsoap.org/claims/EmailAddress", + }, + }, + "groups": { + Name: "groups", + Array: true, + }, + }, + }, + expected: map[string]interface{}{ + "email": "soap@example.com", + "groups": []string{ + "group1", + "group2", + }, + }, + }, } for i, example := range examples { diff --git a/internal/models/sso.go b/internal/models/sso.go index bbdd138ce..1cf982604 100644 --- a/internal/models/sso.go +++ b/internal/models/sso.go @@ -36,6 +36,7 @@ type SAMLAttribute struct { Name string `json:"name,omitempty"` Names []string `json:"names,omitempty"` Default interface{} `json:"default,omitempty"` + Array bool `json:"array,omitempty"` } type SAMLAttributeMapping struct { @@ -78,6 +79,10 @@ func (m *SAMLAttributeMapping) Equal(o *SAMLAttributeMapping) bool { if mvalue.Default != value.Default { return false } + + if mvalue.Array != value.Array { + return false + } } return true