Repository navigation
Expand file tree
/
Copy pathpasskey_http.go
More file actions
180 lines (167 loc) · 5.36 KB
/
Copy pathpasskey_http.go
File metadata and controls
180 lines (167 loc) · 5.36 KB
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
package authkit
import (
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/goat-io/authkit/identity"
)
type passkeyFlow struct {
UserID string
Session []byte
}
const passkeyFlowTTL = 5 * time.Minute
func (a *Auth) beginPasskeyRegistration(w http.ResponseWriter, r *http.Request) {
p, ok := a.principal(r)
if !ok || p.Kind != identity.KindUser {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
user, err := a.config.Storage.Users.GetByID(r.Context(), p.Subject)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
options, ceremony, err := a.Passkeys.BeginRegistration(r.Context(), user)
if err != nil {
http.Error(w, "unable to start registration", http.StatusBadRequest)
return
}
if err := a.savePasskeyFlow(w, r, "register", passkeyFlow{UserID: user.ID, Session: ceremony}); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
writeOptions(w, options)
}
func (a *Auth) finishPasskeyRegistration(w http.ResponseWriter, r *http.Request) {
flow, ok := a.takePasskeyFlow(w, r, "register")
if !ok {
return
}
p, authenticated := a.principal(r)
if !authenticated || p.Kind != identity.KindUser || p.Subject != flow.UserID {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
response, ok := readCeremonyResponse(w, r)
if !ok {
return
}
user, err := a.config.Storage.Users.GetByID(r.Context(), flow.UserID)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if err := a.Passkeys.FinishRegistration(r.Context(), user, flow.Session, response); err != nil {
http.Error(w, "invalid passkey response", http.StatusBadRequest)
return
}
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
}
func (a *Auth) beginPasskeySignIn(w http.ResponseWriter, r *http.Request) {
var body struct {
Email string `json:"email"`
}
if !readJSON(w, r, &body) {
return
}
user, err := a.config.Storage.Users.GetByEmail(r.Context(), strings.ToLower(strings.TrimSpace(body.Email)))
if err != nil {
http.Error(w, "unable to start sign-in", http.StatusUnauthorized)
return
}
options, ceremony, err := a.Passkeys.BeginLogin(r.Context(), user)
if err != nil {
http.Error(w, "unable to start sign-in", http.StatusUnauthorized)
return
}
if err := a.savePasskeyFlow(w, r, "sign-in", passkeyFlow{UserID: user.ID, Session: ceremony}); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
writeOptions(w, options)
}
func (a *Auth) finishPasskeySignIn(w http.ResponseWriter, r *http.Request) {
flow, ok := a.takePasskeyFlow(w, r, "sign-in")
if !ok {
return
}
response, ok := readCeremonyResponse(w, r)
if !ok {
return
}
user, err := a.config.Storage.Users.GetByID(r.Context(), flow.UserID)
if err != nil {
http.Error(w, "invalid passkey response", http.StatusBadRequest)
return
}
userID, err := a.Passkeys.FinishLogin(r.Context(), user, flow.Session, response)
if err != nil || userID != flow.UserID {
http.Error(w, "invalid passkey response", http.StatusUnauthorized)
return
}
mfaRequired, err := a.issueSessionOrChallenge(w, r, userID)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
if mfaRequired {
writeJSON(w, http.StatusOK, map[string]any{"mfaRequired": true})
return
}
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
}
func (a *Auth) savePasskeyFlow(w http.ResponseWriter, r *http.Request, kind string, flow passkeyFlow) error {
payload, err := json.Marshal(flow)
if err != nil {
return err
}
id, err := randomID("flow_")
if err != nil {
return err
}
if err := a.config.Storage.Flows.Put(r.Context(), id, "passkey:"+kind, payload, time.Now().Add(passkeyFlowTTL)); err != nil {
return err
}
a.setPasskeyCookie(w, kind, id, int(passkeyFlowTTL.Seconds()))
return nil
}
func (a *Auth) takePasskeyFlow(w http.ResponseWriter, r *http.Request, kind string) (passkeyFlow, bool) {
var flow passkeyFlow
cookie, err := r.Cookie("authkit.passkey." + kind)
a.setPasskeyCookie(w, kind, "", -1)
if err != nil {
http.Error(w, "missing passkey challenge", http.StatusBadRequest)
return flow, false
}
payload, found, err := a.config.Storage.Flows.Take(r.Context(), cookie.Value, "passkey:"+kind)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return flow, false
}
if !found || json.Unmarshal(payload, &flow) != nil || flow.UserID == "" || len(flow.Session) == 0 {
http.Error(w, "invalid passkey challenge", http.StatusBadRequest)
return flow, false
}
return flow, true
}
func (a *Auth) setPasskeyCookie(w http.ResponseWriter, kind, value string, age int) {
http.SetCookie(w, &http.Cookie{Name: "authkit.passkey." + kind, Value: value,
Path: a.basePath + "/passkey/", MaxAge: age, HttpOnly: true, Secure: a.secure, SameSite: http.SameSiteLaxMode})
}
func readCeremonyResponse(w http.ResponseWriter, r *http.Request) ([]byte, bool) {
r.Body = http.MaxBytesReader(w, r.Body, 256<<10)
b, err := io.ReadAll(r.Body)
if err != nil || len(b) == 0 || !json.Valid(b) {
http.Error(w, "invalid passkey response", http.StatusBadRequest)
return nil, false
}
return b, true
}
func writeOptions(w http.ResponseWriter, options []byte) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(options)
}