if username is not available in remote, fall back to username from token

Signed-off-by: Kevin Schu <kevin.schu@aoe.com>
This commit is contained in:
Kevin Schu 2021-07-08 15:36:18 +02:00
parent 29ccdff766
commit 0679f4701e
2 changed files with 33 additions and 2 deletions

9
src/pkg/oidc/helper.go Normal file → Executable file
View File

@ -289,8 +289,12 @@ func mergeUserInfo(remote, local *UserInfo) *UserInfo {
Subject: local.Subject,
Issuer: local.Issuer,
// Used data from userinfo
Username: remote.Username,
Email: remote.Email,
Email: remote.Email,
}
if remote.Username != "" {
res.Username = remote.Username
} else {
res.Username = local.Username
}
if remote.hasGroupClaim {
res.Groups = remote.Groups
@ -346,6 +350,7 @@ func userInfoFromClaims(c claimsProvider, setting cfgModels.OIDCSetting) (*UserI
if username, ok := allClaims[setting.UserClaim].(string); ok {
res.Username = username
} else {
log.Debugf("OIDC. Failed to recover Username from claims: %+v", allClaims)
log.Warningf("OIDC. Failed to recover Username from claim. Claim '%s' is invalid or not a string", setting.UserClaim)
}
}

View File

@ -379,6 +379,32 @@ func TestMergeUserInfo(t *testing.T) {
hasGroupClaim: true,
},
},
{
fromInfo: &UserInfo{
Issuer: "",
Subject: "",
Username: "",
Email: "kevin@whatever.com",
Groups: []string{},
hasGroupClaim: false,
},
fromIDToken: &UserInfo{
Issuer: "issuer-whatever",
Subject: "subject-kevin",
Username: "kevin",
Email: "kevin@whatever.com",
Groups: []string{"g1", "g2"},
hasGroupClaim: true,
},
expected: &UserInfo{
Issuer: "issuer-whatever",
Subject: "subject-kevin",
Username: "kevin",
Email: "kevin@whatever.com",
Groups: []string{"g1", "g2"},
hasGroupClaim: true,
},
},
}
for _, tc := range s {