Skip to content

Commit 54578a5

Browse files
author
Rui Yang
committed
cleanup and optimization
Signed-off-by: Rui Yang <[email protected]>
1 parent f153234 commit 54578a5

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

connector/oauth/oauth.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,14 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
191191
if err != nil {
192192
return identity, fmt.Errorf("OAuth Connector: failed to execute request to userinfo: %v", err)
193193
}
194+
defer userInfoResp.Body.Close()
194195

195196
if userInfoResp.StatusCode != http.StatusOK {
196197
return identity, fmt.Errorf("OAuth Connector: failed to execute request to userinfo: status %d", userInfoResp.StatusCode)
197198
}
198199

199-
defer userInfoResp.Body.Close()
200-
201200
var userInfoResult map[string]interface{}
202201
err = json.NewDecoder(userInfoResp.Body).Decode(&userInfoResult)
203-
204202
if err != nil {
205203
return identity, fmt.Errorf("OAuth Connector: failed to parse userinfo: %v", err)
206204
}
@@ -217,7 +215,7 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
217215
identity.EmailVerified, _ = userInfoResult[c.emailVerifiedKey].(bool)
218216

219217
if s.Groups {
220-
groups := map[string]bool{}
218+
groups := map[string]struct{}{}
221219

222220
c.addGroupsFromMap(groups, userInfoResult)
223221
c.addGroupsFromToken(groups, token.AccessToken)
@@ -239,22 +237,22 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
239237
return identity, nil
240238
}
241239

242-
func (c *oauthConnector) addGroupsFromMap(groups map[string]bool, result map[string]interface{}) error {
240+
func (c *oauthConnector) addGroupsFromMap(groups map[string]struct{}, result map[string]interface{}) error {
243241
groupsClaim, ok := result[c.groupsKey].([]interface{})
244242
if !ok {
245243
return errors.New("cannot convert to slice")
246244
}
247245

248246
for _, group := range groupsClaim {
249247
if groupString, ok := group.(string); ok {
250-
groups[groupString] = true
248+
groups[groupString] = struct{}{}
251249
}
252250
}
253251

254252
return nil
255253
}
256254

257-
func (c *oauthConnector) addGroupsFromToken(groups map[string]bool, token string) error {
255+
func (c *oauthConnector) addGroupsFromToken(groups map[string]struct{}, token string) error {
258256
parts := strings.Split(token, ".")
259257
if len(parts) < 2 {
260258
return errors.New("invalid token")

0 commit comments

Comments
 (0)