Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret bound claims value as a "matches any of" test if a list is provided #41

Merged
merged 5 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,27 @@ func validateBoundClaims(logger log.Logger, boundClaims, allClaims map[string]in
return fmt.Errorf("claim %q is missing", claim)
}

if expValue != actValue {
return fmt.Errorf("claim %q does not match associated bound claim", claim)
var expVals []interface{}

switch v := expValue.(type) {
case []interface{}:
expVals = v
case string:
expVals = []interface{}{v}
default:
return fmt.Errorf("bound claim is not a string or list: %v", expValue)
}

found := false
for _, v := range expVals {
if actValue == v {
found = true
break
}
}

if !found {
return fmt.Errorf("claim %q does not match any associated bound claim values", claim)
}
}

Expand Down
34 changes: 34 additions & 0 deletions claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,40 @@ func TestValidateBoundClaims(t *testing.T) {
},
errExpected: true,
},
{
name: "valid - match alternates",
boundClaims: map[string]interface{}{
"email": []interface{}{"a", "b", "c"},
"color": "green",
},
allClaims: map[string]interface{}{
"email": "c",
"color": "green",
},
errExpected: false,
},
{
name: "invalid - no match alternates",
boundClaims: map[string]interface{}{
"email": []interface{}{"a", "b", "c"},
"color": "green",
},
allClaims: map[string]interface{}{
"email": "d",
"color": "green",
},
errExpected: true,
},
{
name: "invalid bound claim expected value",
boundClaims: map[string]interface{}{
"email": 42,
},
allClaims: map[string]interface{}{
"email": "d",
},
errExpected: true,
},
}
for _, tt := range tests {
if err := validateBoundClaims(hclog.NewNullLogger(), tt.boundClaims, tt.allClaims); (err != nil) != tt.errExpected {
Expand Down