/
setup_oob_otp.go
193 lines (162 loc) · 5.23 KB
/
setup_oob_otp.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
package webapp
import (
"fmt"
"net/http"
"net/url"
"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/interaction"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/template"
"github.com/authgear/authgear-server/pkg/util/validation"
)
var TemplateWebSetupOOBOTPHTML = template.RegisterHTML(
"web/setup_oob_otp.html",
Components...,
)
var SetupOOBOTPEmailSchema = validation.NewSimpleSchema(`
{
"type": "object",
"properties": {
"x_email": { "type": "string" }
},
"required": ["x_email"]
}
`)
var SetupOOBOTPSMSSchema = validation.NewSimpleSchema(`
{
"type": "object",
"properties": {
"x_e164": { "type": "string" }
},
"required": ["x_e164"]
}
`)
func ConfigureSetupOOBOTPRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "POST", "GET").
WithPathPattern("/flows/setup_oob_otp_:channel")
}
type SetupOOBOTPNode interface {
IsOOBAuthenticatorTypeAllowed(oobAuthenticatorType model.AuthenticatorType) (bool, error)
}
type SetupOOBOTPViewModel struct {
// OOBAuthenticatorType is either AuthenticatorTypeOOBSMS or AuthenticatorTypeOOBEmail.
OOBAuthenticatorType model.AuthenticatorType
AlternativeSteps []viewmodels.AlternativeStep
}
func NewSetupOOBOTPViewModel(session *webapp.Session, graph *interaction.Graph, oobAuthenticatorType model.AuthenticatorType, alternatives *viewmodels.AlternativeStepsViewModel) (*SetupOOBOTPViewModel, error) {
var node SetupOOBOTPNode
if !graph.FindLastNode(&node) {
panic("setup_oob_otp: expected graph has node implementing SetupOOBOTPNode")
}
allowedOOBAuthenticatorType, err := node.IsOOBAuthenticatorTypeAllowed(oobAuthenticatorType)
if err != nil {
panic(fmt.Errorf("setup_oob_otp: unexpected error: %w", err))
}
if !allowedOOBAuthenticatorType {
panic(fmt.Errorf("webapp: unexpected oob authenticator type: %s", oobAuthenticatorType))
}
return &SetupOOBOTPViewModel{
OOBAuthenticatorType: oobAuthenticatorType,
AlternativeSteps: alternatives.AlternativeSteps,
}, nil
}
type SetupOOBOTPHandler struct {
ControllerFactory ControllerFactory
BaseViewModel *viewmodels.BaseViewModeler
AlternativeStepsViewModel *viewmodels.AlternativeStepsViewModeler
Renderer Renderer
}
func (h *SetupOOBOTPHandler) GetData(r *http.Request, rw http.ResponseWriter, session *webapp.Session, graph *interaction.Graph, oobAuthenticatorType model.AuthenticatorType) (map[string]interface{}, error) {
var stepKind webapp.SessionStepKind
switch oobAuthenticatorType {
case model.AuthenticatorTypeOOBSMS:
stepKind = webapp.SessionStepSetupOOBOTPSMS
case model.AuthenticatorTypeOOBEmail:
stepKind = webapp.SessionStepSetupOOBOTPEmail
}
data := map[string]interface{}{}
baseViewModel := h.BaseViewModel.ViewModel(r, rw)
alternatives, err := h.AlternativeStepsViewModel.CreateAuthenticatorAlternatives(graph, stepKind)
if err != nil {
return nil, err
}
viewModel, err := NewSetupOOBOTPViewModel(session, graph, oobAuthenticatorType, alternatives)
if err != nil {
return nil, err
}
viewmodels.Embed(data, baseViewModel)
viewmodels.Embed(data, *viewModel)
return data, nil
}
func (h *SetupOOBOTPHandler) 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
}
oc := httproute.GetParam(r, "channel")
oobAuthenticatorType, err := model.GetOOBAuthenticatorType(model.AuthenticatorOOBChannel(oc))
if err != nil {
http.Error(w, "404 page not found", http.StatusNotFound)
return
}
defer ctrl.Serve()
ctrl.Get(func() error {
session, err := ctrl.InteractionSession()
if err != nil {
return err
}
graph, err := ctrl.InteractionGet()
if err != nil {
return err
}
data, err := h.GetData(r, w, session, graph, oobAuthenticatorType)
if err != nil {
return err
}
h.Renderer.RenderHTML(w, r, TemplateWebSetupOOBOTPHTML, data)
return nil
})
ctrl.PostAction("", func() error {
result, err := ctrl.InteractionPost(func() (input interface{}, err error) {
err = GetValidationSchema(oobAuthenticatorType).Validator().ValidateValue(FormToJSON(r.Form))
if err != nil {
return
}
target, inputType, err := FormToOOBTarget(oobAuthenticatorType, r.Form)
input = &InputSetupOOB{
InputType: inputType,
Target: target,
}
return
})
if err != nil {
return err
}
result.WriteResponse(w, r)
return nil
})
handleAlternativeSteps(ctrl)
}
func GetValidationSchema(oobAuthenticatorType model.AuthenticatorType) *validation.SimpleSchema {
switch oobAuthenticatorType {
case model.AuthenticatorTypeOOBEmail:
return SetupOOBOTPEmailSchema
case model.AuthenticatorTypeOOBSMS:
return SetupOOBOTPSMSSchema
}
return nil
}
func FormToOOBTarget(oobAuthenticatorType model.AuthenticatorType, form url.Values) (target string, inputType string, err error) {
if oobAuthenticatorType == model.AuthenticatorTypeOOBSMS {
target = form.Get("x_e164")
inputType = "phone"
return
}
target = form.Get("x_email")
inputType = "email"
return
}