Skip to content

Commit

Permalink
add regex for oidc group matching
Browse files Browse the repository at this point in the history
Signed-off-by: Jackson Argo <[email protected]>
  • Loading branch information
jacksonargo committed Nov 6, 2023
1 parent e41a28b commit 5df1605
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions connector/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -93,6 +94,7 @@ type Config struct {
// ClaimMutations holds all claim mutations options
ClaimMutations struct {
NewGroupFromClaims []NewGroupFromClaims `json:"newGroupFromClaims"`
FilterGroupClaims FilterGroupClaims `json:"filterGroupClaims"`
} `json:"claimModifications"`
}

Expand All @@ -112,6 +114,12 @@ type NewGroupFromClaims struct {
Prefix string `json:"prefix"`
}

// FilterGroupClaims is a regex filter for to keep only the matching groups.
// This is useful when the groups list is too large to fit within an HTTP header.
type FilterGroupClaims struct {
GroupsFilter string `json:"groupsFilter"`
}

// Domains that don't support basic auth. golang.org/x/oauth2 has an internal
// list, but it only matches specific URLs, not top level domains.
var brokenAuthHeaderDomains = []string{
Expand Down Expand Up @@ -184,6 +192,14 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
c.PromptType = "consent"
}

var groupsFilter *regexp.Regexp
if c.ClaimMutations.FilterGroupClaims.GroupsFilter != "" {
groupsFilter, err = regexp.Compile(c.ClaimMutations.FilterGroupClaims.GroupsFilter)
if err != nil {
logger.Warnf("ignoring invalid regex `%s`", c.ClaimMutations.FilterGroupClaims.GroupsFilter)
}
}

clientID := c.ClientID
return &oidcConnector{
provider: provider,
Expand Down Expand Up @@ -214,6 +230,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
emailKey: c.ClaimMapping.EmailKey,
groupsKey: c.ClaimMapping.GroupsKey,
newGroupFromClaims: c.ClaimMutations.NewGroupFromClaims,
groupsFilter: groupsFilter,
}, nil
}

Expand Down Expand Up @@ -243,6 +260,7 @@ type oidcConnector struct {
emailKey string
groupsKey string
newGroupFromClaims []NewGroupFromClaims
groupsFilter *regexp.Regexp
}

func (c *oidcConnector) Close() error {
Expand Down Expand Up @@ -446,6 +464,9 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I
if found {
for _, v := range vs {
if s, ok := v.(string); ok {
if c.groupsFilter != nil && !c.groupsFilter.MatchString(s) {
continue
}
groups = append(groups, s)
} else {
return identity, fmt.Errorf("malformed \"%v\" claim", groupsKey)
Expand Down
19 changes: 19 additions & 0 deletions connector/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestHandleCallback(t *testing.T) {
expectPreferredUsername string
expectedEmailField string
token map[string]interface{}
groupsRegex string
newGroupFromClaims []NewGroupFromClaims
}{
{
Expand Down Expand Up @@ -362,6 +363,23 @@ func TestHandleCallback(t *testing.T) {
"non-string-claim2": 666,
},
},
{
name: "filterGroupClaims",
userIDKey: "", // not configured
userNameKey: "", // not configured
groupsRegex: `^.*\d$`,
expectUserID: "subvalue",
expectUserName: "namevalue",
expectGroups: []string{"group1", "group2"},
expectedEmailField: "emailvalue",
token: map[string]interface{}{
"sub": "subvalue",
"name": "namevalue",
"groups": []string{"group1", "group2", "groupA", "groupB"},
"email": "emailvalue",
"email_verified": true,
},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -398,6 +416,7 @@ func TestHandleCallback(t *testing.T) {
config.ClaimMapping.EmailKey = tc.emailKey
config.ClaimMapping.GroupsKey = tc.groupsKey
config.ClaimMutations.NewGroupFromClaims = tc.newGroupFromClaims
config.ClaimMutations.FilterGroupClaims.GroupsFilter = tc.groupsRegex

conn, err := newConnector(config)
if err != nil {
Expand Down

0 comments on commit 5df1605

Please sign in to comment.