-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathpasskey_authentication.go
More file actions
216 lines (177 loc) · 7.46 KB
/
passkey_authentication.go
File metadata and controls
216 lines (177 loc) · 7.46 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
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
package api
import (
"encoding/json"
"net/http"
"time"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/gofrs/uuid"
"github.com/supabase/auth/internal/api/apierrors"
"github.com/supabase/auth/internal/metering"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/storage"
"github.com/supabase/auth/internal/utilities"
)
// PasskeyAuthenticationOptionsResponse is the response body for POST /passkeys/authentication/options.
type PasskeyAuthenticationOptionsResponse struct {
ChallengeID string `json:"challenge_id"`
Options *protocol.PublicKeyCredentialRequestOptions `json:"options"`
ExpiresAt int64 `json:"expires_at"`
}
// PasskeyAuthenticationVerifyParams is the request body for POST /passkeys/authentication/verify.
type PasskeyAuthenticationVerifyParams struct {
ChallengeID string `json:"challenge_id"`
Credential json.RawMessage `json:"credential"`
}
// PasskeyAuthenticationOptions handles POST /passkeys/authentication/options.
// Generates WebAuthn authentication options for discoverable credential login.
func (a *API) PasskeyAuthenticationOptions(w http.ResponseWriter, r *http.Request) error {
config := a.config
db := a.db.WithContext(r.Context())
webAuthn, err := a.getPasskeyWebAuthn()
if err != nil {
return apierrors.NewInternalServerError("Failed to initialize WebAuthn").WithInternalError(err)
}
// Discoverable flow: empty allowCredentials, no user binding
options, session, err := webAuthn.BeginDiscoverableLogin()
if err != nil {
return apierrors.NewInternalServerError("Failed to generate WebAuthn authentication options").WithInternalError(err)
}
expiresAt := time.Now().Add(config.WebAuthn.ChallengeExpiryDuration)
challenge := models.NewWebAuthnChallenge(
nil, // no user_id for discoverable flow
models.WebAuthnChallengeTypeAuthentication,
&models.WebAuthnSessionData{SessionData: session},
expiresAt,
)
if err := db.Create(challenge); err != nil {
return apierrors.NewInternalServerError("Database error storing challenge").WithInternalError(err)
}
return sendJSON(w, http.StatusOK, &PasskeyAuthenticationOptionsResponse{
ChallengeID: challenge.ID.String(),
Options: &options.Response,
ExpiresAt: expiresAt.Unix(),
})
}
// PasskeyAuthenticationVerify handles POST /passkeys/authentication/verify.
// Validates the WebAuthn assertion and issues tokens for discoverable credential login.
func (a *API) PasskeyAuthenticationVerify(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
config := a.config
db := a.db.WithContext(ctx)
params := &PasskeyAuthenticationVerifyParams{}
body, err := utilities.GetBodyBytes(r)
if err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Could not read request body")
}
if err := json.Unmarshal(body, params); err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Could not parse request body as JSON: %v", err)
}
if params.ChallengeID == "" {
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "challenge_id is required")
}
if params.Credential == nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "credential is required")
}
challengeID, err := uuid.FromString(params.ChallengeID)
if err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "challenge_id must be a valid UUID")
}
challenge, err := models.ConsumeWebAuthnChallengeByID(db, challengeID, models.WebAuthnChallengeTypeAuthentication, nil)
if err != nil {
if models.IsNotFoundError(err) {
return apierrors.NewBadRequestError(apierrors.ErrorCodeWebAuthnChallengeNotFound, "Challenge not found or already used")
}
return apierrors.NewInternalServerError("Database error consuming challenge").WithInternalError(err)
}
if challenge.IsExpired() {
return apierrors.NewBadRequestError(apierrors.ErrorCodeWebAuthnChallengeExpired, "Challenge has expired")
}
parsedResponse, err := parseCredentialAssertionResponse(params.Credential)
if err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeWebAuthnVerificationFailed, "Invalid credential response").WithInternalError(err)
}
webAuthn, err := a.getPasskeyWebAuthn()
if err != nil {
return apierrors.NewInternalServerError("Failed to initialize WebAuthn").WithInternalError(err)
}
sessionData := *challenge.SessionData.SessionData
// Discoverable login: resolve user from userHandle in the assertion
handler := func(rawID, userHandle []byte) (webauthn.User, error) {
userID, uerr := uuid.FromString(string(userHandle))
if uerr != nil {
return nil, uerr
}
u, uerr := models.FindUserByID(db, userID)
if uerr != nil {
return nil, uerr
}
creds, uerr := models.FindWebAuthnCredentialsByUserID(db, u.ID)
if uerr != nil {
return nil, uerr
}
if len(creds) == 0 {
return nil, models.WebAuthnCredentialNotFoundError{}
}
return newWebAuthnUser(u, creds), nil
}
webauthnUser, credential, err := webAuthn.ValidatePasskeyLogin(handler, sessionData, parsedResponse)
if err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeWebAuthnVerificationFailed, "Credential verification failed").WithInternalError(err)
}
// Look up the authenticated user from the validated assertion's userHandle
userID, err := uuid.FromString(string(webauthnUser.WebAuthnID()))
if err != nil {
return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Invalid user handle in assertion")
}
user, err := models.FindUserByID(db, userID)
if err != nil {
return apierrors.NewInternalServerError("Database error loading user").WithInternalError(err)
}
if user.GetEmail() != "" && !user.IsConfirmed() {
return apierrors.NewForbiddenError(apierrors.ErrorCodeEmailNotConfirmed, "Email not confirmed")
}
if user.GetPhone() != "" && !user.IsPhoneConfirmed() {
return apierrors.NewForbiddenError(apierrors.ErrorCodePhoneNotConfirmed, "Phone not confirmed")
}
if user.IsBanned() {
return apierrors.NewForbiddenError(apierrors.ErrorCodeUserBanned, "User is banned")
}
// Find the matching WebAuthnCredential record to update
passkeyCredential, err := models.FindWebAuthnCredentialByCredentialID(db, credential.ID)
if err != nil {
return apierrors.NewInternalServerError("Database error loading passkey").WithInternalError(err)
}
var grantParams models.GrantParams
grantParams.FillGrantParams(r)
var token *AccessTokenResponse
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
if terr = passkeyCredential.UpdateLastUsedWithSignCount(tx, credential.Authenticator.SignCount); terr != nil {
return terr
}
if terr = models.NewAuditLogEntry(config.AuditLog, r, tx, user, models.LoginAction, map[string]any{
"passkey_id": passkeyCredential.ID,
}); terr != nil {
return terr
}
token, terr = a.issueRefreshToken(r, w.Header(), tx, user, models.PasskeyLogin, grantParams)
if terr != nil {
return terr
}
return nil
})
if err != nil {
return err
}
metering.RecordLogin(metering.LoginTypePasskey, user.ID, nil)
return sendJSON(w, http.StatusOK, token)
}
// parseCredentialAssertionResponse parses a WebAuthn credential assertion response from raw JSON.
func parseCredentialAssertionResponse(raw json.RawMessage) (*protocol.ParsedCredentialAssertionData, error) {
var car protocol.CredentialAssertionResponse
if err := json.Unmarshal(raw, &car); err != nil {
return nil, err
}
return car.Parse()
}