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 Aug 26, 2024
1 parent b64840e commit 0c6372a
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 @@ -853,7 +853,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 @@ -63,6 +63,7 @@ type (
}
HookExecutor struct {
d executorDependencies
c *claims.Claims
}
HookExecutorProvider interface {
LoginHookExecutor() *HookExecutor
Expand Down Expand Up @@ -119,15 +120,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 @@ -168,13 +177,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 @@ -178,7 +178,7 @@ func (s *Strategy) processLogin(w http.ResponseWriter, r *http.Request, loginFlo
httprouter.ParamsFromContext(r.Context()).ByName("organization"))
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(w, r, loginFlow, provider.Config().ID, nil, err)
}
return nil, nil
Expand Down

0 comments on commit 0c6372a

Please sign in to comment.