Skip to content

Commit d7644bd

Browse files
authored
Merge pull request #99 from silinternational/feature/rename-user
rename DynamoUser to WebauthnUser
2 parents 8bbf3cc + fd5316c commit d7644bd

File tree

7 files changed

+49
-48
lines changed

7 files changed

+49
-48
lines changed

fixtures_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func getDBConfig(ms *MfaSuite) baseTestConfig {
3838
}
3939
}
4040

41-
func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []DynamoUser {
41+
func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []WebauthnUser {
4242
cred10 := webauthn.Credential{ID: []byte("C10")}
4343
cred20 := webauthn.Credential{ID: []byte("C20")}
4444
cred21 := webauthn.Credential{ID: []byte("C21")}
@@ -59,7 +59,7 @@ func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []DynamoUser {
5959
apiKey2.Key = "1234567890123456"
6060
apiKey2.Secret = "E286600E-3DBF-4C23-A0DA-9C55D448"
6161

62-
testUser0 := DynamoUser{
62+
testUser0 := WebauthnUser{
6363
ID: apiKey0.Secret,
6464
Name: "Nancy_NoCredential",
6565
DisplayName: "Nancy NoCredential",
@@ -94,7 +94,7 @@ func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []DynamoUser {
9494
testUser0.PublicKey = "somePublicKey"
9595
testUser0.EncryptedPublicKey = "someEncryptedPublicKey"
9696

97-
for _, u := range []DynamoUser{testUser0, testUser1, testUser2} {
97+
for _, u := range []WebauthnUser{testUser0, testUser1, testUser2} {
9898
ms.NoError(u.encryptAndStoreCredentials(), "failed saving initial test user")
9999
}
100100

@@ -107,5 +107,5 @@ func getTestWebauthnUsers(ms *MfaSuite, config baseTestConfig) []DynamoUser {
107107
ms.NoError(err, "failed to scan storage for new user entries")
108108
ms.Equal(int32(3), results.Count, "Count:3", "initial data wasn't saved properly")
109109

110-
return []DynamoUser{testUser0, testUser1, testUser2}
110+
return []WebauthnUser{testUser0, testUser1, testUser2}
111111
}

storage_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (ms *MfaSuite) TestStorage_StoreLoad() {
3737
},
3838
args: args{
3939
key: "2B28BED1-1225-4EC9-98F9-EAB8FBCEDBA0",
40-
item: &DynamoUser{
40+
item: &WebauthnUser{
4141
ID: "2B28BED1-1225-4EC9-98F9-EAB8FBCEDBA0",
4242
Name: "test_user",
4343
DisplayName: "Test User",
@@ -64,7 +64,7 @@ func (ms *MfaSuite) TestStorage_StoreLoad() {
6464
}
6565
ms.NoError(err, "unexpected error with Store()")
6666

67-
var user DynamoUser
67+
var user WebauthnUser
6868
ms.NoError(s.Load(tt.fields.Table, "uuid", tt.args.key, &user), "error with s.Load()")
6969

7070
ms.Equal(tt.args.key, user.ID, "incorrect user.ID")

u2fsimulator/u2fsimulator_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func newHttpResponseWriter() *httpResponseWriter {
1717
Headers: http.Header{},
1818
}
1919
}
20+
2021
func (w *httpResponseWriter) Header() http.Header {
2122
return w.Headers
2223
}
@@ -49,7 +50,7 @@ func (us *U2fSuite) Test_U2fRegistration() {
4950
httpRequest, err := http.NewRequest(http.MethodPost, "https://example.com", bytes.NewBuffer(requestBody))
5051
us.NoError(err, "error just creating http request for test")
5152

52-
httpRequest.Header.Set("x-mfa-UserUUID", "the-id-of-the-dynamo-user")
53+
httpRequest.Header.Set("x-mfa-UserUUID", "the-id-of-the-webauthn-user")
5354
httpRequest.Header.Set("x-mfa-RPID", rpID)
5455
httpRequest.Header.Set("x-mfa-RPOrigin", rpID)
5556

user.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const WebAuthnTablePK = "uuid"
2828
// have this in its ID field.
2929
const LegacyU2FCredID = "u2f"
3030

31-
// DynamoUser holds user data from DynamoDB, in both encrypted and unencrypted form. It also holds a Webauthn client
31+
// WebauthnUser holds user data from DynamoDB, in both encrypted and unencrypted form. It also holds a Webauthn client
3232
// and Webauthn API data.
33-
type DynamoUser struct {
33+
type WebauthnUser struct {
3434
// Shared fields between U2F and WebAuthn
3535
ID string `dynamodbav:"uuid" json:"uuid"`
3636
ApiKeyValue string `dynamodbav:"apiKey" json:"apiKey"`
@@ -59,9 +59,9 @@ type DynamoUser struct {
5959
Icon string `dynamodbav:"-" json:"-"`
6060
}
6161

62-
// NewDynamoUser creates a new DynamoUser from API input data, a storage client and a Webauthn client.
63-
func NewDynamoUser(apiConfig ApiMeta, storage *Storage, apiKey ApiKey, webAuthnClient *webauthn.WebAuthn) DynamoUser {
64-
u := DynamoUser{
62+
// NewWebauthnUser creates a new WebauthnUser from API input data, a storage client and a Webauthn client.
63+
func NewWebauthnUser(apiConfig ApiMeta, storage *Storage, apiKey ApiKey, webAuthnClient *webauthn.WebAuthn) WebauthnUser {
64+
u := WebauthnUser{
6565
ID: apiConfig.UserUUID,
6666
Name: apiConfig.Username,
6767
DisplayName: apiConfig.UserDisplayName,
@@ -85,7 +85,7 @@ func NewDynamoUser(apiConfig ApiMeta, storage *Storage, apiKey ApiKey, webAuthnC
8585

8686
// RemoveU2F clears U2F fields in the user struct. To be used when a user has requested removal of their legacy U2F key.
8787
// Should be followed by a database store operation.
88-
func (u *DynamoUser) RemoveU2F() {
88+
func (u *WebauthnUser) RemoveU2F() {
8989
u.AppId = ""
9090
u.EncryptedAppId = ""
9191
u.KeyHandle = ""
@@ -95,14 +95,14 @@ func (u *DynamoUser) RemoveU2F() {
9595
}
9696

9797
// unsetSessionData clears the encrypted session data from a user and stores the updated record in the database.
98-
func (u *DynamoUser) unsetSessionData() error {
98+
func (u *WebauthnUser) unsetSessionData() error {
9999
u.EncryptedSessionData = nil
100100
return u.Store.Store(envConfig.WebauthnTable, u)
101101
}
102102

103103
// saveSessionData encrypts the user's session data and updates the database record.
104104
// CAUTION: user data is refreshed from the database by this function. Any unsaved data will be lost.
105-
func (u *DynamoUser) saveSessionData(sessionData webauthn.SessionData) error {
105+
func (u *WebauthnUser) saveSessionData(sessionData webauthn.SessionData) error {
106106
// load to be sure working with latest data
107107
err := u.Load()
108108
if err != nil {
@@ -127,7 +127,7 @@ func (u *DynamoUser) saveSessionData(sessionData webauthn.SessionData) error {
127127
// saveNewCredential appends a new credential to the user's credential list, encrypts the list, and updates the
128128
// database record.
129129
// CAUTION: user data is refreshed from the database by this function. Any unsaved data will be lost.
130-
func (u *DynamoUser) saveNewCredential(credential webauthn.Credential) error {
130+
func (u *WebauthnUser) saveNewCredential(credential webauthn.Credential) error {
131131
// load to be sure working with latest data
132132
err := u.Load()
133133
if err != nil {
@@ -153,7 +153,7 @@ func (u *DynamoUser) saveNewCredential(credential webauthn.Credential) error {
153153
// should be removed (i.e. by matching the string "u2f") then that user is saved with all of its legacy u2f fields
154154
// blanked out.
155155
// CAUTION: user data is refreshed from the database by this function. Any unsaved data will be lost.
156-
func (u *DynamoUser) DeleteCredential(credIDHash string) (int, error) {
156+
func (u *WebauthnUser) DeleteCredential(credIDHash string) (int, error) {
157157
// load to be sure working with the latest data
158158
err := u.Load()
159159
if err != nil {
@@ -197,7 +197,7 @@ func (u *DynamoUser) DeleteCredential(credIDHash string) (int, error) {
197197
}
198198

199199
// encryptAndStoreCredentials encrypts the user's credential list and updates the database record
200-
func (u *DynamoUser) encryptAndStoreCredentials() error {
200+
func (u *WebauthnUser) encryptAndStoreCredentials() error {
201201
js, err := json.Marshal(u.Credentials)
202202
if err != nil {
203203
return err
@@ -213,7 +213,7 @@ func (u *DynamoUser) encryptAndStoreCredentials() error {
213213
}
214214

215215
// Load refreshes a user object from the database record and decrypts the session data and credential list
216-
func (u *DynamoUser) Load() error {
216+
func (u *WebauthnUser) Load() error {
217217
err := u.Store.Load(envConfig.WebauthnTable, WebAuthnTablePK, u.ID, u)
218218
if err != nil {
219219
return errors.Wrap(err, "failed to load user")
@@ -262,15 +262,15 @@ func (u *DynamoUser) Load() error {
262262
}
263263

264264
// Delete removes the user from the database
265-
func (u *DynamoUser) Delete() error {
265+
func (u *WebauthnUser) Delete() error {
266266
return u.Store.Delete(envConfig.WebauthnTable, WebAuthnTablePK, u.ID)
267267
}
268268

269269
// BeginRegistration processes the first half of the Webauthn Registration flow for the user and returns the
270270
// CredentialCreation data to pass back to the client. User session data is saved in the database.
271-
func (u *DynamoUser) BeginRegistration() (*protocol.CredentialCreation, error) {
271+
func (u *WebauthnUser) BeginRegistration() (*protocol.CredentialCreation, error) {
272272
if u.WebAuthnClient == nil {
273-
return nil, fmt.Errorf("dynamoUser, %s, missing WebAuthClient in BeginRegistration", u.Name)
273+
return nil, fmt.Errorf("webauthnUser, %s, missing WebAuthClient in BeginRegistration", u.Name)
274274
}
275275

276276
rrk := false
@@ -295,7 +295,7 @@ func (u *DynamoUser) BeginRegistration() (*protocol.CredentialCreation, error) {
295295
// FinishRegistration processes the last half of the Webauthn Registration flow for the user and returns the
296296
// key_handle_hash to pass back to the client. The client should store this value for later use. User session data is
297297
// cleared from the database.
298-
func (u *DynamoUser) FinishRegistration(r *http.Request) (string, error) {
298+
func (u *WebauthnUser) FinishRegistration(r *http.Request) (string, error) {
299299
if r.Body == nil {
300300
return "", fmt.Errorf("request Body may not be nil in FinishRegistration")
301301
}
@@ -330,7 +330,7 @@ func (u *DynamoUser) FinishRegistration(r *http.Request) (string, error) {
330330

331331
// BeginLogin processes the first half of the Webauthn Authentication flow for the user and returns the
332332
// CredentialAssertion data to pass back to the client. User session data is saved in the database.
333-
func (u *DynamoUser) BeginLogin() (*protocol.CredentialAssertion, error) {
333+
func (u *WebauthnUser) BeginLogin() (*protocol.CredentialAssertion, error) {
334334
extensions := protocol.AuthenticationExtensions{}
335335
if u.EncryptedAppId != "" {
336336
appid, err := u.ApiKey.DecryptLegacy([]byte(u.EncryptedAppId))
@@ -356,7 +356,7 @@ func (u *DynamoUser) BeginLogin() (*protocol.CredentialAssertion, error) {
356356

357357
// FinishLogin processes the last half of the Webauthn Authentication flow for the user and returns the
358358
// Credential data to pass back to the client. User session data is untouched by this function.
359-
func (u *DynamoUser) FinishLogin(r *http.Request) (*webauthn.Credential, error) {
359+
func (u *WebauthnUser) FinishLogin(r *http.Request) (*webauthn.Credential, error) {
360360
if r.Body == nil {
361361
return nil, fmt.Errorf("request Body may not be nil in FinishLogin")
362362
}
@@ -400,27 +400,27 @@ func (u *DynamoUser) FinishLogin(r *http.Request) (*webauthn.Credential, error)
400400
}
401401

402402
// WebAuthnID returns the user's ID according to the Relying Party
403-
func (u *DynamoUser) WebAuthnID() []byte {
403+
func (u *WebauthnUser) WebAuthnID() []byte {
404404
return []byte(u.ID)
405405
}
406406

407407
// WebAuthnName returns the user's name according to the Relying Party
408-
func (u *DynamoUser) WebAuthnName() string {
408+
func (u *WebauthnUser) WebAuthnName() string {
409409
return u.Name
410410
}
411411

412412
// WebAuthnDisplayName returns the display name of the user
413-
func (u *DynamoUser) WebAuthnDisplayName() string {
413+
func (u *WebauthnUser) WebAuthnDisplayName() string {
414414
return u.DisplayName
415415
}
416416

417417
// WebAuthnIcon returns the user's icon URL
418-
func (u *DynamoUser) WebAuthnIcon() string {
418+
func (u *WebauthnUser) WebAuthnIcon() string {
419419
return u.Icon
420420
}
421421

422422
// WebAuthnCredentials returns an array of credentials (passkeys) plus a U2F credential if present
423-
func (u *DynamoUser) WebAuthnCredentials() []webauthn.Credential {
423+
func (u *WebauthnUser) WebAuthnCredentials() []webauthn.Credential {
424424
if u.EncryptedKeyHandle == "" || u.EncryptedPublicKey == "" {
425425
// no U2F credential found
426426
return u.Credentials

user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (ms *MfaSuite) Test_User_DeleteCredential() {
2222

2323
tests := []struct {
2424
name string
25-
user DynamoUser
25+
user WebauthnUser
2626
credID string
2727
wantErrContains string
2828
wantStatus int
@@ -111,7 +111,7 @@ func (ms *MfaSuite) Test_User_DeleteCredential() {
111111
tt.verifyFn(results)
112112
}
113113

114-
gotUser := DynamoUser{
114+
gotUser := WebauthnUser{
115115
ID: tt.user.ID,
116116
ApiKey: tt.user.ApiKey,
117117
Store: baseConfigs.Storage,

webauthn.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,20 @@ func getApiMetaFromRequest(r *http.Request) (ApiMeta, error) {
296296
return meta, nil
297297
}
298298

299-
// getUserFromContext returns the authenticated DynamoUser from the request context. The authentication middleware or
299+
// getUserFromContext returns the authenticated WebauthnUser from the request context. The authentication middleware or
300300
// early handler processing inserts the authenticated user into the context for retrieval by this function.
301-
func getUserFromContext(r *http.Request) (*DynamoUser, error) {
302-
user, ok := r.Context().Value(UserContextKey).(*DynamoUser)
301+
func getUserFromContext(r *http.Request) (*WebauthnUser, error) {
302+
user, ok := r.Context().Value(UserContextKey).(*WebauthnUser)
303303
if !ok {
304-
return &DynamoUser{}, errors.New("unable to get user from request context")
304+
return &WebauthnUser{}, errors.New("unable to get user from request context")
305305
}
306306

307307
return user, nil
308308
}
309309

310310
// AuthenticateRequest checks the provided API key against the keys stored in the database. If the key is active and
311-
// valid, a Webauthn client and DynamoUser are created and stored in the request context.
312-
func AuthenticateRequest(r *http.Request) (*DynamoUser, error) {
311+
// valid, a Webauthn client and WebauthnUser are created and stored in the request context.
312+
func AuthenticateRequest(r *http.Request) (*WebauthnUser, error) {
313313
// get key and secret from headers
314314
key := r.Header.Get("x-mfa-apikey")
315315
secret := r.Header.Get("x-mfa-apisecret")
@@ -361,7 +361,7 @@ func AuthenticateRequest(r *http.Request) (*DynamoUser, error) {
361361
return nil, fmt.Errorf("unable to create webauthn client from api meta config: %w", err)
362362
}
363363

364-
user := NewDynamoUser(apiMeta, localStorage, apiKey, webAuthnClient)
364+
user := NewWebauthnUser(apiMeta, localStorage, apiKey, webAuthnClient)
365365

366366
// If this user exists (api key value is not empty), make sure the calling API Key owns the user and is allowed to operate on it
367367
if user.ApiKeyValue != "" && user.ApiKeyValue != apiKey.Key {

webauthn_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func getTestAssertionResponse(credID, authData, clientData, attestationObject st
5151
}`)
5252
}
5353

54-
func getTestAssertionRequest(credID1, authData1, clientData1, attestObject1 string, user *DynamoUser) *http.Request {
54+
func getTestAssertionRequest(credID1, authData1, clientData1, attestObject1 string, user *WebauthnUser) *http.Request {
5555
assertResp := getTestAssertionResponse(credID1, authData1, clientData1, attestObject1)
5656

5757
body := io.NopCloser(bytes.NewReader(assertResp))
@@ -137,7 +137,7 @@ func (ms *MfaSuite) Test_BeginRegistration() {
137137
const userID = "12345678-1234-1234-1234-123456789012"
138138
userIDEncoded := base64.StdEncoding.EncodeToString([]byte(userID))
139139

140-
userNoID := DynamoUser{
140+
userNoID := WebauthnUser{
141141
Name: "Nelly_NoID",
142142
DisplayName: "Nelly NoID",
143143
Store: localStorage,
@@ -150,7 +150,7 @@ func (ms *MfaSuite) Test_BeginRegistration() {
150150
ctxNoID := context.WithValue(reqNoID.Context(), UserContextKey, &userNoID)
151151
reqNoID = *reqNoID.WithContext(ctxNoID)
152152

153-
testUser := DynamoUser{
153+
testUser := WebauthnUser{
154154
ID: userID,
155155
Name: "Charlie_HasCredentials",
156156
DisplayName: "Charlie HasCredentials",
@@ -276,7 +276,7 @@ func (ms *MfaSuite) Test_FinishRegistration() {
276276
const userID = "00345678-1234-1234-1234-123456789012"
277277
const challenge = "W8GzFU8pGjhoRbWrLDlamAfq_y4S1CZG1VuoeRLARrE"
278278

279-
testUser := DynamoUser{
279+
testUser := WebauthnUser{
280280
ID: userID,
281281
Name: "Charlie_HasCredentials",
282282
DisplayName: "Charlie HasCredentials",
@@ -431,7 +431,7 @@ func (ms *MfaSuite) Test_BeginLogin() {
431431
ms.NoError(err, "failed creating new webAuthnClient for test")
432432

433433
// Just check one of the error conditions with this user
434-
userNoCreds := DynamoUser{
434+
userNoCreds := WebauthnUser{
435435
ID: "",
436436
Name: "Nelly_NoCredentials",
437437
DisplayName: "Nelly NoCredentials",
@@ -461,7 +461,7 @@ func (ms *MfaSuite) Test_BeginLogin() {
461461
},
462462
}
463463

464-
userWithCreds := DynamoUser{
464+
userWithCreds := WebauthnUser{
465465
ID: userID,
466466
Name: "Charlie_HasCredentials",
467467
DisplayName: "Charlie HasCredentials",
@@ -607,7 +607,7 @@ func (ms *MfaSuite) Test_FinishLogin() {
607607
},
608608
}
609609

610-
userWithCreds := DynamoUser{
610+
userWithCreds := WebauthnUser{
611611
ID: userID,
612612
Name: "Charlie_HasCredentials",
613613
DisplayName: "Charlie HasCredentials",
@@ -800,7 +800,7 @@ func (ms *MfaSuite) Test_DeleteCredential() {
800800
users := getTestWebauthnUsers(ms, baseConfigs)
801801
testUser0, testUser1, testUser2 := users[0], users[1], users[2]
802802

803-
for i, u := range []DynamoUser{testUser0, testUser1, testUser2} {
803+
for i, u := range []WebauthnUser{testUser0, testUser1, testUser2} {
804804
ms.NoError(u.ApiKey.Hash(), "error trying to hash apikey: %d", i)
805805
ms.NoError(u.encryptAndStoreCredentials(), "failed updating test user")
806806
ms.NoError(u.ApiKey.Store.Store(baseConfigs.EnvConfig.ApiKeyTable, u.ApiKey), "failed saving initial apikey")
@@ -823,7 +823,7 @@ func (ms *MfaSuite) Test_DeleteCredential() {
823823

824824
tests := []struct {
825825
name string
826-
user DynamoUser
826+
user WebauthnUser
827827
credID string
828828
wantErrContains string
829829
wantStatus int

0 commit comments

Comments
 (0)