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

refactor: transactional interface #772

Closed
wants to merge 4 commits into from
Closed
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
34 changes: 12 additions & 22 deletions handler/oauth2/flow_authorize_code_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,21 @@ func (c *AuthorizeExplicitGrantHandler) PopulateTokenEndpointResponse(ctx contex
}
}

ctx, err = storage.MaybeBeginTx(ctx, c.CoreStorage)
if err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
defer func() {
if err != nil {
if rollBackTxnErr := storage.MaybeRollbackTx(ctx, c.CoreStorage); rollBackTxnErr != nil {
err = errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebugf("error: %s; rollback error: %s", err, rollBackTxnErr))
}
if err := storage.MaybeTransaction(ctx, c.CoreStorage, func(ctx context.Context) error {
if err := c.CoreStorage.InvalidateAuthorizeCodeSession(ctx, signature); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
}()

if err = c.CoreStorage.InvalidateAuthorizeCodeSession(ctx, signature); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
} else if err = c.CoreStorage.CreateAccessTokenSession(ctx, accessSignature, requester.Sanitize([]string{})); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
} else if refreshSignature != "" {
if err = c.CoreStorage.CreateRefreshTokenSession(ctx, refreshSignature, requester.Sanitize([]string{})); err != nil {
if err := c.CoreStorage.CreateAccessTokenSession(ctx, accessSignature, requester.Sanitize([]string{})); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
if refreshSignature != "" {
if err := c.CoreStorage.CreateRefreshTokenSession(ctx, refreshSignature, requester.Sanitize([]string{})); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
}
return nil
}); err != nil {
return err // error already wrapped inside tx callback
}

responder.SetAccessToken(access)
Expand All @@ -182,11 +177,6 @@ func (c *AuthorizeExplicitGrantHandler) PopulateTokenEndpointResponse(ctx contex
if refresh != "" {
responder.SetExtra("refresh_token", refresh)
}

if err = storage.MaybeCommitTx(ctx, c.CoreStorage); err != nil {
return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}

return nil
}

Expand Down
237 changes: 0 additions & 237 deletions handler/oauth2/flow_authorize_code_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ import (
//"github.com/golang/mock/gomock"
"time"

"github.com/golang/mock/gomock"

"github.com/ory/fosite/internal"

"github.com/ory/fosite" //"github.com/ory/fosite/internal"
"github.com/ory/fosite/storage"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -441,235 +436,3 @@ func TestAuthorizeCode_HandleTokenEndpointRequest(t *testing.T) {
})
}
}

func TestAuthorizeCodeTransactional_HandleTokenEndpointRequest(t *testing.T) {
var mockTransactional *internal.MockTransactional
var mockCoreStore *internal.MockCoreStorage
strategy := hmacshaStrategy
request := &fosite.AccessRequest{
GrantTypes: fosite.Arguments{"authorization_code"},
Request: fosite.Request{
Client: &fosite.DefaultClient{
GrantTypes: fosite.Arguments{"authorization_code", "refresh_token"},
},
GrantedScope: fosite.Arguments{"offline"},
Session: &fosite.DefaultSession{},
RequestedAt: time.Now().UTC(),
},
}
token, _, err := strategy.GenerateAuthorizeCode(context.Background(), nil)
require.NoError(t, err)
request.Form = url.Values{"code": {token}}
response := fosite.NewAccessResponse()
propagatedContext := context.Background()

// some storage implementation that has support for transactions, notice the embedded type `storage.Transactional`
type transactionalStore struct {
storage.Transactional
CoreStorage
}

for _, testCase := range []struct {
description string
setup func()
expectError error
}{
{
description: "transaction should be committed successfully if no errors occur",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(propagatedContext, nil)
mockCoreStore.
EXPECT().
InvalidateAuthorizeCodeSession(gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockCoreStore.
EXPECT().
CreateAccessTokenSession(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockCoreStore.
EXPECT().
CreateRefreshTokenSession(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockTransactional.
EXPECT().
Commit(propagatedContext).
Return(nil).
Times(1)
},
},
{
description: "transaction should be rolled back if `InvalidateAuthorizeCodeSession` returns an error",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(propagatedContext, nil)
mockCoreStore.
EXPECT().
InvalidateAuthorizeCodeSession(gomock.Any(), gomock.Any()).
Return(errors.New("Whoops, a nasty database error occurred!")).
Times(1)
mockTransactional.
EXPECT().
Rollback(propagatedContext).
Return(nil).
Times(1)
},
expectError: fosite.ErrServerError,
},
{
description: "transaction should be rolled back if `CreateAccessTokenSession` returns an error",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(propagatedContext, nil)
mockCoreStore.
EXPECT().
InvalidateAuthorizeCodeSession(gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockCoreStore.
EXPECT().
CreateAccessTokenSession(propagatedContext, gomock.Any(), gomock.Any()).
Return(errors.New("Whoops, a nasty database error occurred!")).
Times(1)
mockTransactional.
EXPECT().
Rollback(propagatedContext).
Return(nil).
Times(1)
},
expectError: fosite.ErrServerError,
},
{
description: "should result in a server error if transaction cannot be created",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(nil, errors.New("Whoops, unable to create transaction!"))
},
expectError: fosite.ErrServerError,
},
{
description: "should result in a server error if transaction cannot be rolled back",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(propagatedContext, nil)
mockCoreStore.
EXPECT().
InvalidateAuthorizeCodeSession(gomock.Any(), gomock.Any()).
Return(errors.New("Whoops, a nasty database error occurred!")).
Times(1)
mockTransactional.
EXPECT().
Rollback(propagatedContext).
Return(errors.New("Whoops, unable to rollback transaction!")).
Times(1)
},
expectError: fosite.ErrServerError,
},
{
description: "should result in a server error if transaction cannot be committed",
setup: func() {
mockCoreStore.
EXPECT().
GetAuthorizeCodeSession(gomock.Any(), gomock.Any(), gomock.Any()).
Return(request, nil).
Times(1)
mockTransactional.
EXPECT().
BeginTX(propagatedContext).
Return(propagatedContext, nil)
mockCoreStore.
EXPECT().
InvalidateAuthorizeCodeSession(gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockCoreStore.
EXPECT().
CreateAccessTokenSession(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockCoreStore.
EXPECT().
CreateRefreshTokenSession(propagatedContext, gomock.Any(), gomock.Any()).
Return(nil).
Times(1)
mockTransactional.
EXPECT().
Commit(propagatedContext).
Return(errors.New("Whoops, unable to commit transaction!")).
Times(1)
mockTransactional.
EXPECT().
Rollback(propagatedContext).
Return(nil).
Times(1)
},
expectError: fosite.ErrServerError,
},
} {
t.Run(fmt.Sprintf("scenario=%s", testCase.description), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockTransactional = internal.NewMockTransactional(ctrl)
mockCoreStore = internal.NewMockCoreStorage(ctrl)
testCase.setup()

handler := AuthorizeExplicitGrantHandler{
CoreStorage: transactionalStore{
mockTransactional,
mockCoreStore,
},
AccessTokenStrategy: &strategy,
RefreshTokenStrategy: &strategy,
AuthorizeCodeStrategy: &strategy,
Config: &fosite.Config{
ScopeStrategy: fosite.HierarchicScopeStrategy,
AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy,
AuthorizeCodeLifespan: time.Minute,
},
}

if err := handler.PopulateTokenEndpointResponse(propagatedContext, request, response); testCase.expectError != nil {
assert.EqualError(t, err, testCase.expectError.Error())
}
})
}
}
Loading
Loading