-
Notifications
You must be signed in to change notification settings - Fork 28
/
verify_identity.go
223 lines (187 loc) · 6.02 KB
/
verify_identity.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package webapp
import (
"net/http"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels"
"github.com/authgear/authgear-server/pkg/auth/webapp"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/authn/otp"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/mail"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/phone"
"github.com/authgear/authgear-server/pkg/util/template"
"github.com/authgear/authgear-server/pkg/util/validation"
)
var TemplateWebVerifyIdentityHTML = template.RegisterHTML(
"web/verify_identity.html",
Components...,
)
var VerifyIdentitySchema = validation.NewSimpleSchema(`
{
"type": "object",
"properties": {
"x_verification_code": {
"type": "string",
"format": "x_verification_code"
}
},
"required": ["x_verification_code"]
}
`)
func ConfigureVerifyIdentityRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "POST", "GET").
WithPathPattern("/flows/verify_identity")
}
const (
VerifyIdentityActionSubmit = "submit"
)
type VerifyIdentityViewModel struct {
VerificationCode string
VerificationCodeSendCooldown int
VerificationCodeLength int
VerificationCodeChannel string
FailedAttemptRateLimitExceeded bool
IdentityDisplayID string
Action string
}
type FlashMessage interface {
Flash(rw http.ResponseWriter, messageType string)
}
type VerifyIdentityHandler struct {
ControllerFactory ControllerFactory
BaseViewModel *viewmodels.BaseViewModeler
Renderer Renderer
FlashMessage FlashMessage
OTPCodeService OTPCodeService
Clock clock.Clock
Config *config.AppConfig
}
type VerifyIdentityNode interface {
GetVerificationIdentity() *identity.Info
GetVerificationCodeChannel() string
GetVerificationCodeTarget() string
GetVerificationCodeLength() int
GetRequestedByUser() bool
}
func (h *VerifyIdentityHandler) GetData(r *http.Request, rw http.ResponseWriter, action string, maybeSession *webapp.Session, graph *interaction.Graph) (map[string]interface{}, error) {
data := map[string]interface{}{}
baseViewModel := h.BaseViewModel.ViewModel(r, rw)
code := r.Form.Get("code")
if code == "" && maybeSession != nil {
step := maybeSession.CurrentStep()
if c, ok := step.FormData["x_verification_code"].(string); ok {
code = c
}
}
viewModel := VerifyIdentityViewModel{
VerificationCode: code,
Action: action,
}
var n VerifyIdentityNode
if graph.FindLastNode(&n) {
rawIdentityDisplayID := n.GetVerificationIdentity().DisplayID()
viewModel.VerificationCodeLength = n.GetVerificationCodeLength()
viewModel.VerificationCodeChannel = n.GetVerificationCodeChannel()
target := n.GetVerificationCodeTarget()
channel := model.AuthenticatorOOBChannel(viewModel.VerificationCodeChannel)
switch channel {
case model.AuthenticatorOOBChannelSMS:
viewModel.IdentityDisplayID = phone.Mask(rawIdentityDisplayID)
case model.AuthenticatorOOBChannelEmail:
viewModel.IdentityDisplayID = mail.MaskAddress(rawIdentityDisplayID)
default:
panic("webapp: unknown verification channel")
}
state, err := h.OTPCodeService.InspectState(otp.KindVerification(h.Config, channel), target)
if err != nil {
return nil, err
}
viewModel.FailedAttemptRateLimitExceeded = state.TooManyAttempts
cooldown := int(state.CanResendAt.Sub(h.Clock.NowUTC()).Seconds())
if cooldown < 0 {
viewModel.VerificationCodeSendCooldown = 0
} else {
viewModel.VerificationCodeSendCooldown = cooldown
}
}
phoneOTPAlternatives := viewmodels.PhoneOTPAlternativeStepsViewModel{}
if err := phoneOTPAlternatives.AddAlternatives(graph, webapp.SessionStepVerifyIdentityViaOOBOTP); err != nil {
return nil, err
}
viewmodels.Embed(data, baseViewModel)
viewmodels.Embed(data, viewModel)
viewmodels.Embed(data, phoneOTPAlternatives)
return data, nil
}
func (h *VerifyIdentityHandler) GetErrorData(r *http.Request, rw http.ResponseWriter, err error) (map[string]interface{}, error) {
data := map[string]interface{}{}
baseViewModel := h.BaseViewModel.ViewModel(r, rw)
baseViewModel.SetError(err)
viewModel := VerifyIdentityViewModel{}
viewmodels.Embed(data, baseViewModel)
viewmodels.Embed(data, viewModel)
return data, nil
}
func (h *VerifyIdentityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctrl, err := h.ControllerFactory.New(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer ctrl.Serve()
inputFn := func() (input interface{}, err error) {
err = VerifyIdentitySchema.Validator().ValidateValue(FormToJSON(r.Form))
if err != nil {
return
}
code := r.Form.Get("x_verification_code")
input = &InputVerificationCode{
Code: code,
}
return
}
ctrl.Get(func() error {
// This page should be opened by the original user agent.
session, err := ctrl.InteractionSession()
if err != nil {
return err
}
graph, err := ctrl.InteractionGet()
if err != nil {
return err
}
data, err := h.GetData(r, w, VerifyIdentityActionSubmit, session, graph)
if err != nil {
return err
}
h.Renderer.RenderHTML(w, r, TemplateWebVerifyIdentityHTML, data)
return nil
})
ctrl.PostAction("resend", func() error {
result, err := ctrl.InteractionPost(func() (input interface{}, err error) {
input = &InputResendCode{}
return
})
if err != nil {
return err
}
if !result.IsInteractionErr {
h.FlashMessage.Flash(w, string(webapp.FlashMessageTypeResendCodeSuccess))
}
result.WriteResponse(w, r)
return nil
})
ctrl.PostAction(VerifyIdentityActionSubmit, func() error {
result, err := ctrl.InteractionPost(inputFn)
if err != nil {
return err
}
result.WriteResponse(w, r)
return nil
})
handleAlternativeSteps(ctrl)
}