/
use_passkey.go
109 lines (88 loc) · 3.38 KB
/
use_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
104
105
106
107
108
109
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 TemplateWebAuthflowUsePasskeyHTML = template.RegisterHTML(
"web/authflowv2/use_passkey.html",
handlerwebapp.Components...,
)
func ConfigureAuthflowV2UsePasskeyRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "POST", "GET").
WithPathPattern(AuthflowV2RouteUsePasskey)
}
type AuthflowV2UsePasskeyViewModel struct {
PasskeyRequestOptionsJSON string
}
func NewAuthflowV2UsePasskeyViewModel(s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) (*AuthflowV2UsePasskeyViewModel, error) {
index := *screen.Screen.TakenBranchIndex
flowResponse := screen.BranchStateTokenFlowResponse
data := flowResponse.Action.Data.(declarative.StepAuthenticateData)
option := data.Options[index]
requestOptionsJSONBytes, err := json.Marshal(option.RequestOptions)
if err != nil {
return nil, err
}
return &AuthflowV2UsePasskeyViewModel{
PasskeyRequestOptionsJSON: string(requestOptionsJSONBytes),
}, nil
}
type AuthflowV2UsePasskeyHandler struct {
Controller *handlerwebapp.AuthflowController
BaseViewModel *viewmodels.BaseViewModeler
Renderer handlerwebapp.Renderer
}
func (h *AuthflowV2UsePasskeyHandler) 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)
screenViewModel, err := NewAuthflowV2UsePasskeyViewModel(s, screen)
if err != nil {
return nil, err
}
viewmodels.Embed(data, *screenViewModel)
branchViewModel := viewmodels.NewAuthflowBranchViewModel(screen)
viewmodels.Embed(data, branchViewModel)
return data, nil
}
func (h *AuthflowV2UsePasskeyHandler) 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, TemplateWebAuthflowUsePasskeyHTML, data)
return nil
})
handlers.PostAction("", func(s *webapp.Session, screen *webapp.AuthflowScreenWithFlowResponse) error {
assertionResponseStr := r.Form.Get("x_assertion_response")
var assertionResponseJSON interface{}
err := json.Unmarshal([]byte(assertionResponseStr), &assertionResponseJSON)
if err != nil {
return err
}
index := *screen.Screen.TakenBranchIndex
flowResponse := screen.BranchStateTokenFlowResponse
data := flowResponse.Action.Data.(declarative.StepAuthenticateData)
option := data.Options[index]
input := map[string]interface{}{
"authentication": option.Authentication,
"assertion_response": assertionResponseJSON,
}
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)
}