Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new function to TokenRevocationStorage to support refresh token grace-period #635

Merged
merged 3 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion handler/oauth2/flow_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func (c *RefreshTokenGrantHandler) PopulateTokenEndpointResponse(ctx context.Con
return c.handleRefreshTokenEndpointStorageError(ctx, true, err)
} else if err := c.TokenRevocationStorage.RevokeAccessToken(ctx, ts.GetID()); err != nil {
return c.handleRefreshTokenEndpointStorageError(ctx, true, err)
} else if err := c.TokenRevocationStorage.RevokeRefreshToken(ctx, ts.GetID()); err != nil {
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
}

if err := c.TokenRevocationStorage.RevokeRefreshTokenMaybeGracePeriod(ctx, ts.GetID(), signature); err != nil {
return c.handleRefreshTokenEndpointStorageError(ctx, true, err)
}

Expand Down
22 changes: 11 additions & 11 deletions handler/oauth2/flow_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -620,7 +620,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
expectError: fosite.ErrInvalidRequest,
},
{
description: "transaction should be rolled back if call to `RevokeRefreshToken` results in an error",
description: "transaction should be rolled back if call to `RevokeRefreshTokenMaybeGracePeriod` results in an error",
setup: func() {
request.GrantTypes = fosite.Arguments{"refresh_token"}
mockTransactional.
Expand All @@ -640,7 +640,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(errors.New("Whoops, a nasty database error occurred!")).
Times(1)
mockTransactional.
Expand All @@ -652,7 +652,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
expectError: fosite.ErrServerError,
},
{
description: "should result in a fosite.ErrInvalidRequest if call to `RevokeRefreshToken` results in a " +
description: "should result in a fosite.ErrInvalidRequest if call to `RevokeRefreshTokenMaybeGracePeriod` results in a " +
"fosite.ErrSerializationFailure error",
setup: func() {
request.GrantTypes = fosite.Arguments{"refresh_token"}
Expand All @@ -673,7 +673,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(fosite.ErrSerializationFailure).
Times(1)
mockTransactional.
Expand Down Expand Up @@ -705,7 +705,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -741,7 +741,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -821,7 +821,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -897,7 +897,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down Expand Up @@ -940,7 +940,7 @@ func TestRefreshFlowTransactional_PopulateTokenEndpointResponse(t *testing.T) {
Times(1)
mockRevocationStore.
EXPECT().
RevokeRefreshToken(propagatedContext, gomock.Any()).
RevokeRefreshTokenMaybeGracePeriod(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockRevocationStore.
Expand Down
12 changes: 12 additions & 0 deletions handler/oauth2/revocation_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ type TokenRevocationStorage interface {
// grant (see Implementation Note).
RevokeRefreshToken(ctx context.Context, requestID string) error

// RevokeRefreshTokenMaybeGracePeriod revokes a refresh token as specified in:
// https://tools.ietf.org/html/rfc7009#section-2.1
// If the particular
// token is a refresh token and the authorization server supports the
// revocation of access tokens, then the authorization server SHOULD
// also invalidate all access tokens based on the same authorization
// grant (see Implementation Note).
//
// If the Refresh Token grace period is greater than zero in configuration the token
// will have its expiration time set as UTCNow + GracePeriod.
RevokeRefreshTokenMaybeGracePeriod(ctx context.Context, requestID string, signature string) error

// RevokeAccessToken revokes an access token as specified in:
// https://tools.ietf.org/html/rfc7009#section-2.1
// If the token passed to the request
Expand Down
19 changes: 19 additions & 0 deletions internal/oauth2_revoke_storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ func (s *MemoryStore) RevokeRefreshToken(ctx context.Context, requestID string)
return nil
}

func (s *MemoryStore) RevokeRefreshTokenMaybeGracePeriod(ctx context.Context, requestID string, signature string) error {
// no configuration option is available; grace period is not available with memory store
return s.RevokeRefreshToken(ctx, requestID)
}

func (s *MemoryStore) RevokeAccessToken(ctx context.Context, requestID string) error {
s.accessTokenRequestIDsMutex.RLock()
defer s.accessTokenRequestIDsMutex.RUnlock()
Expand Down