Skip to content

Commit

Permalink
refactor: use functional options pattern to reduce passing nil
Browse files Browse the repository at this point in the history
  • Loading branch information
fenech committed Oct 1, 2024
1 parent 42e242b commit 3d6656f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion selfservice/flow/login/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ continueLogin:
return
}

if err := h.d.LoginHookExecutor().PostLoginHook(w, r, group, f, i, sess, nil, ""); err != nil {
if err := h.d.LoginHookExecutor().PostLoginHook(w, r, group, f, i, sess, ""); err != nil {
if errors.Is(err, ErrAddressNotVerified) {
h.d.LoginFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, errors.WithStack(schema.NewAddressNotVerifiedError()))
return
Expand Down
17 changes: 15 additions & 2 deletions selfservice/flow/login/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type (
}
HookExecutor struct {
d executorDependencies
c *claims.Claims
}
HookExecutorProvider interface {
LoginHookExecutor() *HookExecutor
Expand Down Expand Up @@ -120,15 +121,23 @@ func (e *HookExecutor) handleLoginError(_ http.ResponseWriter, r *http.Request,
return flowError
}

type PostLoginHookOpt func(*HookExecutor)

func WithClaims(c *claims.Claims) PostLoginHookOpt {
return func(h *HookExecutor) {
h.c = c
}
}

func (e *HookExecutor) PostLoginHook(
w http.ResponseWriter,
r *http.Request,
g node.UiNodeGroup,
f *Flow,
i *identity.Identity,
s *session.Session,
c *claims.Claims,
provider string,
opts ...PostLoginHookOpt,
) (err error) {
ctx := r.Context()
ctx, span := e.d.Tracer(ctx).Tracer().Start(ctx, "HookExecutor.PostLoginHook")
Expand Down Expand Up @@ -169,13 +178,17 @@ func (e *HookExecutor) PostLoginHook(
classified := s
s = s.Declassified()

for _, o := range opts {
o(e)
}

e.d.Logger().
WithRequest(r).
WithField("identity_id", i.ID).
WithField("flow_method", f.Active).
Debug("Running ExecuteLoginPostHook.")
for k, executor := range e.d.PostLoginHooks(r.Context(), f.Active) {
if err := executor.ExecuteLoginPostHook(w, r, g, f, s, c); err != nil {
if err := executor.ExecuteLoginPostHook(w, r, g, f, s, e.c); err != nil {
if errors.Is(err, ErrHookAbortFlow) {
e.d.Logger().
WithRequest(r).
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/login/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLoginExecutor(t *testing.T) {
}

testhelpers.SelfServiceHookLoginErrorHandler(t, w, r,
reg.LoginHookExecutor().PostLoginHook(w, r, strategy.ToUiNodeGroup(), loginFlow, useIdentity, sess, nil, ""))
reg.LoginHookExecutor().PostLoginHook(w, r, strategy.ToUiNodeGroup(), loginFlow, useIdentity, sess, ""))
})

ts := httptest.NewServer(router)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/strategy_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *Strategy) processLogin(ctx context.Context, w http.ResponseWriter, r *h
sess.CompletedLoginForWithProvider(s.ID(), identity.AuthenticatorAssuranceLevel1, provider.Config().ID, provider.Config().OrganizationID)
for _, c := range oidcCredentials.Providers {
if c.Subject == claims.Subject && c.Provider == provider.Config().ID {
if err = s.d.LoginHookExecutor().PostLoginHook(w, r, node.OpenIDConnectGroup, loginFlow, i, sess, claims, provider.Config().ID); err != nil {
if err = s.d.LoginHookExecutor().PostLoginHook(w, r, node.OpenIDConnectGroup, loginFlow, i, sess, provider.Config().ID, login.WithClaims(claims)); err != nil {
return nil, s.handleError(ctx, w, r, loginFlow, provider.Config().ID, nil, err)
}
return nil, nil
Expand Down

0 comments on commit 3d6656f

Please sign in to comment.