From 31eb7b31671f6b7ba8c17752d082bb1adbc0b233 Mon Sep 17 00:00:00 2001 From: dushu Date: Mon, 29 Apr 2024 19:21:12 -0400 Subject: [PATCH] fix: handle the user code generation duplication --- generate-mocks.sh | 2 + generate.go | 2 + go.mod | 2 +- go.sum | 6 +- handler/rfc8628/auth_handler.go | 65 ++++++--- handler/rfc8628/auth_handler_test.go | 163 ++++++++++++++++++++- internal/rfc8628_code_strategy.go | 144 ++++++++++++++++++ internal/rfc8628_core_storage.go | 211 +++++++++++++++++++++++++++ 8 files changed, 572 insertions(+), 23 deletions(-) create mode 100644 internal/rfc8628_code_strategy.go create mode 100644 internal/rfc8628_core_storage.go diff --git a/generate-mocks.sh b/generate-mocks.sh index 2ed96b70..c85368f4 100755 --- a/generate-mocks.sh +++ b/generate-mocks.sh @@ -7,6 +7,7 @@ mockgen -package internal -destination internal/oauth2_storage.go github.com/ory mockgen -package internal -destination internal/oauth2_strategy.go github.com/ory/fosite/handler/oauth2 CoreStrategy mockgen -package internal -destination internal/authorize_code_storage.go github.com/ory/fosite/handler/oauth2 AuthorizeCodeStorage mockgen -package internal -destination internal/device_code_storage.go github.com/ory/fosite/handler/rfc8628 DeviceCodeStorage +mockgen -package internal -destination internal/rfc8628_core_storage.go github.com/ory/fosite/handler/rfc8628 RFC8628CoreStorage mockgen -package internal -destination internal/oauth2_auth_jwt_storage.go github.com/ory/fosite/handler/rfc7523 RFC7523KeyStorage mockgen -package internal -destination internal/access_token_storage.go github.com/ory/fosite/handler/oauth2 AccessTokenStorage mockgen -package internal -destination internal/refresh_token_strategy.go github.com/ory/fosite/handler/oauth2 RefreshTokenStorage @@ -17,6 +18,7 @@ mockgen -package internal -destination internal/openid_id_token_storage.go githu mockgen -package internal -destination internal/access_token_strategy.go github.com/ory/fosite/handler/oauth2 AccessTokenStrategy mockgen -package internal -destination internal/refresh_token_strategy.go github.com/ory/fosite/handler/oauth2 RefreshTokenStrategy mockgen -package internal -destination internal/authorize_code_strategy.go github.com/ory/fosite/handler/oauth2 AuthorizeCodeStrategy +mockgen -package internal -destination internal/rfc8628_code_strategy.go github.com/ory/fosite/handler/rfc8628 RFC8628CodeStrategy mockgen -package internal -destination internal/device_code_rate_limit_strategy.go github.com/ory/fosite/handler/rfc8628 DeviceRateLimitStrategy mockgen -package internal -destination internal/id_token_strategy.go github.com/ory/fosite/handler/openid OpenIDConnectTokenStrategy mockgen -package internal -destination internal/pkce_storage_strategy.go github.com/ory/fosite/handler/pkce PKCERequestStorage diff --git a/generate.go b/generate.go index 0b3efaf6..b9955192 100644 --- a/generate.go +++ b/generate.go @@ -10,6 +10,7 @@ package fosite //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/oauth2_strategy.go github.com/ory/fosite/handler/oauth2 CoreStrategy //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/authorize_code_storage.go github.com/ory/fosite/handler/oauth2 AuthorizeCodeStorage //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/device_code_storage.go github.com/ory/fosite/handler/rfc8628 DeviceCodeStorage +//go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/rfc8628_core_storage.go github.com/ory/fosite/handler/rfc8628 RFC8628CoreStorage //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/oauth2_auth_jwt_storage.go github.com/ory/fosite/handler/rfc7523 RFC7523KeyStorage //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/access_token_storage.go github.com/ory/fosite/handler/oauth2 AccessTokenStorage //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/refresh_token_strategy.go github.com/ory/fosite/handler/oauth2 RefreshTokenStorage @@ -21,6 +22,7 @@ package fosite //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/refresh_token_strategy.go github.com/ory/fosite/handler/oauth2 RefreshTokenStrategy //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/authorize_code_strategy.go github.com/ory/fosite/handler/oauth2 AuthorizeCodeStrategy //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/device_code_rate_limit_strategy.go github.com/ory/fosite/handler/rfc8628 DeviceRateLimitStrategy +//go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/rfc8628_code_strategy.go github.com/ory/fosite/handler/rfc8628 RFC8628CodeStrategy //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/id_token_strategy.go github.com/ory/fosite/handler/openid OpenIDConnectTokenStrategy //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/pkce_storage_strategy.go github.com/ory/fosite/handler/pkce PKCERequestStorage //go:generate go run github.com/golang/mock/mockgen -package internal -destination internal/authorize_handler.go github.com/ory/fosite AuthorizeEndpointHandler diff --git a/go.mod b/go.mod index 7b9a7d52..5ffbc103 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/ory/x v0.0.613 github.com/parnurzeal/gorequest v0.2.15 github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.14.3 go.opentelemetry.io/otel/trace v1.21.0 golang.org/x/crypto v0.21.0 diff --git a/go.sum b/go.sum index 3d115230..8255b9fd 100644 --- a/go.sum +++ b/go.sum @@ -392,8 +392,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -402,8 +403,9 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= diff --git a/handler/rfc8628/auth_handler.go b/handler/rfc8628/auth_handler.go index aa31ff52..8ebf874e 100644 --- a/handler/rfc8628/auth_handler.go +++ b/handler/rfc8628/auth_handler.go @@ -5,13 +5,16 @@ package rfc8628 import ( "context" + "fmt" "time" - "github.com/ory/x/errorsx" - "github.com/ory/fosite" + "github.com/ory/x/errorsx" ) +// MaxAttempts for retrying the generation of user codes. +const MaxAttempts = 3 + // DeviceAuthHandler is a response handler for the Device Authorisation Grant as // defined in https://tools.ietf.org/html/rfc8628#section-3.1 type DeviceAuthHandler struct { @@ -25,25 +28,18 @@ type DeviceAuthHandler struct { // HandleDeviceEndpointRequest implements https://tools.ietf.org/html/rfc8628#section-3.1 func (d *DeviceAuthHandler) HandleDeviceEndpointRequest(ctx context.Context, dar fosite.DeviceRequester, resp fosite.DeviceResponder) error { - deviceCode, deviceCodeSignature, err := d.Strategy.GenerateDeviceCode(ctx) - if err != nil { - return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) - } + var err error - userCode, userCodeSignature, err := d.Strategy.GenerateUserCode(ctx) + var deviceCode string + deviceCode, err = d.handleDeviceCode(ctx, dar) if err != nil { - return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) + return err } - // Store the User Code session (this has no real data other that the user and device code), can be converted into a 'full' session after user auth - dar.GetSession().SetExpiresAt(fosite.DeviceCode, time.Now().UTC().Add(d.Config.GetDeviceAndUserCodeLifespan(ctx))) - if err := d.Storage.CreateDeviceCodeSession(ctx, deviceCodeSignature, dar.Sanitize(nil)); err != nil { - return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) - } - - dar.GetSession().SetExpiresAt(fosite.UserCode, time.Now().UTC().Add(d.Config.GetDeviceAndUserCodeLifespan(ctx)).Round(time.Second)) - if err := d.Storage.CreateUserCodeSession(ctx, userCodeSignature, dar.Sanitize(nil)); err != nil { - return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) + var userCode string + userCode, err = d.handleUserCode(ctx, dar) + if err != nil { + return err } // Populate the response fields @@ -55,3 +51,38 @@ func (d *DeviceAuthHandler) HandleDeviceEndpointRequest(ctx context.Context, dar resp.SetInterval(int(d.Config.GetDeviceAuthTokenPollingInterval(ctx).Seconds())) return nil } + +func (d *DeviceAuthHandler) handleDeviceCode(ctx context.Context, dar fosite.DeviceRequester) (string, error) { + code, signature, err := d.Strategy.GenerateDeviceCode(ctx) + if err != nil { + return "", errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) + } + + dar.GetSession().SetExpiresAt(fosite.DeviceCode, time.Now().UTC().Add(d.Config.GetDeviceAndUserCodeLifespan(ctx))) + if err = d.Storage.CreateDeviceCodeSession(ctx, signature, dar.Sanitize(nil)); err != nil { + return "", errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error())) + } + + return code, nil +} + +func (d *DeviceAuthHandler) handleUserCode(ctx context.Context, dar fosite.DeviceRequester) (string, error) { + var err error + var userCode, signature string + // 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)) + if err = d.Storage.CreateUserCodeSession(ctx, signature, dar.Sanitize(nil)); err == nil { + return userCode, nil + } + } + + errMsg := fmt.Sprintf("Exceeded user-code generation max attempts %v: %s", MaxAttempts, err.Error()) + return "", errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(errMsg)) +} diff --git a/handler/rfc8628/auth_handler_test.go b/handler/rfc8628/auth_handler_test.go index 74f01050..ec20b852 100644 --- a/handler/rfc8628/auth_handler_test.go +++ b/handler/rfc8628/auth_handler_test.go @@ -5,15 +5,20 @@ package rfc8628_test import ( "context" + "errors" + "fmt" "testing" "time" - "github.com/ory/fosite/storage" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/golang/mock/gomock" + + "github.com/ory/fosite/internal" "github.com/ory/fosite" "github.com/ory/fosite/handler/rfc8628" + "github.com/ory/fosite/storage" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func Test_HandleDeviceEndpointRequest(t *testing.T) { @@ -52,3 +57,155 @@ func Test_HandleDeviceEndpointRequest(t *testing.T) { assert.Contains(t, resp.GetDeviceCode(), ".") assert.Equal(t, resp.GetVerificationURI(), "www.test.com") } + +func Test_HandleDeviceEndpointRequestWithRetry(t *testing.T) { + var mockRFC8628CoreStorage *internal.MockRFC8628CoreStorage + var mockRFC8628CodeStrategy *internal.MockRFC8628CodeStrategy + + ctx := context.Background() + req := &fosite.DeviceRequest{ + Request: fosite.Request{ + Client: &fosite.DefaultClient{ + Audience: []string{"https://www.ory.sh/api"}, + }, + Session: &fosite.DefaultSession{}, + }, + } + + testCases := []struct { + description string + setup func() + check func(t *testing.T, resp *fosite.DeviceResponse) + expectError error + }{ + { + description: "should pass when generating a unique user code at the first attempt", + setup: func() { + mockRFC8628CodeStrategy. + EXPECT(). + GenerateDeviceCode(ctx). + Return("deviceCode", "signature", nil) + mockRFC8628CoreStorage. + EXPECT(). + CreateDeviceCodeSession(ctx, "signature", gomock.Any()). + Return(nil) + mockRFC8628CodeStrategy. + EXPECT(). + GenerateUserCode(ctx). + Return("userCode", "signature", nil). + Times(1) + mockRFC8628CoreStorage. + EXPECT(). + CreateUserCodeSession(ctx, "signature", gomock.Any()). + Return(nil). + Times(1) + }, + check: func(t *testing.T, resp *fosite.DeviceResponse) { + assert.Equal(t, "userCode", resp.GetUserCode()) + }, + }, + { + description: "should pass when generating a unique user code within allowed attempts", + setup: func() { + mockRFC8628CodeStrategy. + EXPECT(). + GenerateDeviceCode(ctx). + Return("deviceCode", "signature", nil) + mockRFC8628CoreStorage. + EXPECT(). + CreateDeviceCodeSession(ctx, "signature", gomock.Any()). + Return(nil) + gomock.InOrder( + mockRFC8628CodeStrategy. + EXPECT(). + GenerateUserCode(ctx). + Return("duplicatedUserCode", "duplicatedSignature", nil), + mockRFC8628CoreStorage. + EXPECT(). + CreateUserCodeSession(ctx, "duplicatedSignature", gomock.Any()). + Return(errors.New("unique constraint violation")), + mockRFC8628CodeStrategy. + EXPECT(). + GenerateUserCode(ctx). + Return("uniqueUserCode", "uniqueSignature", nil), + mockRFC8628CoreStorage. + EXPECT(). + CreateUserCodeSession(ctx, "uniqueSignature", gomock.Any()). + Return(nil), + ) + }, + check: func(t *testing.T, resp *fosite.DeviceResponse) { + assert.Equal(t, "uniqueUserCode", resp.GetUserCode()) + }, + }, + { + description: "should fail after maximum retries to generate a unique user code", + setup: func() { + mockRFC8628CodeStrategy. + EXPECT(). + GenerateDeviceCode(ctx). + Return("deviceCode", "signature", nil) + mockRFC8628CoreStorage. + EXPECT(). + CreateDeviceCodeSession(ctx, "signature", gomock.Any()). + Return(nil) + mockRFC8628CodeStrategy. + EXPECT(). + GenerateUserCode(ctx). + Return("duplicatedUserCode", "duplicatedSignature", nil). + Times(rfc8628.MaxAttempts) + mockRFC8628CoreStorage. + EXPECT(). + CreateUserCodeSession(ctx, "duplicatedSignature", gomock.Any()). + Return(errors.New("unique constraint violation")). + Times(rfc8628.MaxAttempts) + }, + check: func(t *testing.T, resp *fosite.DeviceResponse) { + assert.Empty(t, resp.GetUserCode()) + }, + expectError: fosite.ErrServerError, + }, + } + + for _, testCase := range testCases { + t.Run(fmt.Sprintf("scenario=%s", testCase.description), func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockRFC8628CoreStorage = internal.NewMockRFC8628CoreStorage(ctrl) + mockRFC8628CodeStrategy = internal.NewMockRFC8628CodeStrategy(ctrl) + + h := rfc8628.DeviceAuthHandler{ + Storage: mockRFC8628CoreStorage, + Strategy: mockRFC8628CodeStrategy, + Config: &fosite.Config{ + DeviceAndUserCodeLifespan: time.Minute * 10, + DeviceAuthTokenPollingInterval: time.Second * 5, + DeviceVerificationURL: "www.test.com", + AccessTokenLifespan: time.Hour, + RefreshTokenLifespan: time.Hour, + ScopeStrategy: fosite.HierarchicScopeStrategy, + AudienceMatchingStrategy: fosite.DefaultAudienceMatchingStrategy, + RefreshTokenScopes: []string{"offline"}, + }, + } + + if testCase.setup != nil { + testCase.setup() + } + + resp := fosite.NewDeviceResponse() + err := h.HandleDeviceEndpointRequest(ctx, req, resp) + + if testCase.expectError != nil { + require.EqualError(t, err, testCase.expectError.Error(), "%+v", err) + } else { + require.NoError(t, err, "%+v", err) + } + + if testCase.check != nil { + testCase.check(t, resp) + } + }) + } +} diff --git a/internal/rfc8628_code_strategy.go b/internal/rfc8628_code_strategy.go new file mode 100644 index 00000000..a328ac1f --- /dev/null +++ b/internal/rfc8628_code_strategy.go @@ -0,0 +1,144 @@ +// Copyright © 2024 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/ory/fosite/handler/rfc8628 (interfaces: RFC8628CodeStrategy) + +// Package internal is a generated GoMock package. +package internal + +import ( + context "context" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + fosite "github.com/ory/fosite" +) + +// MockRFC8628CodeStrategy is a mock of RFC8628CodeStrategy interface. +type MockRFC8628CodeStrategy struct { + ctrl *gomock.Controller + recorder *MockRFC8628CodeStrategyMockRecorder +} + +// MockRFC8628CodeStrategyMockRecorder is the mock recorder for MockRFC8628CodeStrategy. +type MockRFC8628CodeStrategyMockRecorder struct { + mock *MockRFC8628CodeStrategy +} + +// NewMockRFC8628CodeStrategy creates a new mock instance. +func NewMockRFC8628CodeStrategy(ctrl *gomock.Controller) *MockRFC8628CodeStrategy { + mock := &MockRFC8628CodeStrategy{ctrl: ctrl} + mock.recorder = &MockRFC8628CodeStrategyMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockRFC8628CodeStrategy) EXPECT() *MockRFC8628CodeStrategyMockRecorder { + return m.recorder +} + +// DeviceCodeSignature mocks base method. +func (m *MockRFC8628CodeStrategy) DeviceCodeSignature(arg0 context.Context, arg1 string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeviceCodeSignature", arg0, arg1) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DeviceCodeSignature indicates an expected call of DeviceCodeSignature. +func (mr *MockRFC8628CodeStrategyMockRecorder) DeviceCodeSignature(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeviceCodeSignature", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).DeviceCodeSignature), arg0, arg1) +} + +// GenerateDeviceCode mocks base method. +func (m *MockRFC8628CodeStrategy) GenerateDeviceCode(arg0 context.Context) (string, string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateDeviceCode", arg0) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(string) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GenerateDeviceCode indicates an expected call of GenerateDeviceCode. +func (mr *MockRFC8628CodeStrategyMockRecorder) GenerateDeviceCode(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateDeviceCode", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).GenerateDeviceCode), arg0) +} + +// GenerateUserCode mocks base method. +func (m *MockRFC8628CodeStrategy) GenerateUserCode(arg0 context.Context) (string, string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateUserCode", arg0) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(string) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GenerateUserCode indicates an expected call of GenerateUserCode. +func (mr *MockRFC8628CodeStrategyMockRecorder) GenerateUserCode(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateUserCode", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).GenerateUserCode), arg0) +} + +// ShouldRateLimit mocks base method. +func (m *MockRFC8628CodeStrategy) ShouldRateLimit(arg0 context.Context, arg1 string) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ShouldRateLimit", arg0, arg1) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ShouldRateLimit indicates an expected call of ShouldRateLimit. +func (mr *MockRFC8628CodeStrategyMockRecorder) ShouldRateLimit(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ShouldRateLimit", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).ShouldRateLimit), arg0, arg1) +} + +// UserCodeSignature mocks base method. +func (m *MockRFC8628CodeStrategy) UserCodeSignature(arg0 context.Context, arg1 string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UserCodeSignature", arg0, arg1) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UserCodeSignature indicates an expected call of UserCodeSignature. +func (mr *MockRFC8628CodeStrategyMockRecorder) UserCodeSignature(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UserCodeSignature", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).UserCodeSignature), arg0, arg1) +} + +// ValidateDeviceCode mocks base method. +func (m *MockRFC8628CodeStrategy) ValidateDeviceCode(arg0 context.Context, arg1 fosite.Requester, arg2 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateDeviceCode", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// ValidateDeviceCode indicates an expected call of ValidateDeviceCode. +func (mr *MockRFC8628CodeStrategyMockRecorder) ValidateDeviceCode(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateDeviceCode", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).ValidateDeviceCode), arg0, arg1, arg2) +} + +// ValidateUserCode mocks base method. +func (m *MockRFC8628CodeStrategy) ValidateUserCode(arg0 context.Context, arg1 fosite.Requester, arg2 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateUserCode", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// ValidateUserCode indicates an expected call of ValidateUserCode. +func (mr *MockRFC8628CodeStrategyMockRecorder) ValidateUserCode(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateUserCode", reflect.TypeOf((*MockRFC8628CodeStrategy)(nil).ValidateUserCode), arg0, arg1, arg2) +} diff --git a/internal/rfc8628_core_storage.go b/internal/rfc8628_core_storage.go new file mode 100644 index 00000000..93340de2 --- /dev/null +++ b/internal/rfc8628_core_storage.go @@ -0,0 +1,211 @@ +// Copyright © 2024 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/ory/fosite/handler/rfc8628 (interfaces: RFC8628CoreStorage) + +// Package internal is a generated GoMock package. +package internal + +import ( + context "context" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + fosite "github.com/ory/fosite" +) + +// MockRFC8628CoreStorage is a mock of RFC8628CoreStorage interface. +type MockRFC8628CoreStorage struct { + ctrl *gomock.Controller + recorder *MockRFC8628CoreStorageMockRecorder +} + +// MockRFC8628CoreStorageMockRecorder is the mock recorder for MockRFC8628CoreStorage. +type MockRFC8628CoreStorageMockRecorder struct { + mock *MockRFC8628CoreStorage +} + +// NewMockRFC8628CoreStorage creates a new mock instance. +func NewMockRFC8628CoreStorage(ctrl *gomock.Controller) *MockRFC8628CoreStorage { + mock := &MockRFC8628CoreStorage{ctrl: ctrl} + mock.recorder = &MockRFC8628CoreStorageMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockRFC8628CoreStorage) EXPECT() *MockRFC8628CoreStorageMockRecorder { + return m.recorder +} + +// CreateAccessTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) CreateAccessTokenSession(arg0 context.Context, arg1 string, arg2 fosite.Requester) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateAccessTokenSession", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateAccessTokenSession indicates an expected call of CreateAccessTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) CreateAccessTokenSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAccessTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).CreateAccessTokenSession), arg0, arg1, arg2) +} + +// CreateDeviceCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) CreateDeviceCodeSession(arg0 context.Context, arg1 string, arg2 fosite.Requester) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateDeviceCodeSession", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateDeviceCodeSession indicates an expected call of CreateDeviceCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) CreateDeviceCodeSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateDeviceCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).CreateDeviceCodeSession), arg0, arg1, arg2) +} + +// CreateRefreshTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) CreateRefreshTokenSession(arg0 context.Context, arg1 string, arg2 fosite.Requester) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateRefreshTokenSession", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateRefreshTokenSession indicates an expected call of CreateRefreshTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) CreateRefreshTokenSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRefreshTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).CreateRefreshTokenSession), arg0, arg1, arg2) +} + +// CreateUserCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) CreateUserCodeSession(arg0 context.Context, arg1 string, arg2 fosite.Requester) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateUserCodeSession", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateUserCodeSession indicates an expected call of CreateUserCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) CreateUserCodeSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUserCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).CreateUserCodeSession), arg0, arg1, arg2) +} + +// DeleteAccessTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) DeleteAccessTokenSession(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteAccessTokenSession", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteAccessTokenSession indicates an expected call of DeleteAccessTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) DeleteAccessTokenSession(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAccessTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).DeleteAccessTokenSession), arg0, arg1) +} + +// DeleteRefreshTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) DeleteRefreshTokenSession(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteRefreshTokenSession", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteRefreshTokenSession indicates an expected call of DeleteRefreshTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) DeleteRefreshTokenSession(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteRefreshTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).DeleteRefreshTokenSession), arg0, arg1) +} + +// GetAccessTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) GetAccessTokenSession(arg0 context.Context, arg1 string, arg2 fosite.Session) (fosite.Requester, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAccessTokenSession", arg0, arg1, arg2) + ret0, _ := ret[0].(fosite.Requester) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetAccessTokenSession indicates an expected call of GetAccessTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) GetAccessTokenSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccessTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).GetAccessTokenSession), arg0, arg1, arg2) +} + +// GetDeviceCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) GetDeviceCodeSession(arg0 context.Context, arg1 string, arg2 fosite.Session) (fosite.Requester, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDeviceCodeSession", arg0, arg1, arg2) + ret0, _ := ret[0].(fosite.Requester) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDeviceCodeSession indicates an expected call of GetDeviceCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) GetDeviceCodeSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDeviceCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).GetDeviceCodeSession), arg0, arg1, arg2) +} + +// GetRefreshTokenSession mocks base method. +func (m *MockRFC8628CoreStorage) GetRefreshTokenSession(arg0 context.Context, arg1 string, arg2 fosite.Session) (fosite.Requester, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRefreshTokenSession", arg0, arg1, arg2) + ret0, _ := ret[0].(fosite.Requester) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRefreshTokenSession indicates an expected call of GetRefreshTokenSession. +func (mr *MockRFC8628CoreStorageMockRecorder) GetRefreshTokenSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRefreshTokenSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).GetRefreshTokenSession), arg0, arg1, arg2) +} + +// GetUserCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) GetUserCodeSession(arg0 context.Context, arg1 string, arg2 fosite.Session) (fosite.Requester, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUserCodeSession", arg0, arg1, arg2) + ret0, _ := ret[0].(fosite.Requester) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetUserCodeSession indicates an expected call of GetUserCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) GetUserCodeSession(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).GetUserCodeSession), arg0, arg1, arg2) +} + +// InvalidateDeviceCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) InvalidateDeviceCodeSession(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InvalidateDeviceCodeSession", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// InvalidateDeviceCodeSession indicates an expected call of InvalidateDeviceCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) InvalidateDeviceCodeSession(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InvalidateDeviceCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).InvalidateDeviceCodeSession), arg0, arg1) +} + +// InvalidateUserCodeSession mocks base method. +func (m *MockRFC8628CoreStorage) InvalidateUserCodeSession(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "InvalidateUserCodeSession", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// InvalidateUserCodeSession indicates an expected call of InvalidateUserCodeSession. +func (mr *MockRFC8628CoreStorageMockRecorder) InvalidateUserCodeSession(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InvalidateUserCodeSession", reflect.TypeOf((*MockRFC8628CoreStorage)(nil).InvalidateUserCodeSession), arg0, arg1) +}