Skip to content

Commit b68d3fe

Browse files
committed
[4384] Fix the groups coming as JSON string
1 parent 9207486 commit b68d3fe

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

connector/oauth/oauth.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"log"
910
"log/slog"
1011
"net/http"
1112
"strings"
@@ -170,6 +171,8 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
170171
if err != nil {
171172
return identity, fmt.Errorf("OAuth Connector: failed to parse userinfo: %v", err)
172173
}
174+
fmt.Printf("sks here: %+v\n", userInfoResult)
175+
fmt.Printf("group key: %s\n", c.groupsKey)
173176

174177
userID, found := userInfoResult[c.userIDKey]
175178
if !found {
@@ -191,8 +194,12 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
191194
if s.Groups {
192195
groups := map[string]struct{}{}
193196

194-
c.addGroupsFromMap(groups, userInfoResult)
195-
c.addGroupsFromToken(groups, token.AccessToken)
197+
if err := c.addGroupsFromMap(groups, userInfoResult); err != nil {
198+
log.Printf("OAuth Connector: failed to add groups from map: %v", err)
199+
}
200+
if err := c.addGroupsFromToken(groups, token.AccessToken); err != nil {
201+
log.Printf("OAuth Connector: failed to add groups from token: %v", err)
202+
}
196203

197204
for groupName := range groups {
198205
identity.Groups = append(identity.Groups, groupName)
@@ -214,7 +221,15 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
214221
func (c *oauthConnector) addGroupsFromMap(groups map[string]struct{}, result map[string]interface{}) error {
215222
groupsClaim, ok := result[c.groupsKey].([]interface{})
216223
if !ok {
217-
return errors.New("cannot convert to slice")
224+
// sometimes the groups claim is a slice encoded as a JSON string
225+
groupsStr, ok := result[c.groupsKey].(string)
226+
if !ok {
227+
return fmt.Errorf("%T claim is not a list or JSON-encoded list", result[c.groupsKey])
228+
}
229+
err := json.Unmarshal([]byte(groupsStr), &groupsClaim)
230+
if err != nil {
231+
return fmt.Errorf("failed to decode groups claim: %v", err)
232+
}
218233
}
219234

220235
for _, group := range groupsClaim {

connector/oauth/oauth_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ func TestHandleCallBackForGroupsInUserInfo(t *testing.T) {
100100
assert.Equal(t, identity.EmailVerified, false)
101101
}
102102

103+
func TestHandleCallBackForGroupsInUserInfoIsString(t *testing.T) {
104+
tokenClaims := map[string]interface{}{}
105+
106+
userInfoClaims := map[string]interface{}{
107+
"name": "test-name",
108+
"user_id_key": "test-user-id",
109+
"user_name_key": "test-username",
110+
"preferred_username": "test-preferred-username",
111+
"mail": "mod_mail",
112+
"has_verified_email": false,
113+
"groups_key": `["admin-group", "user-group"]`,
114+
}
115+
116+
testServer := testSetup(t, tokenClaims, userInfoClaims)
117+
defer testServer.Close()
118+
119+
conn := newConnector(t, testServer.URL)
120+
req := newRequestWithAuthCode(t, testServer.URL, "TestHandleCallBackForGroupsInUserInfo")
121+
122+
identity, err := conn.HandleCallback(connector.Scopes{Groups: true}, req)
123+
assert.Equal(t, err, nil)
124+
125+
sort.Strings(identity.Groups)
126+
assert.Equal(t, len(identity.Groups), 2)
127+
assert.Equal(t, identity.Groups[0], "admin-group")
128+
assert.Equal(t, identity.Groups[1], "user-group")
129+
assert.Equal(t, identity.UserID, "test-user-id")
130+
assert.Equal(t, identity.Username, "test-username")
131+
assert.Equal(t, identity.PreferredUsername, "test-preferred-username")
132+
assert.Equal(t, identity.Email, "mod_mail")
133+
assert.Equal(t, identity.EmailVerified, false)
134+
}
135+
103136
func TestHandleCallBackForGroupMapsInUserInfo(t *testing.T) {
104137
tokenClaims := map[string]interface{}{}
105138

0 commit comments

Comments
 (0)