Skip to content

Commit

Permalink
fix: always issue session after verification after registration
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Aug 30, 2023
1 parent dda19e8 commit e505f04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion selfservice/flow/registration/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func (e *HookExecutor) PostRegistrationHook(w http.ResponseWriter, r *http.Reque
return err
}

if err != nil {
// We persist the session here so that subsequent hooks (like verification) can use it.
if err := e.d.SessionPersister().UpsertSession(r.Context(), s); err != nil {
return err
}

Expand Down
14 changes: 14 additions & 0 deletions selfservice/flow/verification/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,20 @@ func (h *Handler) updateVerificationFlow(w http.ResponseWriter, r *http.Request,
return
}

if _, err = h.d.SessionManager().FetchFromRequest(ctx, r); errors.As(err, session.NewErrNoActiveSessionFound()) {
// No session was set yet for this request, but we need a session for the consent UI.
sess, err := h.d.SessionPersister().GetSession(ctx, f.SessionID.UUID, session.ExpandDefault)
if err != nil {
h.d.VerificationFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
err = h.d.SessionManager().IssueCookie(ctx, w, r, sess)
if err != nil {
h.d.VerificationFlowErrorHandler().WriteFlowError(w, r, f, node.DefaultGroup, err)
return
}
}

http.Redirect(w, r, callbackURL, http.StatusSeeOther)
return
}
Expand Down
10 changes: 5 additions & 5 deletions selfservice/flow/verification/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/hydra"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/kratos/selfservice/flow/verification"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
)

Expand Down Expand Up @@ -216,16 +214,18 @@ func TestPostFlow(t *testing.T) {

t.Run("suite=with OIDC login challenge", func(t *testing.T) {
t.Run("case=succeeds with a session", func(t *testing.T) {
s := testhelpers.CreateSession(t, reg)

f := &verification.Flow{
ID: uuid.Must(uuid.NewV4()),
Type: "browser",
ExpiresAt: time.Now().Add(1 * time.Hour),
IssuedAt: time.Now(),
OAuth2LoginChallenge: hydra.FakeValidLoginChallenge,
OAuth2LoginChallengeParams: verification.OAuth2LoginChallengeParams{
SessionID: uuid.NullUUID{UUID: uuid.Must(uuid.NewV4()), Valid: true},
IdentityID: uuid.NullUUID{UUID: uuid.Must(uuid.NewV4()), Valid: true},
AMR: session.AuthenticationMethods{{Method: identity.CredentialsTypePassword}},
SessionID: uuid.NullUUID{UUID: s.ID, Valid: true},
IdentityID: uuid.NullUUID{UUID: s.IdentityID, Valid: true},
AMR: s.AMR,
},
}
require.NoError(t, reg.VerificationFlowPersister().CreateVerificationFlow(ctx, f))
Expand Down

0 comments on commit e505f04

Please sign in to comment.