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 Aug 4, 2023
1 parent b78bf1f commit 8838f28
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 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 @@ -87,6 +88,9 @@ type Config struct {
// Configurable key which contains the groups claims
GroupsKey string `json:"groups"` // defaults to "groups"
} `json:"claimMapping"`

// Regex filter applied to the groups
GroupsRegex string `json:"groupsRegex"`
}

// Domains that don't support basic auth. golang.org/x/oauth2 has an internal
Expand Down Expand Up @@ -161,6 +165,14 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
c.PromptType = "consent"
}

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

clientID := c.ClientID
return &oidcConnector{
provider: provider,
Expand Down Expand Up @@ -189,6 +201,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
preferredUsernameKey: c.ClaimMapping.PreferredUsernameKey,
emailKey: c.ClaimMapping.EmailKey,
groupsKey: c.ClaimMapping.GroupsKey,
groupsRegex: groupsRegex,
}, nil
}

Expand Down Expand Up @@ -216,6 +229,7 @@ type oidcConnector struct {
preferredUsernameKey string
emailKey string
groupsKey string
groupsRegex *regexp.Regexp
}

func (c *oidcConnector) Close() error {
Expand Down Expand Up @@ -419,6 +433,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.groupsRegex != nil && !c.groupsRegex.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
}{
{
name: "simpleCase",
Expand Down Expand Up @@ -288,6 +289,23 @@ func TestHandleCallback(t *testing.T) {
"email_verified": true,
},
},
{
name: "groupsRegex",
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 @@ -319,6 +337,7 @@ func TestHandleCallback(t *testing.T) {
InsecureEnableGroups: true,
BasicAuthUnsupported: &basicAuth,
OverrideClaimMapping: tc.overrideClaimMapping,
GroupsRegex: tc.groupsRegex,
}
config.ClaimMapping.PreferredUsernameKey = tc.preferredUsernameKey
config.ClaimMapping.EmailKey = tc.emailKey
Expand Down

0 comments on commit 8838f28

Please sign in to comment.