Skip to content

Commit

Permalink
OIDC: fallback to "email" if IDP doesn't provide "preferred_username"…
Browse files Browse the repository at this point in the history
… claim

Some IDPs (e.g. Authelia) don't support the "preferred_username"
claim. Fallback to the "email" claim in that case.
  • Loading branch information
rhafer committed Nov 30, 2021
1 parent 79fb299 commit dcc4463
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/user-claim-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: OIDC: fallback to "email" if IDP doesn't provide "preferred_username" claim

https://github.com/cs3org/reva/pull/xxxx
14 changes: 11 additions & 3 deletions pkg/auth/manager/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)
return nil, nil, fmt.Errorf("no \"email\" attribute found in userinfo: maybe the client did not request the oidc \"email\"-scope")
}

if claims["preferred_username"] == nil || claims["name"] == nil {
return nil, nil, fmt.Errorf("no \"preferred_username\" or \"name\" attribute found in userinfo: maybe the client did not request the oidc \"profile\"-scope")
user_claim := "preferred_username"
if claims["preferred_username"] == nil {
if claims["email"] != nil {
user_claim = "email"
} else {
return nil, nil, fmt.Errorf("no \"preferred_username\" and \"email\" attribute found in userinfo: maybe the client did not request the oidc \"profile\"-scope")
}
}
if claims["name"] == nil {
return nil, nil, fmt.Errorf("no \"name\" attribute found in userinfo: maybe the client did not request the oidc \"profile\"-scope")
}

var uid, gid float64
Expand Down Expand Up @@ -168,7 +176,7 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)

u := &user.User{
Id: userID,
Username: claims["preferred_username"].(string),
Username: claims[user_claim].(string),
// TODO(labkode) if we can get groups from the claim we need to give the possibility
// to the admin to choose what claim provides the groups.
// TODO(labkode) ... use all claims from oidc?
Expand Down

0 comments on commit dcc4463

Please sign in to comment.