Skip to content

Commit

Permalink
fix(cli): improve display of error responses for token exchange (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Sep 2, 2024
1 parent 579f144 commit 62def36
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
5 changes: 2 additions & 3 deletions cli/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c Configurator) Start(ctx context.Context, prev *Config, flags agentConfig

_, err = c.handleOAuth(ctx, cfg, prev)
if err != nil {
c.logger.Error("Could not handle OAuth", zap.Error(err))
c.logger.Debug("Could not handle OAuth", zap.Error(err))
return err
}

Expand Down Expand Up @@ -265,7 +265,7 @@ func (c Configurator) handleOAuth(ctx context.Context, cfg Config, prev *Config)
var err error
cfg, err = c.exchangeToken(cfg, c.flags.Token)
if err != nil {
c.logger.Error("Could not exchange token", zap.Error(err))
c.logger.Debug("could not exchange token", zap.Error(err))
return Config{}, err
}
}
Expand Down Expand Up @@ -304,7 +304,6 @@ func (c Configurator) exchangeToken(cfg Config, token string) (Config, error) {
c.logger.Debug("Exchanging token", zap.String("token", token))
jwt, err := oauth.ExchangeToken(cfg.OAuthEndpoint(), token)
if err != nil {
c.logger.Error("Could not exchange token", zap.Error(err))
return Config{}, err
}

Expand Down
33 changes: 27 additions & 6 deletions cli/pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func SetLogger(l *zap.Logger) {
logger = l
}

type oauthError struct {
err error
msg string
}

func (e oauthError) Error() string {
return e.err.Error()
}

func (e oauthError) Message() string {
return e.msg
}

func ExchangeToken(endpoint string, token string) (string, error) {
logger.Debug("Exchanging token", zap.String("endpoint", endpoint), zap.String("token", token))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/tokens/%s/exchange", endpoint, token), nil)
Expand All @@ -95,16 +108,24 @@ func ExchangeToken(endpoint string, token string) (string, error) {

res, err := http.DefaultClient.Do(req)
if err != nil {
logger.Debug("Failed to exchange token", zap.Error(err))
return "", fmt.Errorf("failed to exchange token: %w", err)
logger.Debug("Cannot create exchange token request", zap.Error(err))
return "", fmt.Errorf("cannot create exchange token request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusCreated {
logger.Debug("Failed to exchange token", zap.String("status", res.Status))
return "", fmt.Errorf("failed to exchange token: %s", res.Status)
switch res.StatusCode {
case http.StatusNotFound:
return "", oauthError{err: fmt.Errorf("token not found"), msg: "Token not found"}
case http.StatusUnauthorized:
return "", oauthError{err: fmt.Errorf("token expired"), msg: "Token has expired"}
case http.StatusCreated:
logger.Debug("Token exchanged")
default:
b, _ := io.ReadAll(res.Body)
logger.Debug("Failed to exchange token", zap.String("status", res.Status), zap.String("response", string(b)))
return "", oauthError{err: fmt.Errorf("failed to exchange token: %s", res.Status), msg: "Unexpected error exchanging token"}
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
logger.Debug("Failed to read response body", zap.Error(err))
Expand Down

0 comments on commit 62def36

Please sign in to comment.