Skip to content

Commit

Permalink
chore: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wood-push-melon committed May 5, 2024
1 parent 98d4d15 commit e0c091f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
49 changes: 17 additions & 32 deletions handler/rfc8628/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ package rfc8628

import (
"context"
"errors"
"time"

"github.com/ory/x/sqlcon"

"github.com/avast/retry-go/v4"

"github.com/ory/fosite"
"github.com/ory/x/errorsx"
)
Expand Down Expand Up @@ -68,33 +63,23 @@ func (d *DeviceAuthHandler) handleDeviceCode(ctx context.Context, dar fosite.Dev
}

func (d *DeviceAuthHandler) handleUserCode(ctx context.Context, dar fosite.DeviceRequester) (string, error) {
userCode, err := retry.DoWithData(
func() (string, error) {
code, signature, err := d.Strategy.GenerateUserCode(ctx)
if err != nil {
return "", err
}

dar.GetSession().SetExpiresAt(fosite.UserCode, time.Now().UTC().Add(d.Config.GetDeviceAndUserCodeLifespan(ctx)).Round(time.Second))
err = d.Storage.CreateUserCodeSession(ctx, signature, dar.Sanitize(nil))
if err != nil {
return "", err
}

return code, nil
},
retry.Attempts(3),
retry.Context(ctx),
retry.Delay(0),
retry.DelayType(retry.CombineDelay(retry.FixedDelay)),
retry.LastErrorOnly(true),
retry.RetryIf(func(err error) bool {
return errors.Is(err, sqlcon.ErrUniqueViolation)
}),
)
if err != nil {
return "", errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
var err error
var userCode, signature string
maxAttempts := 3
// Retry when persisting user code fails
// Possible causes include database connection issue, constraints violations, etc.
for i := 0; i < maxAttempts; i++ {
userCode, signature, err = d.Strategy.GenerateUserCode(ctx)
if err != nil {
return "", err
}

dar.GetSession().SetExpiresAt(fosite.UserCode, time.Now().UTC().Add(d.Config.GetDeviceAndUserCodeLifespan(ctx)).Round(time.Second))
err = d.Storage.CreateUserCodeSession(ctx, signature, dar.Sanitize(nil))
if err == nil {
return userCode, nil
}
}

return userCode, nil
return "", errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
}
13 changes: 7 additions & 6 deletions handler/rfc8628/auth_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ package rfc8628_test

import (
"context"
"errors"
"fmt"
"testing"
"time"

"github.com/ory/x/sqlcon"

"github.com/golang/mock/gomock"

"github.com/ory/fosite/internal"
Expand Down Expand Up @@ -94,11 +93,13 @@ func Test_HandleDeviceEndpointRequestWithRetry(t *testing.T) {
mockRFC8628CodeStrategy.
EXPECT().
GenerateUserCode(ctx).
Return("userCode", "signature", nil)
Return("userCode", "signature", nil).
Times(1)
mockRFC8628CoreStorage.
EXPECT().
CreateUserCodeSession(ctx, "signature", gomock.Any()).
Return(nil)
Return(nil).
Times(1)
},
check: func(t *testing.T, resp *fosite.DeviceResponse) {
assert.Equal(t, "userCode", resp.GetUserCode())
Expand All @@ -123,7 +124,7 @@ func Test_HandleDeviceEndpointRequestWithRetry(t *testing.T) {
mockRFC8628CoreStorage.
EXPECT().
CreateUserCodeSession(ctx, "duplicatedSignature", gomock.Any()).
Return(sqlcon.ErrUniqueViolation),
Return(errors.New("unique constraint violation")),
mockRFC8628CodeStrategy.
EXPECT().
GenerateUserCode(ctx).
Expand Down Expand Up @@ -157,7 +158,7 @@ func Test_HandleDeviceEndpointRequestWithRetry(t *testing.T) {
mockRFC8628CoreStorage.
EXPECT().
CreateUserCodeSession(ctx, "duplicatedSignature", gomock.Any()).
Return(sqlcon.ErrUniqueViolation).
Return(errors.New("unique constraint violation")).
Times(maxAttempts)
},
check: func(t *testing.T, resp *fosite.DeviceResponse) {
Expand Down

0 comments on commit e0c091f

Please sign in to comment.