diff --git a/connector/oauth/oauth.go b/connector/oauth/oauth.go index e37932ad..237d075e 100644 --- a/connector/oauth/oauth.go +++ b/connector/oauth/oauth.go @@ -209,12 +209,18 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id return identity, fmt.Errorf("OAuth Connector: failed to parse userinfo: %v", err) } - userID, found := userInfoResult[c.userIDKey].(string) + userID, found := userInfoResult[c.userIDKey] if !found { return identity, fmt.Errorf("OAuth Connector: not found %v claim", c.userIDKey) } - identity.UserID = userID + switch userID.(type) { + case float64, int64, string: + identity.UserID = fmt.Sprintf("%v", userID) + default: + return identity, fmt.Errorf("OAuth Connector: %v claim should be string or number, got %T", c.userIDKey, userID) + } + identity.Username, _ = userInfoResult[c.userNameKey].(string) identity.PreferredUsername, _ = userInfoResult[c.preferredUsernameKey].(string) identity.Email, _ = userInfoResult[c.emailKey].(string) diff --git a/connector/oauth/oauth_test.go b/connector/oauth/oauth_test.go index 082a3aa5..91284568 100644 --- a/connector/oauth/oauth_test.go +++ b/connector/oauth/oauth_test.go @@ -168,6 +168,34 @@ func TestHandleCallBackForGroupsInToken(t *testing.T) { assert.Equal(t, identity.EmailVerified, false) } +func TestHandleCallbackForNumericUserID(t *testing.T) { + tokenClaims := map[string]interface{}{} + + userInfoClaims := map[string]interface{}{ + "name": "test-name", + "user_id_key": 1000, + "user_name_key": "test-username", + "preferred_username": "test-preferred-username", + "mail": "mod_mail", + "has_verified_email": false, + } + + testServer := testSetup(t, tokenClaims, userInfoClaims) + defer testServer.Close() + + conn := newConnector(t, testServer.URL) + req := newRequestWithAuthCode(t, testServer.URL, "some-code") + + identity, err := conn.HandleCallback(connector.Scopes{Groups: true}, req) + assert.Equal(t, err, nil) + + assert.Equal(t, identity.UserID, "1000") + assert.Equal(t, identity.Username, "test-username") + assert.Equal(t, identity.PreferredUsername, "test-preferred-username") + assert.Equal(t, identity.Email, "mod_mail") + assert.Equal(t, identity.EmailVerified, false) +} + func testSetup(t *testing.T, tokenClaims map[string]interface{}, userInfoClaims map[string]interface{}) *httptest.Server { key, err := rsa.GenerateKey(rand.Reader, 1024) if err != nil {