generated from datumforge/go-template
/
webauthn.go
384 lines (312 loc) · 13.1 KB
/
webauthn.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package handlers
import (
"context"
"net/http"
echo "github.com/datumforge/echox"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/datumforge/datum/internal/ent/enums"
"github.com/datumforge/datum/internal/ent/privacy/token"
"github.com/datumforge/datum/internal/ent/privacy/viewer"
"github.com/datumforge/datum/pkg/auth"
provider "github.com/datumforge/datum/pkg/providers/webauthn"
"github.com/datumforge/datum/pkg/rout"
"github.com/datumforge/datum/pkg/sessions"
)
const (
webauthnProvider = "WEBAUTHN"
webauthnRegistration = "WEBAUTHN_REGISTRATION"
webauthnLogin = "WEBAUTHN_LOGIN"
)
// WebauthnRegistrationRequest is the request to begin a webauthn login
type WebauthnRegistrationRequest struct {
Email string `json:"email"`
Name string `json:"name"`
}
// WebauthnRegistrationResponse is the response to begin a webauthn login
// this includes the credential creation options and the session token
type WebauthnBeginRegistrationResponse struct {
Reply rout.Reply
*protocol.CredentialCreation
Session string `json:"session,omitempty"`
}
// WebauthnRegistrationResponse is the response after a successful webauthn registration
type WebauthnRegistrationResponse struct {
rout.Reply
Message string `json:"message,omitempty"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token,omitempty"`
Session string `json:"session,omitempty"`
TokenType string `json:"token_type"`
}
// WebauthnBeginLoginResponse is the response to begin a webauthn login
// this includes the credential assertion options and the session token
type WebauthnBeginLoginResponse struct {
Reply rout.Reply
*protocol.CredentialAssertion
Session string `json:"session,omitempty"`
}
// WebauthnRegistrationResponse is the response after a successful webauthn login
type WebauthnLoginResponse struct {
rout.Reply
Message string `json:"message,omitempty"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token,omitempty"`
Session string `json:"session,omitempty"`
TokenType string `json:"token_type"`
}
// BeginWebauthnRegistration is the request to begin a webauthn login
func (h *Handler) BeginWebauthnRegistration(ctx echo.Context) error {
var r WebauthnRegistrationRequest
if err := ctx.Bind(&r); err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
ctxWithToken := token.NewContextWithOauthTooToken(ctx.Request().Context(), r.Email)
// to register a new passkey, the user needs to be created + logged in first
// once the the passkey is added to the user's account, they can use it to login
// we treat this verify similar to the oauth or basic registration flow
// user is created first, no credential method is set / they are unable to login until the credential flow is finished
entUser, err := h.CheckAndCreateUser(ctxWithToken, r.Name, r.Email, enums.AuthProvider(webauthnProvider))
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// set context for remaining request based on logged in user
userCtx := viewer.NewContext(ctxWithToken, viewer.NewUserViewerFromID(entUser.ID, true))
// set webauthn allowed
if err := h.setWebauthnAllowed(userCtx, entUser); err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
if err := h.addDefaultOrgToUserQuery(userCtx, entUser); err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
user := &provider.User{
ID: entUser.ID,
Email: entUser.Email,
Name: entUser.FirstName + " " + entUser.LastName,
}
// options is the object that needs to be returned for the front end to open the creation dialog for the user to create the passkey
options, session, err := h.WebAuthn.BeginRegistration(user,
webauthn.WithResidentKeyRequirement(protocol.ResidentKeyRequirementRequired),
webauthn.WithExclusions(user.CredentialExcludeList()),
)
if err != nil {
return err
}
// we have to set not just a regular datum session for the user but also capture the return of the webauthn session
setSessionMap := map[string]any{}
setSessionMap[sessions.WebAuthnKey] = session
setSessionMap[sessions.UsernameKey] = r.Name
setSessionMap[sessions.UserTypeKey] = webauthnRegistration
setSessionMap[sessions.EmailKey] = r.Email
setSessionMap[sessions.UserIDKey] = user.ID
sessionCtx, err := h.SessionConfig.SaveAndStoreSession(userCtx, ctx.Response().Writer, setSessionMap, user.ID)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// return the session value for the UI to use
// the UI will need to set the cookie because authentication is handled
// server side
s, err := sessions.SessionToken(sessionCtx)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
out := &WebauthnBeginRegistrationResponse{
Reply: rout.Reply{Success: true},
CredentialCreation: options,
Session: s,
}
return ctx.JSON(http.StatusOK, out)
}
// FinishWebauthnRegistration is the request to finish a webauthn registration - this is where we get the credential created by the user back
func (h *Handler) FinishWebauthnRegistration(ctx echo.Context) error {
// lookup userID in cache to ensure cookie and tokens match
session, err := h.SessionConfig.SessionManager.Get(ctx.Request(), h.SessionConfig.CookieConfig.Name)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// Get sessionID from cookie and check against redis
sessionID := h.SessionConfig.SessionManager.GetSessionIDFromCookie(session)
userID, err := h.SessionConfig.RedisStore.GetSession(ctx.Request().Context(), sessionID)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// get session data from cookie to get the user id stored
sessionData := h.SessionConfig.SessionManager.GetSessionDataFromCookie(session)
userIDFromCookie := sessionData.(map[string]any)[sessions.UserIDKey]
// ensure the user is the same as the one who started the registration
if userIDFromCookie != userID {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// get user from the database
entUser, err := h.getUserByID(ctx.Request().Context(), userID, enums.AuthProvider(webauthnProvider))
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// set user in the viewer context for the rest of the request
userCtx := viewer.NewContext(ctx.Request().Context(), viewer.NewUserViewerFromID(entUser.ID, true))
// follows https://www.w3.org/TR/webauthn/#sctn-registering-a-new-credential
response, err := protocol.ParseCredentialCreationResponseBody(ctx.Request().Body)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// get webauthn session data from the session
webauthnData := sessionData.(map[string]any)[sessions.WebAuthnKey]
wd, ok := webauthnData.(webauthn.SessionData)
if !ok {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(ErrNoAuthUser))
}
user := &provider.User{
ID: entUser.ID,
Email: entUser.Email,
Name: entUser.FirstName + " " + entUser.LastName,
}
// validate the credential
credential, err := h.WebAuthn.CreateCredential(user, wd, response)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// save the credential to the database
if err := h.addCredentialToUser(userCtx, entUser, *credential); err != nil {
if IsConstraintError(err) {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(ErrDeviceAlreadyRegistered))
}
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// create claims for verified user
claims := createClaims(entUser)
access, refresh, err := h.TM.CreateTokenPair(claims)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// set cookies for the user
auth.SetAuthCookies(ctx.Response().Writer, access, refresh)
// set sessions in response
if err := h.SessionConfig.CreateAndStoreSession(ctx, user.ID); err != nil {
h.Logger.Errorw("unable to save session", "error", err)
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// return the session value for the UI to use
// the UI will need to set the cookie because authentication is handled
// server side
s, err := sessions.SessionToken(ctx.Request().Context())
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
out := &WebauthnRegistrationResponse{
Reply: rout.Reply{Success: true},
Message: "passkey successfully created",
AccessToken: access,
RefreshToken: refresh,
TokenType: "access_token",
Session: s,
}
return ctx.JSON(http.StatusOK, out)
}
// BeginWebauthnLogin is the request to begin a webauthn login
func (h *Handler) BeginWebauthnLogin(ctx echo.Context) error {
credential, session, err := h.WebAuthn.BeginDiscoverableLogin()
if err != nil {
return err
}
setSessionMap := map[string]any{}
setSessionMap[sessions.WebAuthnKey] = session
setSessionMap[sessions.UserTypeKey] = webauthnLogin
sessionCtx, err := h.SessionConfig.SaveAndStoreSession(ctx.Request().Context(), ctx.Response().Writer, setSessionMap, "")
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// return the session value for the UI to use
// the UI will need to set the cookie because authentication is handled
// server side
s, err := sessions.SessionToken(sessionCtx)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
out := &WebauthnBeginLoginResponse{
Reply: rout.Reply{Success: true},
CredentialAssertion: credential,
Session: s,
}
return ctx.JSON(http.StatusOK, out)
}
// FinishWebauthnLogin is the request to finish a webauthn login
func (h *Handler) FinishWebauthnLogin(ctx echo.Context) error {
session, err := h.SessionConfig.SessionManager.Get(ctx.Request(), h.SessionConfig.CookieConfig.Name)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
sessionData := h.SessionConfig.SessionManager.GetSessionDataFromCookie(session)
webauthnData := sessionData.(map[string]any)[sessions.WebAuthnKey]
wd, ok := webauthnData.(webauthn.SessionData)
if !ok {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(ErrNoAuthUser))
}
response, err := protocol.ParseCredentialRequestResponseBody(ctx.Request().Body)
if err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
if _, err = h.WebAuthn.ValidateDiscoverableLogin(h.userHandler(ctx.Request().Context()), wd, response); err != nil {
return ctx.JSON(http.StatusBadRequest, rout.ErrorResponse(err))
}
// get user from the database
entUser, err := h.getUserByID(ctx.Request().Context(), string(response.Response.UserHandle), enums.AuthProvider(webauthnProvider))
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// create claims for verified user
claims := createClaims(entUser)
access, refresh, err := h.TM.CreateTokenPair(claims)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// set cookies for the user
auth.SetAuthCookies(ctx.Response().Writer, access, refresh)
// set sessions in response
if err := h.SessionConfig.CreateAndStoreSession(ctx, entUser.ID); err != nil {
h.Logger.Errorw("unable to save session", "error", err)
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
// return the session value for the UI to use
// the UI will need to set the cookie because authentication is handled
// server side
s, err := sessions.SessionToken(ctx.Request().Context())
if err != nil {
return ctx.JSON(http.StatusInternalServerError, rout.ErrorResponse(err))
}
out := &WebauthnLoginResponse{
Reply: rout.Reply{Success: true},
Message: "passkey successfully created",
AccessToken: access,
RefreshToken: refresh,
TokenType: "access_token",
Session: s,
}
return ctx.JSON(http.StatusOK, out)
}
// userHandler returns a webauthn.DiscoverableUserHandler that can be used to look up a user by their userHandle
func (h *Handler) userHandler(ctx context.Context) webauthn.DiscoverableUserHandler {
return func(rawID, userHandle []byte) (user webauthn.User, err error) {
u, err := h.getUserByID(ctx, string(userHandle), enums.AuthProvider(webauthnProvider))
if err != nil {
return nil, err
}
authnUser := &provider.User{
ID: u.ID,
Email: u.Email,
Name: u.FirstName + " " + u.LastName,
WebauthnCredentials: []webauthn.Credential{},
}
for _, cred := range u.Edges.Webauthn {
authnCred := webauthn.Credential{
ID: cred.CredentialID,
PublicKey: cred.PublicKey,
AttestationType: cred.AttestationType,
}
for _, t := range cred.Transports {
authnCred.Transport = append(authnCred.Transport, protocol.AuthenticatorTransport(t))
}
authnUser.WebauthnCredentials = append(authnUser.WebauthnCredentials, authnCred)
}
return authnUser, nil
}
}