Skip to content

Commit 89525cd

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 c220a5e commit 89525cd

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
@@ -85,6 +85,7 @@ type oauthConfig struct {
8585
PreferredDomain string `mapstructure:"preferredDomain"`
8686
AzureToken string `mapstructure:"azure_token" envconfig:"azure_token"`
8787
CodeChallengeMethod string `mapstructure:"code_challenge_method" envconfig:"code_challenge_method"`
88+
TeamWhiteListClaim string `mapstructure:"team_whitelist_claim" envconfig:"team_whitelist_claim"`
8889
// DiscordUseIDs defaults to false, maintaining the more common username checking behavior
8990
// If set to true, match the Discord user's ID instead of their username
9091
DiscordUseIDs bool `mapstructure:"discord_use_ids" envconfig:"discord_use_ids"`

pkg/providers/openid/openid.go

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

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

0 commit comments

Comments
 (0)