Skip to content

Commit

Permalink
Map to user by username instead of opaque ID
Browse files Browse the repository at this point in the history
Fix return error
  • Loading branch information
Antoon Prins committed Nov 15, 2021
1 parent 55367df commit 47b9a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/oidc-mapping/users-oidcmapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
"oidc_issuer": "http://iam-login-service:8080/",
"oidc_group": "Analysis",
"opaque_id": "4c510ada-c86b-4815-8820-42cdf82c3d51"
"username": "einstein"
},
{
"oidc_issuer": "http://iam-login-service:8080/",
"oidc_group": "Sciencemesh",
"opaque_id": "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c"
"username": "marie"
}
]
29 changes: 14 additions & 15 deletions pkg/auth/manager/oidcmapping/oidcmapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type config struct {
type oidcUserMapping struct {
OIDCIssuer string `mapstructure:"oidc_issuer" json:"oidc_issuer"`
OIDCGroup string `mapstructure:"oidc_group" json:"oidc_group"`
OpaqueID string `mapstructure:"opaque_id" json:"opaque_id"`
Username string `mapstructure:"username" json:"username"`
}

func (c *config) init() {
Expand Down Expand Up @@ -167,9 +167,8 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)
return nil, nil, fmt.Errorf("oidcmapping: no \"groups\" attribute found in userinfo")
}

// discover the user opaqueID
var opaqueID string

// discover the user username
var username string
mappings := make([]string, 0, len(am.oidcUsersMapping))
for _, v := range am.oidcUsersMapping {
if v.OIDCIssuer == claims["issuer"] {
Expand All @@ -183,12 +182,11 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)
}
if len(intersection.([]interface{})) == 1 {
for _, m := range intersection.([]interface{}) {
opaqueID = am.oidcUsersMapping[m.(string)].OpaqueID
username = am.oidcUsersMapping[m.(string)].Username
}
}
if opaqueID == "" {
// no mappings found
return nil, nil, errors.Wrap(err, "oidcmapping: unable to retrieve local user from claims")
if username == "" {
return nil, nil, errors.New("oidcmapping: unable to retrieve username from mappings")
}

var uid, gid float64
Expand All @@ -200,7 +198,7 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)
}

userID := &user.UserId{
OpaqueId: opaqueID,
OpaqueId: username,
Idp: "",
Type: user.UserType_USER_TYPE_PRIMARY,
}
Expand All @@ -209,15 +207,16 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)
return nil, nil, errors.Wrap(err, "oidcmapping: error getting gateway grpc client")
}

getUserResp, err := gwc.GetUser(ctx, &user.GetUserRequest{
UserId: &user.UserId{OpaqueId: opaqueID},
getUserByClaimResp, err := gwc.GetUserByClaim(ctx, &user.GetUserByClaimRequest{
Claim: "username",
Value: username,
})
if err != nil {
return nil, nil, errors.Wrap(err, "oidcmapping: error getting user")
}

userID.Idp = getUserResp.GetUser().GetId().Idp
userID.Type = getUserResp.GetUser().GetId().Type
userID.Idp = getUserByClaimResp.GetUser().GetId().Idp
userID.Type = getUserByClaimResp.GetUser().GetId().Type

getGroupsResp, err := gwc.GetUserGroups(ctx, &user.GetUserGroupsRequest{
UserId: userID,
Expand All @@ -231,8 +230,8 @@ func (am *mgr) Authenticate(ctx context.Context, clientID, clientSecret string)

u := &user.User{
Id: userID,
Username: getUserResp.GetUser().GetUsername(),
Groups: getUserResp.GetUser().GetGroups(),
Username: getUserByClaimResp.GetUser().GetUsername(),
Groups: getUserByClaimResp.GetUser().GetGroups(),
Mail: claims["email"].(string),
MailVerified: claims["email_verified"].(bool),
DisplayName: claims["name"].(string),
Expand Down

0 comments on commit 47b9a87

Please sign in to comment.