Skip to content

Commit

Permalink
feat: improve refresh token error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Sep 7, 2022
1 parent cdffa1e commit 2769c9b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions oauth2/hook.go
Expand Up @@ -56,7 +56,7 @@ type RefreshTokenHookRequest struct {
// swagger:model refreshTokenHookResponse
type RefreshTokenHookResponse struct {
// Session is the session data returned by the hook.
Session consent.ConsentRequestSessionData `json:"session"`
Session consent.AcceptOAuth2ConsentRequestSession `json:"session"`
}

// RefreshTokenHook is an AccessRequestHook called for `refresh_token` grant type.
Expand Down Expand Up @@ -99,7 +99,8 @@ func RefreshTokenHook(reg interface {
return errorsx.WithStack(
fosite.ErrServerError.
WithWrap(err).
WithDebug("refresh token hook: marshal request body"),
WithDescription("An error occurred while encoding the refresh token hook.").
WithDebugf("Unable to encode the refresh token hook body: %s", err),
)
}

Expand All @@ -108,7 +109,8 @@ func RefreshTokenHook(reg interface {
return errorsx.WithStack(
fosite.ErrServerError.
WithWrap(err).
WithDebug("refresh token hook: new http request"),
WithDescription("An error occurred while preparing the refresh token hook.").
WithDebugf("Unable to prepare the HTTP Request: %s", err),
)
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
Expand All @@ -118,7 +120,8 @@ func RefreshTokenHook(reg interface {
return errorsx.WithStack(
fosite.ErrServerError.
WithWrap(err).
WithDebug("refresh token hook: do http request"),
WithDescription("An error occurred while executing the refresh token hook.").
WithDebugf("Unable to execute HTTP Request: %s", err),
)
}
defer resp.Body.Close()
Expand All @@ -132,12 +135,14 @@ func RefreshTokenHook(reg interface {
case http.StatusForbidden:
return errorsx.WithStack(
fosite.ErrAccessDenied.
WithDebugf("refresh token hook: %s", resp.Status),
WithDescription("The refresh token hook target responded with an error.").
WithDebugf("Refresh token hook responded with HTTP status code: %s", resp.Status),
)
default:
return errorsx.WithStack(
fosite.ErrServerError.
WithDebugf("refresh token hook: %s", resp.Status),
WithDescription("The refresh token hook target responded with an error.").
WithDebugf("Refresh token hook responded with HTTP status code: %s", resp.Status),
)
}

Expand All @@ -146,15 +151,15 @@ func RefreshTokenHook(reg interface {
return errorsx.WithStack(
fosite.ErrServerError.
WithWrap(err).
WithDebugf("refresh token hook: unmarshal response body"),
WithDescription("The refresh token hook target responded with an error.").
WithDebugf("Response from refresh token hook could not be decoded: %s", err),
)
}

// Overwrite existing session data (extra claims).
session.Extra = respBody.Session.AccessToken
idTokenClaims := session.IDTokenClaims()
idTokenClaims.Extra = respBody.Session.IDToken

return nil
}
}

0 comments on commit 2769c9b

Please sign in to comment.