Skip to content

Commit 9a18e70

Browse files
committed
fix: refresh token error wrappers return InactiveToken
Fixes an issue where the incorrect error code was returned when rotating refresh tokens.
1 parent c4fe21c commit 9a18e70

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

handler/oauth2/flow_refresh.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (c *RefreshTokenGrantHandler) HandleTokenEndpointRequest(ctx context.Contex
5353

5454
return errorsx.WithStack(fosite.ErrInactiveToken.WithWrap(err).WithDebug(err.Error()))
5555
} else if errors.Is(err, fosite.ErrNotFound) {
56-
return errorsx.WithStack(fosite.ErrInvalidGrant.WithWrap(err).WithDebugf("The refresh token has not been found: %s", err.Error()))
56+
return errorsx.WithStack(fosite.ErrInactiveToken.WithWrap(err).WithDebug("The refresh token can not be found."))
5757
} else if err != nil {
5858
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
5959
} else if err := c.RefreshTokenStrategy.ValidateRefreshToken(ctx, originalRequest, refresh); err != nil {
@@ -129,23 +129,20 @@ func (c *RefreshTokenGrantHandler) PopulateTokenEndpointResponse(ctx context.Con
129129
if err != nil {
130130
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
131131
}
132-
defer func() {
133-
err = c.handleRefreshTokenEndpointStorageError(ctx, err)
134-
}()
135132

136133
storeReq := requester.Sanitize([]string{})
137134
storeReq.SetID(requester.GetID())
138135

139136
if err = c.TokenRevocationStorage.RotateRefreshToken(ctx, requester.GetID(), signature); err != nil {
140-
return err
137+
return c.handleRefreshTokenEndpointStorageError(ctx, err)
141138
}
142139

143140
if err = c.TokenRevocationStorage.CreateAccessTokenSession(ctx, accessSignature, storeReq); err != nil {
144-
return err
141+
return c.handleRefreshTokenEndpointStorageError(ctx, err)
145142
}
146143

147144
if err = c.TokenRevocationStorage.CreateRefreshTokenSession(ctx, refreshSignature, accessSignature, storeReq); err != nil {
148-
return err
145+
return c.handleRefreshTokenEndpointStorageError(ctx, err)
149146
}
150147

151148
responder.SetAccessToken(accessToken)
@@ -156,7 +153,7 @@ func (c *RefreshTokenGrantHandler) PopulateTokenEndpointResponse(ctx context.Con
156153
responder.SetExtra("refresh_token", refreshToken)
157154

158155
if err = storage.MaybeCommitTx(ctx, c.TokenRevocationStorage); err != nil {
159-
return err
156+
return c.handleRefreshTokenEndpointStorageError(ctx, err)
160157
}
161158

162159
return nil
@@ -214,14 +211,14 @@ func (c *RefreshTokenGrantHandler) handleRefreshTokenEndpointStorageError(ctx co
214211
return errorsx.WithStack(fosite.ErrInvalidRequest.
215212
WithDebugf(storageErr.Error()).
216213
WithWrap(storageErr).
217-
WithHint("Failed to refresh token because of multiple concurrent requests using the same token which is not allowed."))
214+
WithHint("Failed to refresh token because of multiple concurrent requests using the same token. Please retry the request."))
218215
}
219216

220217
if errors.Is(storageErr, fosite.ErrNotFound) || errors.Is(storageErr, fosite.ErrInactiveToken) {
221218
return errorsx.WithStack(fosite.ErrInvalidRequest.
222219
WithDebugf(storageErr.Error()).
223220
WithWrap(storageErr).
224-
WithHint("Failed to refresh token because of multiple concurrent requests using the same token which is not allowed."))
221+
WithHint("Failed to refresh token. Please retry the request."))
225222
}
226223

227224
return errorsx.WithStack(fosite.ErrServerError.WithWrap(storageErr).WithDebug(storageErr.Error()))

handler/oauth2/flow_refresh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
5858

5959
areq.Form.Add("refresh_token", "some.refreshtokensig")
6060
},
61-
expectErr: fosite.ErrInvalidGrant,
61+
expectErr: fosite.ErrInactiveToken,
6262
},
6363
{
6464
description: "should fail because token is valid but does not exist",
@@ -70,7 +70,7 @@ func TestRefreshFlow_HandleTokenEndpointRequest(t *testing.T) {
7070
require.NoError(t, err)
7171
areq.Form.Add("refresh_token", token)
7272
},
73-
expectErr: fosite.ErrInvalidGrant,
73+
expectErr: fosite.ErrInactiveToken,
7474
},
7575
{
7676
description: "should fail because client mismatches",

0 commit comments

Comments
 (0)