-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support username, email and groups claim in OIDC connector #1634
Merged
sagikazarmark
merged 9 commits into
dexidp:master
from
concourse:pr/oidc-username-key-sync
Sep 8, 2020
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a4e0fc
Make OIDC username key configurable
d9afb7e
default to preferred_username claim
4812079
add tests when preferred username key is not set
52c39fb
check if upstream contains preferrend username claim first
61312e7
Add parameter configuration to override email claim key
cyrilix a783667
Add groupsClaimMapping to the OIDC connector
Lemmons 41207ba
Combine #1691 and #1776 to unify OIDC provider claim mapping
0494993
update oidc documentation and email claim err msg
058202d
revert changes for user id and user name
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,14 +49,31 @@ type Config struct { | |
// id tokens | ||
GetUserInfo bool `json:"getUserInfo"` | ||
|
||
// Configurable key which contains the user id claim | ||
// Deprecated: use UserIDKey in claimMapping instead | ||
UserIDKey string `json:"userIDKey"` | ||
|
||
// Configurable key which contains the user name claim | ||
// Deprecated: use UserNameKey in claimMapping instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These shouldn't be deprecated. |
||
UserNameKey string `json:"userNameKey"` | ||
|
||
// PromptType will be used fot the prompt parameter (when offline_access, by default prompt=consent) | ||
PromptType string `json:"promptType"` | ||
|
||
ClaimMapping struct { | ||
// Configurable key which contains the user id claim | ||
UserIDKey string `json:"user_id"` // defaults to "sub" | ||
|
||
// Configurable key which contains the username claim | ||
UserNameKey string `json:"user_name"` // defaults to "name" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess these shouldn't be here either. |
||
|
||
// Configurable key which contains the preferred username claims | ||
PreferredUsernameKey string `json:"preferred_username"` // defaults to "preferred_username" | ||
|
||
// Configurable key which contains the email claims | ||
EmailKey string `json:"email"` // defaults to "email" | ||
|
||
// Configurable key which contains the groups claims | ||
GroupsKey string `json:"groups"` // defaults to "groups" | ||
} `json:"claimMapping"` | ||
} | ||
|
||
// Domains that don't support basic auth. golang.org/x/oauth2 has an internal | ||
|
@@ -121,6 +138,18 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e | |
c.PromptType = "consent" | ||
} | ||
|
||
// Backward compatibility | ||
userIDKey := c.ClaimMapping.UserIDKey | ||
if userIDKey == "" { | ||
userIDKey = c.UserIDKey | ||
} | ||
|
||
// Backward compatibility | ||
userNameKey := c.ClaimMapping.UserNameKey | ||
if userNameKey == "" { | ||
userNameKey = c.UserNameKey | ||
} | ||
|
||
clientID := c.ClientID | ||
return &oidcConnector{ | ||
provider: provider, | ||
|
@@ -141,9 +170,12 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e | |
insecureSkipEmailVerified: c.InsecureSkipEmailVerified, | ||
insecureEnableGroups: c.InsecureEnableGroups, | ||
getUserInfo: c.GetUserInfo, | ||
userIDKey: c.UserIDKey, | ||
userNameKey: c.UserNameKey, | ||
promptType: c.PromptType, | ||
userIDKey: userIDKey, | ||
userNameKey: userNameKey, | ||
preferredUsernameKey: c.ClaimMapping.PreferredUsernameKey, | ||
emailKey: c.ClaimMapping.EmailKey, | ||
groupsKey: c.ClaimMapping.GroupsKey, | ||
}, nil | ||
} | ||
|
||
|
@@ -163,9 +195,12 @@ type oidcConnector struct { | |
insecureSkipEmailVerified bool | ||
insecureEnableGroups bool | ||
getUserInfo bool | ||
promptType string | ||
userIDKey string | ||
userNameKey string | ||
promptType string | ||
preferredUsernameKey string | ||
emailKey string | ||
groupsKey string | ||
} | ||
|
||
func (c *oidcConnector) Close() error { | ||
|
@@ -273,6 +308,11 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I | |
return identity, fmt.Errorf("missing \"%s\" claim", userNameKey) | ||
} | ||
|
||
preferredUsername, found := claims["preferred_username"].(string) | ||
if !found { | ||
preferredUsername, _ = claims[c.preferredUsernameKey].(string) | ||
} | ||
|
||
hasEmailScope := false | ||
for _, s := range c.oauth2Config.Scopes { | ||
if s == "email" { | ||
|
@@ -281,9 +321,16 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I | |
} | ||
} | ||
|
||
email, found := claims["email"].(string) | ||
var email string | ||
emailKey := "email" | ||
email, found = claims[emailKey].(string) | ||
if !found && c.emailKey != "" { | ||
emailKey = c.emailKey | ||
email, found = claims[emailKey].(string) | ||
} | ||
|
||
if !found && hasEmailScope { | ||
return identity, errors.New("missing \"email\" claim") | ||
return identity, fmt.Errorf("missing email claim, not found \"%s\" key", emailKey) | ||
} | ||
|
||
emailVerified, found := claims["email_verified"].(bool) | ||
|
@@ -294,8 +341,28 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I | |
return identity, errors.New("missing \"email_verified\" claim") | ||
} | ||
} | ||
hostedDomain, _ := claims["hd"].(string) | ||
|
||
var groups []string | ||
if c.insecureEnableGroups { | ||
groupsKey := "groups" | ||
vs, found := claims[groupsKey].([]interface{}) | ||
if !found { | ||
groupsKey = c.groupsKey | ||
vs, found = claims[groupsKey].([]interface{}) | ||
} | ||
|
||
if found { | ||
for _, v := range vs { | ||
if s, ok := v.(string); ok { | ||
groups = append(groups, s) | ||
} else { | ||
return identity, fmt.Errorf("malformed \"%v\" claim", groupsKey) | ||
} | ||
} | ||
} | ||
} | ||
|
||
hostedDomain, _ := claims["hd"].(string) | ||
if len(c.hostedDomains) > 0 { | ||
found := false | ||
for _, domain := range c.hostedDomains { | ||
|
@@ -320,11 +387,13 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I | |
} | ||
|
||
identity = connector.Identity{ | ||
UserID: idToken.Subject, | ||
Username: name, | ||
Email: email, | ||
EmailVerified: emailVerified, | ||
ConnectorData: connData, | ||
UserID: idToken.Subject, | ||
Username: name, | ||
PreferredUsername: preferredUsername, | ||
Email: email, | ||
EmailVerified: emailVerified, | ||
Groups: groups, | ||
ConnectorData: connData, | ||
} | ||
|
||
if c.userIDKey != "" { | ||
|
@@ -335,18 +404,5 @@ func (c *oidcConnector) createIdentity(ctx context.Context, identity connector.I | |
identity.UserID = userID | ||
} | ||
|
||
if c.insecureEnableGroups { | ||
vs, ok := claims["groups"].([]interface{}) | ||
if ok { | ||
for _, v := range vs { | ||
if s, ok := v.(string); ok { | ||
identity.Groups = append(identity.Groups, s) | ||
} else { | ||
return identity, errors.New("malformed \"groups\" claim") | ||
} | ||
} | ||
} | ||
} | ||
|
||
return identity, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be added back as these are valid options, but not claims.