Skip to content

Commit d78923d

Browse files
committed
refactor: use functional options pattern to reduce passing nil
1 parent c45af35 commit d78923d

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

selfservice/flow/login/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ continueLogin:
853853
return
854854
}
855855

856-
if err := h.d.LoginHookExecutor().PostLoginHook(w, r, group, f, i, sess, nil, ""); err != nil {
856+
if err := h.d.LoginHookExecutor().PostLoginHook(w, r, group, f, i, sess, ""); err != nil {
857857
if errors.Is(err, ErrAddressNotVerified) {
858858
h.d.LoginFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, errors.WithStack(schema.NewAddressNotVerifiedError()))
859859
return

selfservice/flow/login/hook.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type (
6363
}
6464
HookExecutor struct {
6565
d executorDependencies
66+
c *claims.Claims
6667
}
6768
HookExecutorProvider interface {
6869
LoginHookExecutor() *HookExecutor
@@ -119,15 +120,23 @@ func (e *HookExecutor) handleLoginError(_ http.ResponseWriter, r *http.Request,
119120
return flowError
120121
}
121122

123+
type PostLoginHookOpt func(*HookExecutor)
124+
125+
func WithClaims(c *claims.Claims) PostLoginHookOpt {
126+
return func(h *HookExecutor) {
127+
h.c = c
128+
}
129+
}
130+
122131
func (e *HookExecutor) PostLoginHook(
123132
w http.ResponseWriter,
124133
r *http.Request,
125134
g node.UiNodeGroup,
126135
f *Flow,
127136
i *identity.Identity,
128137
s *session.Session,
129-
c *claims.Claims,
130138
provider string,
139+
opts ...PostLoginHookOpt,
131140
) (err error) {
132141
ctx := r.Context()
133142
ctx, span := e.d.Tracer(ctx).Tracer().Start(ctx, "HookExecutor.PostLoginHook")
@@ -168,13 +177,17 @@ func (e *HookExecutor) PostLoginHook(
168177
classified := s
169178
s = s.Declassified()
170179

180+
for _, o := range opts {
181+
o(e)
182+
}
183+
171184
e.d.Logger().
172185
WithRequest(r).
173186
WithField("identity_id", i.ID).
174187
WithField("flow_method", f.Active).
175188
Debug("Running ExecuteLoginPostHook.")
176189
for k, executor := range e.d.PostLoginHooks(r.Context(), f.Active) {
177-
if err := executor.ExecuteLoginPostHook(w, r, g, f, s, c); err != nil {
190+
if err := executor.ExecuteLoginPostHook(w, r, g, f, s, e.c); err != nil {
178191
if errors.Is(err, ErrHookAbortFlow) {
179192
e.d.Logger().
180193
WithRequest(r).

selfservice/flow/login/hook_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestLoginExecutor(t *testing.T) {
7272
}
7373

7474
testhelpers.SelfServiceHookLoginErrorHandler(t, w, r,
75-
reg.LoginHookExecutor().PostLoginHook(w, r, strategy.ToUiNodeGroup(), loginFlow, useIdentity, sess, nil, ""))
75+
reg.LoginHookExecutor().PostLoginHook(w, r, strategy.ToUiNodeGroup(), loginFlow, useIdentity, sess, ""))
7676
})
7777

7878
ts := httptest.NewServer(router)

selfservice/strategy/oidc/strategy_login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (s *Strategy) processLogin(w http.ResponseWriter, r *http.Request, loginFlo
178178
httprouter.ParamsFromContext(r.Context()).ByName("organization"))
179179
for _, c := range oidcCredentials.Providers {
180180
if c.Subject == claims.Subject && c.Provider == provider.Config().ID {
181-
if err = s.d.LoginHookExecutor().PostLoginHook(w, r, node.OpenIDConnectGroup, loginFlow, i, sess, claims, provider.Config().ID); err != nil {
181+
if err = s.d.LoginHookExecutor().PostLoginHook(w, r, node.OpenIDConnectGroup, loginFlow, i, sess, provider.Config().ID, login.WithClaims(claims)); err != nil {
182182
return nil, s.handleError(w, r, loginFlow, provider.Config().ID, nil, err)
183183
}
184184
return nil, nil

0 commit comments

Comments
 (0)