Skip to content

Commit

Permalink
allow regex in claimrules
Browse files Browse the repository at this point in the history
  • Loading branch information
tommartensen committed Dec 17, 2024
1 parent 5a2d4be commit b909935
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion auth/claimrule/claim_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/jeremywohl/flatten/v2"
Expand Down Expand Up @@ -70,9 +71,14 @@ func (cr *ClaimRule) equalCheck(flatTokenClaims map[string]interface{}, jsonPath
return errors.Errorf("expected claim %q is not found", jsonPath)
}

if cr.Value != tokenClaimValue {
pattern := fmt.Sprintf("^%s$", cr.Value)
found, err := regexp.MatchString(pattern, tokenClaimValue.(string))
if !found {
return errors.Errorf("expected claim %q is not correct", jsonPath)
}
if err != nil {
return errors.Wrapf(err, "error matching claim %s to expected value", tokenClaimValue)
}

return nil
}
Expand Down
33 changes: 33 additions & 0 deletions auth/claimrule/claim_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ func getDataSets() map[string]dataSet {
}},
err: true,
},
"eq-regex-match": {
tokenClaims: map[string]interface{}{
"field": "val1",
},
rules: ClaimRules{{
Value: "(val1|val2)",
Path: "field",
Op: "eq",
}},
err: false,
},
"eq-regex-no-match": {
tokenClaims: map[string]interface{}{
"field": "val3",
},
rules: ClaimRules{{
Value: "(val1|val2)",
Path: "field",
Op: "eq",
}},
err: true,
},
"in-regex-match": {
tokenClaims: map[string]interface{}{
"field": []string{"val1", "val2"},
},
rules: ClaimRules{{
Value: "(val2|val3)",
Path: "field",
Op: "in",
}},
err: false,
},
}
}

Expand Down

0 comments on commit b909935

Please sign in to comment.