Skip to content

Commit 8f3533a

Browse files
authored
fix: get groups claim from idtokenclaims (#3101)
* fix: get groups claim from idtokenclaims Signed-off-by: Philipp Lange <ph.lange@pm.me> * fix: lint Signed-off-by: Philipp Lange <ph.lange@pm.me> --------- Signed-off-by: Philipp Lange <ph.lange@pm.me>
1 parent 0e2aa81 commit 8f3533a

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

pkg/api/routes.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/url"
1818
"path"
1919
"regexp"
20+
"slices"
2021
"sort"
2122
"strconv"
2223
"strings"
@@ -1992,13 +1993,25 @@ func (rh *RouteHandler) OpenIDCodeExchangeCallback() rp.CodeExchangeUserinfoCall
19921993

19931994
val, ok := info.Claims["groups"].([]interface{})
19941995
if !ok {
1995-
rh.c.Log.Info().Msgf("failed to find any 'groups' claim for user %s", email)
1996+
rh.c.Log.Info().Msgf("failed to find any 'groups' claim for user %s in UserInfo", email)
19961997
}
19971998

19981999
for _, group := range val {
19992000
groups = append(groups, fmt.Sprint(group))
20002001
}
20012002

2003+
val, ok = tokens.IDTokenClaims.Claims["groups"].([]interface{})
2004+
if !ok {
2005+
rh.c.Log.Info().Msgf("failed to find any 'groups' claim for user %s in IDTokenClaimsToken", email)
2006+
}
2007+
2008+
for _, group := range val {
2009+
groups = append(groups, fmt.Sprint(group))
2010+
}
2011+
2012+
slices.Sort(groups)
2013+
groups = slices.Compact(groups)
2014+
20022015
callbackUI, err := OAuth2Callback(rh.c, w, r, state, email, groups)
20032016
if err != nil {
20042017
if errors.Is(err, zerr.ErrInvalidStateCookie) {

pkg/api/routes_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ func TestRoutes(t *testing.T) {
117117
So(resp.StatusCode, ShouldEqual, http.StatusUnauthorized)
118118
})
119119

120+
Convey("Test OpenIDCodeExchangeCallback", func() {
121+
callback := rthdlr.OpenIDCodeExchangeCallback()
122+
ctx := context.TODO()
123+
124+
request, _ := http.NewRequestWithContext(ctx, http.MethodGet, baseURL, nil)
125+
response := httptest.NewRecorder()
126+
127+
tokens := &oidc.Tokens[*oidc.IDTokenClaims]{
128+
IDTokenClaims: &oidc.IDTokenClaims{
129+
Claims: map[string]any{
130+
"groups": []interface{}{"group1", "group3"},
131+
},
132+
},
133+
}
134+
relyingParty, err := rp.NewRelyingPartyOAuth(&oauth2.Config{})
135+
So(err, ShouldBeNil)
136+
137+
userinfo := &oidc.UserInfo{
138+
Subject: "sub",
139+
Claims: map[string]any{
140+
"email": "test@test.com",
141+
"groups": []interface{}{"group1", "group2"},
142+
},
143+
UserInfoEmail: oidc.UserInfoEmail{Email: "test@test.com"},
144+
}
145+
146+
callback(response, request, tokens, "state", relyingParty, userinfo)
147+
148+
resp := response.Result()
149+
defer resp.Body.Close()
150+
So(resp, ShouldNotBeNil)
151+
So(resp.StatusCode, ShouldEqual, http.StatusUnauthorized)
152+
})
153+
120154
Convey("Test OAuth2Callback errors", func() {
121155
ctx := context.TODO()
122156

0 commit comments

Comments
 (0)