Skip to content

Commit

Permalink
cleanup and optimization
Browse files Browse the repository at this point in the history
Signed-off-by: Rui Yang <ruiya@vmware.com>
  • Loading branch information
Rui Yang committed May 10, 2021
1 parent f153234 commit 54578a5
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions connector/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,14 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
if err != nil {
return identity, fmt.Errorf("OAuth Connector: failed to execute request to userinfo: %v", err)
}
defer userInfoResp.Body.Close()

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

defer userInfoResp.Body.Close()

var userInfoResult map[string]interface{}
err = json.NewDecoder(userInfoResp.Body).Decode(&userInfoResult)

if err != nil {
return identity, fmt.Errorf("OAuth Connector: failed to parse userinfo: %v", err)
}
Expand All @@ -217,7 +215,7 @@ func (c *oauthConnector) HandleCallback(s connector.Scopes, r *http.Request) (id
identity.EmailVerified, _ = userInfoResult[c.emailVerifiedKey].(bool)

if s.Groups {
groups := map[string]bool{}
groups := map[string]struct{}{}

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

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

for _, group := range groupsClaim {
if groupString, ok := group.(string); ok {
groups[groupString] = true
groups[groupString] = struct{}{}
}
}

return nil
}

func (c *oauthConnector) addGroupsFromToken(groups map[string]bool, token string) error {
func (c *oauthConnector) addGroupsFromToken(groups map[string]struct{}, token string) error {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return errors.New("invalid token")
Expand Down

0 comments on commit 54578a5

Please sign in to comment.