Skip to content

Commit

Permalink
feat: Improve logging in the oauth2 callback handler (#11370)
Browse files Browse the repository at this point in the history
  • Loading branch information
umi0410 committed Jul 16, 2023
1 parent 97b6fa8 commit 22d4e17
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions server/auth/sso/sso.go
Expand Up @@ -230,6 +230,7 @@ func (s *sso) HandleCallback(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie(state)
http.SetCookie(w, &http.Cookie{Name: state, MaxAge: 0})
if err != nil {
log.WithError(err).Error("failed to get cookie")
w.WriteHeader(400)
return
}
Expand All @@ -238,25 +239,28 @@ func (s *sso) HandleCallback(w http.ResponseWriter, r *http.Request) {
oauth2Context := context.WithValue(ctx, oauth2.HTTPClient, s.httpClient)
oauth2Token, err := s.config.Exchange(oauth2Context, r.URL.Query().Get("code"), redirectOption)
if err != nil {
log.WithError(err).Error("failed to get oauth2Token by using code from the oauth2 server")
w.WriteHeader(401)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
log.Error("failed to extract id_token from the response")
w.WriteHeader(401)
return
}
idToken, err := s.idTokenVerifier.Verify(ctx, rawIDToken)
if err != nil {
log.WithError(err).Error("failed to verify the id token issued")
w.WriteHeader(401)
return
}
c := &types.Claims{}
if err := idToken.Claims(c); err != nil {
log.WithError(err).Error("failed to get claims from the id token")
w.WriteHeader(401)
return
}

// Default to groups claim but if customClaimName is set
// extract groups based on that claim key
groups := c.Groups
Expand All @@ -266,17 +270,16 @@ func (s *sso) HandleCallback(w http.ResponseWriter, r *http.Request) {
log.Warn(err)
}
}

// Some SSO implementations (Okta) require a call to
// the OIDC user info path to get attributes like groups
if s.userInfoPath != "" {
groups, err = c.GetUserInfoGroups(oauth2Token.AccessToken, s.issuer, s.userInfoPath)
if err != nil {
log.WithError(err).Errorf("failed to get groups claim from the given userInfoPath(%s)", s.userInfoPath)
w.WriteHeader(401)
return
}
}

argoClaims := &types.Claims{
Claims: jwt.Claims{
Issuer: issuer,
Expand All @@ -291,9 +294,9 @@ func (s *sso) HandleCallback(w http.ResponseWriter, r *http.Request) {
PreferredUsername: c.PreferredUsername,
ServiceAccountNamespace: c.ServiceAccountNamespace,
}

raw, err := jwt.Encrypted(s.encrypter).Claims(argoClaims).CompactSerialize()
if err != nil {
log.WithError(err).Errorf("failed to encrypt and serialize the jwt token")
w.WriteHeader(401)
return
}
Expand Down

0 comments on commit 22d4e17

Please sign in to comment.