Skip to content

Commit c1342b6

Browse files
claims: add JSON serialization for interface arrays
Implement handling of []interface{} types by serializing them to JSON string format. This allows arrays like ["role1", "role2"] to be converted to string representations for further processing.
1 parent 59cef11 commit c1342b6

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

.changelog/26958.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
oidc: add support for array-based OIDC claims
3+
```

lib/auth/claims.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,23 @@ func getClaim(all map[string]interface{}, claim string) interface{} {
152152
// stringifyClaimValue will try to convert the provided raw value into a
153153
// faithful string representation of that value per these rules:
154154
//
155-
// - strings => unchanged
156-
// - bool => "true" / "false"
157-
// - json.Number => String()
158-
// - float32/64 => truncated to int64 and then formatted as an ascii string
159-
// - intXX/uintXX => casted to int64 and then formatted as an ascii string
155+
// - []interface{} => marshaling to JSON string
156+
// - strings => unchanged
157+
// - bool => "true" / "false"
158+
// - json.Number => String()
159+
// - float32/64 => truncated to int64 and then formatted as an ascii string
160+
// - intXX/uintXX => casted to int64 and then formatted as an ascii string
160161
//
161162
// If successful the string value and true are returned. otherwise an empty
162163
// string and false are returned.
163164
func stringifyClaimValue(rawValue interface{}) (string, bool) {
164165
switch v := rawValue.(type) {
166+
case []interface{}:
167+
b, err := json.Marshal(v)
168+
if err != nil {
169+
return "", false
170+
}
171+
return string(b), true
165172
case string:
166173
return v, true
167174
case bool:

lib/auth/claims_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ func TestSelectorData(t *testing.T) {
6868
},
6969
},
7070
},
71+
72+
{
73+
"nested list claim",
74+
nil,
75+
map[string]string{"roles": "r"},
76+
map[string]any{
77+
"roles": []any{
78+
[]any{"role1", "role2", "roleN"}, 42, false,
79+
},
80+
},
81+
&structs.ACLAuthClaims{
82+
Value: map[string]string{},
83+
List: map[string][]string{
84+
"r": {`["role1","role2","roleN"]`, "42", "false"},
85+
},
86+
},
87+
},
88+
7189
}
7290

7391
for _, tt := range cases {

0 commit comments

Comments
 (0)