-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
493 lines (407 loc) · 14.4 KB
/
user.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package mfa
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/fxamacker/cbor/v2"
"github.com/pkg/errors"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/protocol/webauthncose"
"github.com/go-webauthn/webauthn/webauthn"
)
const (
UserContextKey = "user"
WebAuthnTablePK = "uuid"
LegacyU2FCredID = "u2f"
)
type DynamoUser struct {
// Shared fields between U2F and WebAuthn
ID string `json:"uuid"`
ApiKeyValue string `json:"apiKey"`
ApiKey ApiKey `json:"-"`
Store *Storage `json:"-"`
// U2F fields
AppId string `json:"-"`
EncryptedAppId string `json:"encryptedAppId,omitempty"`
KeyHandle string `json:"-"`
EncryptedKeyHandle string `json:"encryptedKeyHandle,omitempty"`
PublicKey string `json:"-"`
EncryptedPublicKey string `json:"encryptedPublicKey,omitempty"`
// WebAuthn fields
SessionData webauthn.SessionData `json:"-"`
EncryptedSessionData []byte `json:"EncryptedSessionData,omitempty"`
// These can be multiple Yubikeys or other WebAuthn entries
Credentials []webauthn.Credential `json:"-"`
EncryptedCredentials []byte `json:"EncryptedCredentials,omitempty"`
WebAuthnClient *webauthn.WebAuthn `json:"-"`
Name string `json:"-"`
DisplayName string `json:"-"`
Icon string `json:"-"`
}
func NewDynamoUser(apiConfig ApiMeta, storage *Storage, apiKey ApiKey, webAuthnClient *webauthn.WebAuthn) DynamoUser {
u := DynamoUser{
ID: apiConfig.UserUUID,
Name: apiConfig.Username,
DisplayName: apiConfig.UserDisplayName,
Icon: apiConfig.UserIcon,
Store: storage,
WebAuthnClient: webAuthnClient,
ApiKey: apiKey,
ApiKeyValue: apiKey.Key,
}
if u.ID == "" {
return u
}
err := u.Load()
if err != nil {
log.Printf("failed to load user: %s\n", err)
}
return u
}
func (u *DynamoUser) RemoveU2F() {
u.AppId = ""
u.EncryptedAppId = ""
u.KeyHandle = ""
u.EncryptedKeyHandle = ""
u.PublicKey = ""
u.EncryptedPublicKey = ""
}
func (u *DynamoUser) unsetSessionData() error {
u.EncryptedSessionData = nil
return u.Store.Store(envConfig.WebauthnTable, u)
}
func (u *DynamoUser) saveSessionData(sessionData webauthn.SessionData) error {
// load to be sure working with latest data
err := u.Load()
if err != nil {
return err
}
js, err := json.Marshal(sessionData)
if err != nil {
log.Printf("error marshaling session data to json. Session data: %+v\n Error: %s\n", sessionData, err)
return err
}
enc, err := u.ApiKey.Encrypt(js)
if err != nil {
return err
}
u.EncryptedSessionData = enc
return u.Store.Store(envConfig.WebauthnTable, u)
}
func (u *DynamoUser) saveNewCredential(credential webauthn.Credential) error {
// load to be sure working with latest data
err := u.Load()
if err != nil {
return err
}
// check existing credentials to make sure this one doesn't already exist
for _, c := range u.Credentials {
if string(c.ID) == string(credential.ID) {
return fmt.Errorf("a credential with this ID already exists")
}
}
// append new credential to existing
u.Credentials = append(u.Credentials, credential)
// encrypt credentials for storage
return u.encryptAndStoreCredentials()
}
// DeleteCredential expects a hashed-encoded credential id. It finds a matching credential for that user and saves the
// user without that credential included. Alternatively, if the given credential id indicates that a legacy U2F key
// should be removed (e.g. by matching the string "u2f") then that user is saved with all of its legacy u2f fields
// blanked out.
func (u *DynamoUser) DeleteCredential(credIDHash string) (int, error) {
// load to be sure working with the latest data
err := u.Load()
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("error in DeleteCredential: %w", err)
}
if credIDHash == LegacyU2FCredID {
u.RemoveU2F()
if err := u.Store.Store(envConfig.WebauthnTable, u); err != nil {
return http.StatusInternalServerError, fmt.Errorf("error in DeleteCredential deleting legacy u2f: %w", err)
}
return http.StatusNoContent, nil
}
if len(u.Credentials) == 0 {
err := fmt.Errorf("error in DeleteCredential. No webauthn credentials available.")
return http.StatusNotFound, err
}
remainingCreds := []webauthn.Credential{}
// remove the requested credential from among the user's current webauthn credentials
for _, c := range u.Credentials {
if hashAndEncodeKeyHandle(c.ID) == credIDHash {
continue
}
remainingCreds = append(remainingCreds, c)
}
if len(remainingCreds) == len(u.Credentials) {
err := fmt.Errorf("error in DeleteCredential. Credential not found with id: %s", credIDHash)
return http.StatusNotFound, err
}
u.Credentials = remainingCreds
if err := u.encryptAndStoreCredentials(); err != nil {
return http.StatusInternalServerError, fmt.Errorf("error in DeleteCredential storing remaining credentials: %w", err)
}
return http.StatusNoContent, nil
}
func (u *DynamoUser) encryptAndStoreCredentials() error {
js, err := json.Marshal(u.Credentials)
if err != nil {
return err
}
enc, err := u.ApiKey.Encrypt(js)
if err != nil {
return err
}
u.EncryptedCredentials = enc
return u.Store.Store(envConfig.WebauthnTable, u)
}
func (u *DynamoUser) Load() error {
err := u.Store.Load(envConfig.WebauthnTable, WebAuthnTablePK, u.ID, u)
if err != nil {
return errors.Wrap(err, "failed to load user")
}
// decrypt SessionStorage if available
if len(u.EncryptedSessionData) > 0 {
plain, err := u.ApiKey.Decrypt(u.EncryptedSessionData)
if err != nil {
return errors.Wrap(err, "failed to decrypt encrypted session data")
}
// decryption process includes extra/invalid \x00 character, so trim it out
plain = bytes.Trim(plain, "\x00")
// unmarshal decrypted session data into SessionData
var sd webauthn.SessionData
err = json.Unmarshal(plain, &sd)
if err != nil {
log.Printf("failed to unmarshal encrypted session data, will discard and continue. error: %s", err)
}
u.SessionData = sd
}
// decrypt Credentials if available
if len(u.EncryptedCredentials) > 0 {
dec, err := u.ApiKey.Decrypt(u.EncryptedCredentials)
if err != nil {
return errors.Wrap(err, "failed to decrypt encrypted credential data")
}
// decryption process includes extra/invalid \x00 character, so trim it out
dec = bytes.Trim(dec, "\x00")
// unmarshal decrypted session data into Credentials
var creds []webauthn.Credential
err = json.Unmarshal(dec, &creds)
if err != nil {
return errors.Wrap(err, "failed to unmarshal credential data")
}
u.Credentials = creds
}
return nil
}
func (u *DynamoUser) Delete() error {
return u.Store.Delete(envConfig.WebauthnTable, WebAuthnTablePK, u.ID)
}
func (u *DynamoUser) BeginRegistration() (*protocol.CredentialCreation, error) {
if u.WebAuthnClient == nil {
return nil, fmt.Errorf("dynamoUser, %s, missing WebAuthClient in BeginRegistration", u.Name)
}
rrk := false
authSelection := protocol.AuthenticatorSelection{
RequireResidentKey: &rrk,
UserVerification: protocol.VerificationDiscouraged,
}
options, sessionData, err := u.WebAuthnClient.BeginRegistration(u, webauthn.WithAuthenticatorSelection(authSelection))
if err != nil {
return &protocol.CredentialCreation{}, fmt.Errorf("failed to begin registration: %w", err)
}
err = u.saveSessionData(*sessionData)
if err != nil {
return &protocol.CredentialCreation{}, fmt.Errorf("failed to save session data: %w", err)
}
return options, nil
}
func (u *DynamoUser) FinishRegistration(r *http.Request) (string, error) {
if r.Body == nil {
return "", fmt.Errorf("request Body may not be nil in FinishRegistration")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", fmt.Errorf("failed to get api config from request: %w", err)
}
br := fixEncoding(body)
parsedResponse, err := protocol.ParseCredentialCreationResponseBody(br)
if err != nil {
logProtocolError("unable to parse body", err)
return "", fmt.Errorf("unable to parse credential creation response body: %w", err)
}
credential, err := u.WebAuthnClient.CreateCredential(u, u.SessionData, parsedResponse)
if err != nil {
logProtocolError("unable to create credential", err)
return "", fmt.Errorf("unable to create credential: %w", err)
}
err = u.saveNewCredential(*credential)
if err != nil {
return "", fmt.Errorf("unable to save new credential`: %w", err)
}
keyHandleHash := hashAndEncodeKeyHandle(credential.ID)
return keyHandleHash, u.unsetSessionData()
}
func (u *DynamoUser) BeginLogin() (*protocol.CredentialAssertion, error) {
extensions := protocol.AuthenticationExtensions{}
if u.EncryptedAppId != "" {
appid, err := u.ApiKey.DecryptLegacy([]byte(u.EncryptedAppId))
if err != nil {
return nil, fmt.Errorf("unable to decrypt legacy app id: %s", err)
}
extensions["appid"] = string(appid)
}
options, sessionData, err := u.WebAuthnClient.BeginLogin(u, webauthn.WithAssertionExtensions(extensions), webauthn.WithUserVerification(protocol.VerificationDiscouraged))
if err != nil {
return &protocol.CredentialAssertion{}, err
}
err = u.saveSessionData(*sessionData)
if err != nil {
log.Printf("error saving session data: %s\n", err)
return nil, err
}
return options, nil
}
func (u *DynamoUser) FinishLogin(r *http.Request) (*webauthn.Credential, error) {
if r.Body == nil {
return nil, fmt.Errorf("request Body may not be nil in FinishLogin")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request body: %s", err)
return &webauthn.Credential{}, fmt.Errorf("failed to read request body: %s", err)
}
br := fixEncoding(body)
parsedResponse, err := protocol.ParseCredentialRequestResponseBody(br)
if err != nil {
logProtocolError(fmt.Sprintf("failed to parse credential request response body: %s", body), err)
return &webauthn.Credential{}, fmt.Errorf("failed to parse credential request response body: %s", err)
}
// If user has registered U2F creds, check if RPIDHash is actually hash of AppId
// if so, replace authenticator data RPIDHash with a hash of the RPID for validation
if u.EncryptedAppId != "" {
appid, err := u.ApiKey.DecryptLegacy([]byte(u.EncryptedAppId))
if err != nil {
return nil, fmt.Errorf("unable to decrypt legacy app id: %s", err)
}
appIdHash := sha256.Sum256([]byte(appid))
rpIdHash := sha256.Sum256([]byte(u.WebAuthnClient.Config.RPID))
if fmt.Sprintf("%x", parsedResponse.Response.AuthenticatorData.RPIDHash) == fmt.Sprintf("%x", appIdHash) {
parsedResponse.Response.AuthenticatorData.RPIDHash = rpIdHash[:]
}
}
// there is an issue with URLEncodeBase64.UnmarshalJSON and null values
// see https://github.com/go-webauthn/webauthn/issues/69
// null byte sequence is []byte{158,233,101}
if isNullByteSlice(parsedResponse.Response.UserHandle) {
parsedResponse.Response.UserHandle = nil
}
credential, err := u.WebAuthnClient.ValidateLogin(u, u.SessionData, parsedResponse)
if err != nil {
logProtocolError("failed to validate login", err)
return &webauthn.Credential{}, fmt.Errorf("failed to validate login: %s", err)
}
return credential, nil
}
// User ID according to the Relying Party
func (u *DynamoUser) WebAuthnID() []byte {
return []byte(u.ID)
}
// User Name according to the Relying Party
func (u *DynamoUser) WebAuthnName() string {
return u.Name
}
// Display Name of the user
func (u *DynamoUser) WebAuthnDisplayName() string {
return u.DisplayName
}
// User's icon url
func (u *DynamoUser) WebAuthnIcon() string {
return u.Icon
}
// WebAuthnCredentials returns an array of credentials plus a U2F cred if present
func (u *DynamoUser) WebAuthnCredentials() []webauthn.Credential {
creds := u.Credentials
if u.EncryptedKeyHandle != "" && u.EncryptedPublicKey != "" {
credId, err := u.ApiKey.DecryptLegacy([]byte(u.EncryptedKeyHandle))
if err != nil {
log.Printf("unable to decrypt credential id: %s", err)
return nil
}
// decryption process includes extra/invalid \x00 character, so trim it out
// at some point early in dev this was needed, but in testing recently it doesn't
// make a difference. Leaving commented out for now until we know 100% it's not needed
// credId = bytes.Trim(credId, "\x00")
decodedCredId, err := base64.RawURLEncoding.DecodeString(string(credId))
if err != nil {
log.Println("error decoding credential id:", err)
return nil
}
pubKey, err := u.ApiKey.DecryptLegacy([]byte(u.EncryptedPublicKey))
if err != nil {
log.Printf("unable to decrypt pubic key: %s", err)
return nil
}
// Same as credId
// pubKey = bytes.Trim(pubKey, "\x00")
decodedPubKey, err := base64.RawURLEncoding.DecodeString(string(pubKey))
if err != nil {
log.Println("error decoding public key:", err)
return nil
}
// U2F key is concatenation of 0x4 + Xcoord + Ycoord
// documentation / example at https://docs.yubico.com/yesdk/users-manual/application-piv/attestation.html
coordLen := (len(decodedPubKey) - 1) / 2
xCoord := decodedPubKey[1 : coordLen+1]
yCoord := decodedPubKey[1+coordLen:]
ec2PublicKey := webauthncose.EC2PublicKeyData{
XCoord: xCoord,
YCoord: yCoord,
PublicKeyData: webauthncose.PublicKeyData{
Algorithm: int64(webauthncose.AlgES256),
KeyType: int64(webauthncose.EllipticKey),
},
}
// Get the CBOR-encoded representation of the OKPPublicKeyData
cborEncodedKey, err := cbor.Marshal(ec2PublicKey)
if err != nil {
log.Printf("error marshalling key to cbor: %s", err)
return nil
}
creds = append(creds, webauthn.Credential{
ID: decodedCredId,
PublicKey: cborEncodedKey,
AttestationType: string(protocol.PublicKeyCredentialType),
})
}
return creds
}
// isNullByteSlice works around a bug in json unmarshalling for a urlencoded base64 string
func isNullByteSlice(slice []byte) bool {
if len(slice) != 3 {
return false
}
if slice[0] == 158 && slice[1] == 233 && slice[2] == 101 {
return true
}
return false
}
func hashAndEncodeKeyHandle(id []byte) string {
hash := sha256.Sum256(id)
return base64.RawURLEncoding.EncodeToString(hash[:])
}
// logProtocolError logs a detailed message if the given error is an Error from go-webauthn/webauthn/protocol
func logProtocolError(msg string, err error) {
var protocolError *protocol.Error
if errors.As(err, &protocolError) {
log.Printf("%s, ProtocolError: %s, DevInfo: %s", msg, protocolError.Details, protocolError.DevInfo)
} else {
log.Printf("%s, Error: %s", msg, err)
}
}