Skip to content

Commit

Permalink
refactor: replace all occurrences of login request to flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent 48c4906 commit 1b3c491
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion driver/registry.go
Expand Up @@ -95,7 +95,7 @@ type Registry interface {
settings.RequestPersistenceProvider
settings.StrategyProvider

login.RequestPersistenceProvider
login.FlowPersistenceProvider
login.ErrorHandlerProvider
login.HooksProvider
login.HookExecutorProvider
Expand Down
2 changes: 1 addition & 1 deletion internal/testhelpers/login.go
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ory/kratos/x"
)

func NewLoginUIRequestEchoServer(t *testing.T, reg driver.Registry) *httptest.Server {
func NewLoginUIFlowEchoServer(t *testing.T, reg driver.Registry) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e, err := reg.LoginFlowPersister().GetLoginFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("flow")))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/testhelpers/selfservice.go
Expand Up @@ -128,7 +128,7 @@ func SelfServiceHookRegistrationViperSetPost(strategy string, c []configuration.
}

func SelfServiceHookLoginErrorHandler(t *testing.T, w http.ResponseWriter, r *http.Request, err error) bool {
return selfServiceHookErrorHandler(t, w, r, login.ErrHookAbortRequest, err)
return selfServiceHookErrorHandler(t, w, r, login.ErrHookAbortFlow, err)
}

func SelfServiceHookRegistrationErrorHandler(t *testing.T, w http.ResponseWriter, r *http.Request, err error) bool {
Expand Down
16 changes: 8 additions & 8 deletions selfservice/flow/login/error.go
Expand Up @@ -21,8 +21,8 @@ import (
)

var (
ErrHookAbortRequest = errors.New("aborted login hook execution")
ErrAlreadyLoggedIn = herodot.ErrBadRequest.WithReason("A valid session was detected and thus login is not possible. Did you forget to set `?refresh=true`?")
ErrHookAbortFlow = errors.New("aborted login hook execution")
ErrAlreadyLoggedIn = herodot.ErrBadRequest.WithReason("A valid session was detected and thus login is not possible. Did you forget to set `?refresh=true`?")
)

type (
Expand All @@ -31,7 +31,7 @@ type (
x.WriterProvider
x.LoggingProvider

RequestPersistenceProvider
FlowPersistenceProvider
HandlerProvider
}

Expand All @@ -42,14 +42,14 @@ type (
c configuration.Provider
}

flowExpiredError struct {
FlowExpiredError struct {
*herodot.DefaultError
ago time.Duration
}
)

func NewRequestExpiredError(ago time.Duration) *flowExpiredError {
return &flowExpiredError{
func NewFlowExpiredError(ago time.Duration) *FlowExpiredError {
return &FlowExpiredError{
ago: ago,
DefaultError: herodot.ErrBadRequest.
WithError("login flow expired").
Expand All @@ -74,7 +74,7 @@ func (s *ErrorHandler) WriteFlowError(w http.ResponseWriter, r *http.Request, ct
return
}

if e := new(flowExpiredError); errors.As(err, &e) {
if e := new(FlowExpiredError); errors.As(err, &e) {
// create new flow because the old one is not valid
a, err := s.d.LoginHandler().NewLoginFlow(w, r, f.Type)
if err != nil {
Expand All @@ -83,7 +83,7 @@ func (s *ErrorHandler) WriteFlowError(w http.ResponseWriter, r *http.Request, ct
return
}

a.Messages.Add(text.NewErrorValidationLoginRequestExpired(e.ago))
a.Messages.Add(text.NewErrorValidationLoginFlowExpired(e.ago))
if err := s.d.LoginFlowPersister().UpdateLoginFlow(r.Context(), a); err != nil {
s.forward(w, r, a, err)
return
Expand Down
12 changes: 6 additions & 6 deletions selfservice/flow/login/error_test.go
Expand Up @@ -38,7 +38,7 @@ func TestHandleError(t *testing.T) {
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)

testhelpers.NewLoginUIRequestEchoServer(t, reg)
testhelpers.NewLoginUIFlowEchoServer(t, reg)
testhelpers.NewErrorTestServer(t, reg)

h := reg.LoginFlowErrorHandler()
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestHandleError(t *testing.T) {
res, err := ts.Client().Do(testhelpers.NewHTTPGetJSONRequest(t, ts.URL+"/error"))
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, "application/json; charset=utf-8", res.Header.Get("Content-Type"))
assert.Contains(t, res.Header.Get("Content-Type"), "application/json")
assert.NotContains(t, res.Request.URL.String(), conf.SelfServiceFlowErrorURL().String()+"?error=")

body, err := ioutil.ReadAll(res.Body)
Expand All @@ -113,7 +113,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

loginFlow = newFlow(t, time.Minute, flow.TypeAPI)
flowError = login.NewRequestExpiredError(time.Hour)
flowError = login.NewFlowExpiredError(time.Hour)
ct = identity.CredentialsTypePassword

res, err := ts.Client().Do(testhelpers.NewHTTPGetJSONRequest(t, ts.URL+"/error"))
Expand All @@ -124,7 +124,7 @@ func TestHandleError(t *testing.T) {

body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
assert.Equal(t, int(text.ErrorValidationLoginRequestExpired), int(gjson.GetBytes(body, "messages.0.id").Int()))
assert.Equal(t, int(text.ErrorValidationLoginFlowExpired), int(gjson.GetBytes(body, "messages.0.id").Int()))
assert.NotEqual(t, loginFlow.ID.String(), gjson.GetBytes(body, "id").String())
})

Expand Down Expand Up @@ -198,12 +198,12 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

loginFlow = &login.Flow{Type: flow.TypeBrowser}
flowError = login.NewRequestExpiredError(time.Hour)
flowError = login.NewFlowExpiredError(time.Hour)
ct = identity.CredentialsTypePassword

lf, _ := expectLoginUI(t)
require.Len(t, lf.Messages, 1)
assert.Equal(t, int(text.ErrorValidationLoginRequestExpired), int(lf.Messages[0].ID))
assert.Equal(t, int(text.ErrorValidationLoginFlowExpired), int(lf.Messages[0].ID))
})

t.Run("case=validation error", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions selfservice/flow/login/flow.go
Expand Up @@ -80,7 +80,7 @@ type Flow struct {
// UpdatedAt is a helper struct field for gobuffalo.pop.
UpdatedAt time.Time `json:"-" db:"updated_at"`

// CSRFToken contains the anti-csrf token associated with this flow.
// CSRFToken contains the anti-csrf token associated with this flow. Only set for browser flows.
CSRFToken string `json:"-" db:"csrf_token"`

// Forced stores whether this login flow should enforce re-authentication.
Expand Down Expand Up @@ -143,7 +143,7 @@ func (f Flow) TableName() string {

func (f *Flow) Valid() error {
if f.ExpiresAt.Before(time.Now()) {
return errors.WithStack(NewRequestExpiredError(time.Since(f.ExpiresAt)))
return errors.WithStack(NewFlowExpiredError(time.Since(f.ExpiresAt)))
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions selfservice/flow/login/handler.go
Expand Up @@ -50,11 +50,11 @@ func NewHandler(d handlerDependencies, c configuration.Provider) *Handler {
func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
public.GET(RouteInitBrowserFlow, h.initBrowserFlow)
public.GET(RouteInitAPIFlow, h.initAPIFlow)
public.GET(RouteGetFlow, h.fetchLoginRequest)
public.GET(RouteGetFlow, h.fetchLoginFlow)
}

func (h *Handler) RegisterAdminRoutes(admin *x.RouterAdmin) {
admin.GET(RouteGetFlow, h.fetchLoginRequest)
admin.GET(RouteGetFlow, h.fetchLoginFlow)
}

func (h *Handler) NewLoginFlow(w http.ResponseWriter, r *http.Request, flow flow.Type) (*Flow, error) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func (h *Handler) initBrowserFlow(w http.ResponseWriter, r *http.Request, ps htt

// nolint:deadcode,unused
// swagger:parameters getSelfServiceLoginFlow
type getSelfServiceBrowserLoginRequestParameters struct {
type getSelfServiceLoginFlowParameters struct {
// The Login Flow ID
//
// The value for this parameter comes from `flow` URL Query parameter sent to your
Expand Down Expand Up @@ -234,7 +234,7 @@ type getSelfServiceBrowserLoginRequestParameters struct {
// 404: genericError
// 410: genericError
// 500: genericError
func (h *Handler) fetchLoginRequest(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func (h *Handler) fetchLoginFlow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ar, err := h.d.LoginFlowPersister().GetLoginFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("id")))
if err != nil {
h.d.Writer().WriteError(w, r, err)
Expand Down
10 changes: 5 additions & 5 deletions selfservice/flow/login/handler_test.go
Expand Up @@ -34,7 +34,7 @@ func TestInitFlow(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
router := x.NewRouterPublic()
ts, _ := testhelpers.NewKratosServerWithRouters(t, reg, router, x.NewRouterAdmin())
loginTS := testhelpers.NewLoginUIRequestEchoServer(t, reg)
loginTS := testhelpers.NewLoginUIFlowEchoServer(t, reg)

viper.Set(configuration.ViperKeySelfServiceBrowserDefaultReturnTo, "https://www.ory.sh")
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/login.schema.json")
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestGetFlow(t *testing.T) {
}))
}

assertRequestPayload := func(t *testing.T, body []byte) {
assertFlowPayload := func(t *testing.T, body []byte) {
assert.Equal(t, "password", gjson.GetBytes(body, "methods.password.method").String(), "%s", body)
assert.NotEmpty(t, gjson.GetBytes(body, "methods.password.config.fields.#(name==csrf_token).value").String(), "%s", body)
assert.NotEmpty(t, gjson.GetBytes(body, "id").String(), "%s", body)
Expand All @@ -168,7 +168,7 @@ func TestGetFlow(t *testing.T) {
assert.Equal(t, public.URL+login.RouteInitBrowserFlow, gjson.GetBytes(body, "error.details.redirect_to").String(), "%s", body)
}

newExpiredRequest := func() *login.Flow {
newExpiredFlow := func() *login.Flow {
return &login.Flow{
ID: x.NewUUID(),
ExpiresAt: time.Now().Add(-time.Minute),
Expand All @@ -186,11 +186,11 @@ func TestGetFlow(t *testing.T) {
"enabled": true})

t.Run("case=valid", func(t *testing.T) {
assertRequestPayload(t, x.EasyGetBody(t, endpoint.Client(), public.URL+login.RouteInitBrowserFlow))
assertFlowPayload(t, x.EasyGetBody(t, endpoint.Client(), public.URL+login.RouteInitBrowserFlow))
})

t.Run("case=expired", func(t *testing.T) {
lr := newExpiredRequest()
lr := newExpiredFlow()
require.NoError(t, reg.LoginFlowPersister().CreateLoginFlow(context.Background(), lr))
res, body := x.EasyGet(t, admin.Client(), endpoint.URL+login.RouteGetFlow+"?id="+lr.ID.String())
assertExpiredPayload(t, res, body)
Expand Down
4 changes: 2 additions & 2 deletions selfservice/flow/login/hook.go
Expand Up @@ -82,8 +82,8 @@ func (e *HookExecutor) PostLoginHook(w http.ResponseWriter, r *http.Request, ct

for _, executor := range e.d.PostLoginHooks(ct) {
if err := executor.ExecuteLoginPostHook(w, r, a, s); err != nil {
if errors.Is(err, ErrHookAbortRequest) {
e.d.Logger().Warn("A successful login attempt was aborted because a hook returned ErrHookAbortRequest.")
if errors.Is(err, ErrHookAbortFlow) {
e.d.Logger().Warn("A successful login attempt was aborted because a hook returned ErrHookAbortFlow.")
return nil
}
return err
Expand Down
14 changes: 7 additions & 7 deletions selfservice/flow/login/persistence.go
Expand Up @@ -23,7 +23,7 @@ type (
UpdateLoginFlowMethod(context.Context, uuid.UUID, identity.CredentialsType, *FlowMethod) error
ForceLoginFlow(ctx context.Context, id uuid.UUID) error
}
RequestPersistenceProvider interface {
FlowPersistenceProvider interface {
LoginFlowPersister() FlowPersister
}
)
Expand All @@ -42,7 +42,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
require.Error(t, err)
})

var newRequest = func(t *testing.T) *Flow {
var newFlow = func(t *testing.T) *Flow {
var r Flow
require.NoError(t, faker.FakeData(&r))
clearids(&r)
Expand All @@ -60,7 +60,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
})

t.Run("case=should create a new login flow and properly set IDs", func(t *testing.T) {
r := newRequest(t)
r := newFlow(t)
methods := len(r.Methods)
err := p.CreateLoginFlow(context.Background(), r)
require.NoError(t, err, "%#v", err)
Expand All @@ -74,7 +74,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
})

t.Run("case=should create and fetch a login flow", func(t *testing.T) {
expected := newRequest(t)
expected := newFlow(t)
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)

Expand All @@ -91,7 +91,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
})

t.Run("case=should properly set the flow type", func(t *testing.T) {
expected := newRequest(t)
expected := newFlow(t)
expected.Forced = true
expected.Type = flow.TypeAPI
expected.Methods = map[identity.CredentialsType]*FlowMethod{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
})

t.Run("case=should properly update a flow", func(t *testing.T) {
expected := newRequest(t)
expected := newFlow(t)
expected.Type = flow.TypeAPI
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
Expand All @@ -142,7 +142,7 @@ func TestFlowPersister(p FlowPersister) func(t *testing.T) {
})

t.Run("case=should update a login flow", func(t *testing.T) {
expected := newRequest(t)
expected := newFlow(t)
delete(expected.Methods, identity.CredentialsTypeOIDC)
err := p.CreateLoginFlow(context.Background(), expected)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/recovery/handler_test.go
Expand Up @@ -51,7 +51,7 @@ func TestRecoveryHandler(t *testing.T) {
testhelpers.RecoveryFlowEnable(true)

testhelpers.NewRedirTS(t, "")
testhelpers.NewLoginUIRequestEchoServer(t, reg)
testhelpers.NewLoginUIFlowEchoServer(t, reg)
testhelpers.NewErrorTestServer(t, reg)

public, admin := testhelpers.NewKratosServerWithCSRF(t, reg)
Expand Down
4 changes: 2 additions & 2 deletions selfservice/hook/error.go
Expand Up @@ -49,11 +49,11 @@ func (e Error) ExecuteSettingsPostPersistHook(w http.ResponseWriter, r *http.Req
}

func (e Error) ExecuteLoginPostHook(w http.ResponseWriter, r *http.Request, a *login.Flow, s *session.Session) error {
return e.err("ExecuteLoginPostHook", login.ErrHookAbortRequest)
return e.err("ExecuteLoginPostHook", login.ErrHookAbortFlow)
}

func (e Error) ExecuteLoginPreHook(w http.ResponseWriter, r *http.Request, a *login.Flow) error {
return e.err("ExecuteLoginPreHook", login.ErrHookAbortRequest)
return e.err("ExecuteLoginPreHook", login.ErrHookAbortFlow)
}

func (e Error) ExecuteRegistrationPreHook(w http.ResponseWriter, r *http.Request, a *registration.Flow) error {
Expand Down
4 changes: 2 additions & 2 deletions selfservice/hook/redirector.go
Expand Up @@ -43,7 +43,7 @@ func (e *Redirector) ExecuteLoginPreHook(w http.ResponseWriter, r *http.Request,
if err := e.do(w, r); err != nil {
return err
}
return errors.WithStack(login.ErrHookAbortRequest)
return errors.WithStack(login.ErrHookAbortFlow)
}

func (e *Redirector) ExecuteRegistrationPreHook(w http.ResponseWriter, r *http.Request, _ *registration.Flow) error {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (e *Redirector) ExecuteLoginPostHook(w http.ResponseWriter, r *http.Request
if err := e.do(w, r); err != nil {
return err
}
return errors.WithStack(login.ErrHookAbortRequest)
return errors.WithStack(login.ErrHookAbortFlow)
}

func (e *Redirector) do(w http.ResponseWriter, r *http.Request) error {
Expand Down
4 changes: 2 additions & 2 deletions selfservice/hook/redirector_test.go
Expand Up @@ -30,7 +30,7 @@ func TestRedirector(t *testing.T) {
require.Error(t, l.ExecuteSettingsPrePersistHook(w, r, nil, nil), settings.ErrHookAbortRequest)
})
router.GET("/b", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
require.Error(t, l.ExecuteLoginPostHook(w, r, nil, nil), login.ErrHookAbortRequest)
require.Error(t, l.ExecuteLoginPostHook(w, r, nil, nil), login.ErrHookAbortFlow)
})
router.GET("/c", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
require.Error(t, l.ExecutePostRegistrationPrePersistHook(w, r, nil, nil), registration.ErrHookAbortRequest)
Expand All @@ -42,7 +42,7 @@ func TestRedirector(t *testing.T) {
require.Error(t, l.ExecutePostRegistrationPrePersistHook(w, r, nil, nil), registration.ErrHookAbortRequest)
})
router.GET("/f", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
require.Error(t, l.ExecuteLoginPreHook(w, r, nil), login.ErrHookAbortRequest)
require.Error(t, l.ExecuteLoginPreHook(w, r, nil), login.ErrHookAbortFlow)
})
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/strategy.go
Expand Up @@ -58,7 +58,7 @@ type dependencies interface {
session.HandlerProvider

login.HookExecutorProvider
login.RequestPersistenceProvider
login.FlowPersistenceProvider
login.HooksProvider
login.StrategyProvider
login.HandlerProvider
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/password/login_test.go
Expand Up @@ -89,7 +89,7 @@ func TestCompleteLogin(t *testing.T) {
ts, _ := testhelpers.NewKratosServer(t, reg)

errTs := testhelpers.NewErrorTestServer(t, reg)
uiTs := testhelpers.NewLoginUIRequestEchoServer(t, reg)
uiTs := testhelpers.NewLoginUIFlowEchoServer(t, reg)
newReturnTs(t, reg)

// Overwrite these two:
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/password/strategy.go
Expand Up @@ -45,7 +45,7 @@ type registrationStrategyDependencies interface {
login.HooksProvider
login.ErrorHandlerProvider
login.HookExecutorProvider
login.RequestPersistenceProvider
login.FlowPersistenceProvider
login.HandlerProvider

settings.RequestPersistenceProvider
Expand Down

0 comments on commit 1b3c491

Please sign in to comment.