Skip to content

Commit 09e32a1

Browse files
committed
Implement TeamWhiteList functionality for OIDC providers
- Add new config option TeamWhiteListClaim that references the UserInfo parameter that contains an array of teams the user is part of - Add matching teams to the users TeamMemberships field
1 parent 012b2fd commit 09e32a1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pkg/cfg/oauth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type oauthConfig struct {
8383
PreferredDomain string `mapstructure:"preferredDomain"`
8484
AzureToken string `mapstructure:"azure_token" envconfig:"azure_token"`
8585
CodeChallengeMethod string `mapstructure:"code_challenge_method" envconfig:"code_challenge_method"`
86+
TeamWhiteListClaim string `mapstructure:"team_whitelist_claim" envconfig:"team_whitelist_claim"`
8687
}
8788

8889
type oauthClaimsConfig struct {

pkg/providers/openid/openid.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ func (Provider) Configure() {
3232
log = cfg.Logging.Logger
3333
}
3434

35+
func GenerateTeamsOfUser(customClaims *structs.CustomClaims, claimName string) map[string]bool {
36+
teamOutput := make(map[string]bool)
37+
if val, ok := customClaims.Claims[claimName]; ok {
38+
39+
customClaimsSlice := val.([]interface{})
40+
41+
for _, teamValue := range customClaimsSlice {
42+
team, isMyType := teamValue.(string)
43+
if isMyType {
44+
teamOutput[team] = true
45+
}
46+
}
47+
48+
return teamOutput
49+
}
50+
log.Debugf("Claim %s missing from UserInfo response. Make sure you include the correct scope", claimName)
51+
return teamOutput
52+
}
53+
3554
// GetUserInfo provider specific call to get userinfomation
3655
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens, opts ...oauth2.AuthCodeOption) (rerr error) {
3756
client, _, err := common.PrepareTokensAndClient(r, ptokens, true, opts...)
@@ -58,5 +77,17 @@ func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *s
5877
return err
5978
}
6079
user.PrepareUserData()
80+
81+
if len(cfg.Cfg.TeamWhiteList) != 0 && len(cfg.GenOAuth.TeamWhiteListClaim) != 0 {
82+
allTeamsOfUser := GenerateTeamsOfUser(customClaims, cfg.GenOAuth.TeamWhiteListClaim)
83+
84+
for _, whiteListedTeam := range cfg.Cfg.TeamWhiteList {
85+
if allTeamsOfUser[whiteListedTeam] {
86+
user.TeamMemberships = append(user.TeamMemberships, whiteListedTeam)
87+
}
88+
}
89+
}
90+
log.Debug("getUserInfoFromOAuth")
91+
log.Debug(user)
6192
return nil
6293
}

0 commit comments

Comments
 (0)