/
verification.go
135 lines (112 loc) · 3.89 KB
/
verification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package hook
import (
"context"
"net/http"
"github.com/gofrs/uuid"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/flow/verification"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
"github.com/ory/x/otelx"
)
var (
_ registration.PostHookPostPersistExecutor = new(Verifier)
_ settings.PostHookPostPersistExecutor = new(Verifier)
)
type (
verifierDependencies interface {
config.Provider
x.CSRFTokenGeneratorProvider
x.CSRFProvider
verification.StrategyProvider
verification.FlowPersistenceProvider
identity.PrivilegedPoolProvider
x.WriterProvider
}
Verifier struct {
r verifierDependencies
}
)
func NewVerifier(r verifierDependencies) *Verifier {
return &Verifier{r: r}
}
func (e *Verifier) ExecutePostRegistrationPostPersistHook(w http.ResponseWriter, r *http.Request, f *registration.Flow, s *session.Session) error {
return otelx.WithSpan(r.Context(), "selfservice.hook.Verifier.ExecutePostRegistrationPostPersistHook", func(ctx context.Context) error {
return e.do(w, r.WithContext(ctx), s.Identity, f, func(v *verification.Flow) {
v.OAuth2LoginChallenge = f.OAuth2LoginChallenge
v.SessionID = uuid.NullUUID{UUID: s.ID, Valid: true}
v.IdentityID = uuid.NullUUID{UUID: s.Identity.ID, Valid: true}
v.AMR = s.AMR
})
})
}
func (e *Verifier) ExecuteSettingsPostPersistHook(w http.ResponseWriter, r *http.Request, f *settings.Flow, i *identity.Identity, _ *session.Session) error {
return otelx.WithSpan(r.Context(), "selfservice.hook.Verifier.ExecuteSettingsPostPersistHook", func(ctx context.Context) error {
return e.do(w, r.WithContext(ctx), i, f, nil)
})
}
func (e *Verifier) do(
w http.ResponseWriter,
r *http.Request,
i *identity.Identity,
f flow.FlowWithContinueWith,
flowCallback func(*verification.Flow),
) error {
// This is called after the identity has been created so we can safely assume that all addresses are available
// already.
ctx := r.Context()
strategy, err := e.r.GetActiveVerificationStrategy(ctx)
if err != nil {
return err
}
isBrowserFlow := f.GetType() == flow.TypeBrowser
isRegistrationFlow := f.GetFlowName() == flow.RegistrationFlow
for k := range i.VerifiableAddresses {
address := &i.VerifiableAddresses[k]
if address.Status != identity.VerifiableAddressStatusPending {
continue
}
var csrf string
// TODO: this is pretty ugly, we should probably have a better way to handle CSRF tokens here.
if isBrowserFlow {
if isRegistrationFlow {
// If this hook is executed from a registration flow, we need to regenerate the CSRF token.
csrf = e.r.CSRFHandler().RegenerateToken(w, r)
} else {
// If it came from a settings flow, there already is a CSRF token, so we can just use that.
csrf = e.r.GenerateCSRFToken(r)
}
}
verificationFlow, err := verification.NewPostHookFlow(e.r.Config(),
e.r.Config().SelfServiceFlowVerificationRequestLifespan(ctx),
csrf, r, strategy, f)
if err != nil {
return err
}
if flowCallback != nil {
flowCallback(verificationFlow)
}
verificationFlow.State = flow.StateEmailSent
if err := strategy.PopulateVerificationMethod(r, verificationFlow); err != nil {
return err
}
if err := e.r.VerificationFlowPersister().CreateVerificationFlow(ctx, verificationFlow); err != nil {
return err
}
if err := strategy.SendVerificationEmail(ctx, verificationFlow, i, address); err != nil {
return err
}
flowURL := ""
if verificationFlow.Type == flow.TypeBrowser {
flowURL = verificationFlow.AppendTo(e.r.Config().SelfServiceFlowVerificationUI(ctx)).String()
}
f.AddContinueWith(flow.NewContinueWithVerificationUI(verificationFlow, address.Value, flowURL))
}
return nil
}