-
Notifications
You must be signed in to change notification settings - Fork 27
/
prompt_create_passkey.go
103 lines (84 loc) · 3.14 KB
/
prompt_create_passkey.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
package authflowv2
import (
"encoding/json"
"net/http"
handlerwebapp "github.com/authgear/authgear-server/pkg/auth/handler/webapp"
"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/authenticationflow/declarative"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/template"
)
var TemplateWebAuthflowPromptCreatePasskeyHTML = template.RegisterHTML(
"web/authflowv2/prompt_create_passkey.html",
handlerwebapp.Components...,
)
func ConfigureAuthflowV2PromptCreatePasskeyRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "POST", "GET").
WithPathPattern(AuthflowV2RoutePromptCreatePasskey)
}
type AuthflowV2PromptCreatePasskeyViewModel struct {
CreationOptionsJSON string
}
type AuthflowV2PromptCreatePasskeyHandler struct {
Controller *handlerwebapp.AuthflowController
BaseViewModel *viewmodels.BaseViewModeler
Renderer handlerwebapp.Renderer
}
func (h *AuthflowV2PromptCreatePasskeyHandler) GetData(w http.ResponseWriter, r *http.Request, s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) (map[string]interface{}, error) {
data := make(map[string]interface{})
baseViewModel := h.BaseViewModel.ViewModelForAuthFlow(r, w)
viewmodels.Embed(data, baseViewModel)
screenData := screen.StateTokenFlowResponse.Action.Data.(declarative.NodePromptCreatePasskeyData)
creationOptionsJSONBytes, err := json.Marshal(screenData.CreationOptions)
if err != nil {
return nil, err
}
creationOptionsJSON := string(creationOptionsJSONBytes)
screenViewModel := AuthflowV2PromptCreatePasskeyViewModel{
CreationOptionsJSON: creationOptionsJSON,
}
viewmodels.Embed(data, screenViewModel)
return data, nil
}
func (h *AuthflowV2PromptCreatePasskeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var handlers handlerwebapp.AuthflowControllerHandlers
handlers.Get(func(s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) error {
data, err := h.GetData(w, r, s, screen)
if err != nil {
return err
}
h.Renderer.RenderHTML(w, r, TemplateWebAuthflowPromptCreatePasskeyHTML, data)
return nil
})
handlers.PostAction("skip", func(s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) error {
input := map[string]interface{}{
"skip": true,
}
result, err := h.Controller.AdvanceWithInput(r, s, screen, input, nil)
if err != nil {
return err
}
result.WriteResponse(w, r)
return nil
})
handlers.PostAction("", func(s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) error {
attestationResponseStr := r.Form.Get("x_attestation_response")
var creationResponseJSON interface{}
err := json.Unmarshal([]byte(attestationResponseStr), &creationResponseJSON)
if err != nil {
return err
}
input := map[string]interface{}{
"creation_response": creationResponseJSON,
}
result, err := h.Controller.AdvanceWithInput(r, s, screen, input, nil)
if err != nil {
return err
}
result.WriteResponse(w, r)
return nil
})
h.Controller.HandleStep(w, r, &handlers)
}