|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-webauthn/webauthn/protocol" |
| 7 | + "github.com/gofrs/uuid" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | + "github.com/supabase/auth/internal/conf" |
| 11 | + "github.com/supabase/auth/internal/models" |
| 12 | +) |
| 13 | + |
| 14 | +type PasskeyWebAuthnTestSuite struct { |
| 15 | + suite.Suite |
| 16 | + API *API |
| 17 | + Config *conf.GlobalConfiguration |
| 18 | +} |
| 19 | + |
| 20 | +func TestPasskeyWebAuthn(t *testing.T) { |
| 21 | + api, config, err := setupAPIForTest() |
| 22 | + require.NoError(t, err) |
| 23 | + ts := &PasskeyWebAuthnTestSuite{ |
| 24 | + API: api, |
| 25 | + Config: config, |
| 26 | + } |
| 27 | + defer api.db.Close() |
| 28 | + suite.Run(t, ts) |
| 29 | +} |
| 30 | + |
| 31 | +func (ts *PasskeyWebAuthnTestSuite) TestGetPasskeyWebAuthn() { |
| 32 | + ts.API.config.WebAuthn = conf.WebAuthnConfiguration{ |
| 33 | + RPID: "example.com", |
| 34 | + RPDisplayName: "Example App", |
| 35 | + RPOrigins: []string{"https://example.com"}, |
| 36 | + } |
| 37 | + |
| 38 | + wn, err := ts.API.getPasskeyWebAuthn() |
| 39 | + ts.Require().NoError(err) |
| 40 | + ts.Require().NotNil(wn) |
| 41 | +} |
| 42 | + |
| 43 | +func (ts *PasskeyWebAuthnTestSuite) TestGetPasskeyWebAuthnMultipleOrigins() { |
| 44 | + ts.API.config.WebAuthn = conf.WebAuthnConfiguration{ |
| 45 | + RPID: "example.com", |
| 46 | + RPDisplayName: "Example App", |
| 47 | + RPOrigins: []string{"https://example.com", "https://app.example.com"}, |
| 48 | + } |
| 49 | + |
| 50 | + wn, err := ts.API.getPasskeyWebAuthn() |
| 51 | + ts.Require().NoError(err) |
| 52 | + ts.Require().NotNil(wn) |
| 53 | +} |
| 54 | + |
| 55 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnUserWithEmail() { |
| 56 | + userID := uuid.Must(uuid.NewV4()) |
| 57 | + user := &models.User{ |
| 58 | + ID: userID, |
| 59 | + } |
| 60 | + user.Email = "user@example.com" |
| 61 | + |
| 62 | + wu := newWebAuthnUser(user, nil) |
| 63 | + |
| 64 | + ts.Equal([]byte(userID.String()), wu.WebAuthnID()) |
| 65 | + ts.Equal("user@example.com", wu.WebAuthnName()) |
| 66 | + ts.Equal("user@example.com", wu.WebAuthnDisplayName()) |
| 67 | + ts.Empty(wu.WebAuthnCredentials()) |
| 68 | +} |
| 69 | + |
| 70 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnUserWithPhone() { |
| 71 | + userID := uuid.Must(uuid.NewV4()) |
| 72 | + user := &models.User{ |
| 73 | + ID: userID, |
| 74 | + } |
| 75 | + user.Phone = "+1234567890" |
| 76 | + |
| 77 | + wu := newWebAuthnUser(user, nil) |
| 78 | + |
| 79 | + ts.Equal("+1234567890", wu.WebAuthnName()) |
| 80 | + ts.Equal("+1234567890", wu.WebAuthnDisplayName()) |
| 81 | +} |
| 82 | + |
| 83 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnUserEmailTakesPrecedence() { |
| 84 | + user := &models.User{ |
| 85 | + ID: uuid.Must(uuid.NewV4()), |
| 86 | + } |
| 87 | + user.Email = "user@example.com" |
| 88 | + user.Phone = "+1234567890" |
| 89 | + |
| 90 | + wu := newWebAuthnUser(user, nil) |
| 91 | + |
| 92 | + ts.Equal("user@example.com", wu.WebAuthnName()) |
| 93 | +} |
| 94 | + |
| 95 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnDisplayNameFromMetadata() { |
| 96 | + user := &models.User{ |
| 97 | + ID: uuid.Must(uuid.NewV4()), |
| 98 | + UserMetaData: map[string]any{ |
| 99 | + "name": "John Doe", |
| 100 | + }, |
| 101 | + } |
| 102 | + user.Email = "user@example.com" |
| 103 | + |
| 104 | + wu := newWebAuthnUser(user, nil) |
| 105 | + |
| 106 | + ts.Equal("user@example.com", wu.WebAuthnName()) |
| 107 | + ts.Equal("John Doe", wu.WebAuthnDisplayName()) |
| 108 | +} |
| 109 | + |
| 110 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnDisplayNameFallsBackToEmail() { |
| 111 | + user := &models.User{ |
| 112 | + ID: uuid.Must(uuid.NewV4()), |
| 113 | + UserMetaData: map[string]any{}, |
| 114 | + } |
| 115 | + user.Email = "user@example.com" |
| 116 | + |
| 117 | + wu := newWebAuthnUser(user, nil) |
| 118 | + |
| 119 | + ts.Equal("user@example.com", wu.WebAuthnDisplayName()) |
| 120 | +} |
| 121 | + |
| 122 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnDisplayNameSkipsEmptyMetadataName() { |
| 123 | + user := &models.User{ |
| 124 | + ID: uuid.Must(uuid.NewV4()), |
| 125 | + UserMetaData: map[string]any{ |
| 126 | + "name": "", |
| 127 | + }, |
| 128 | + } |
| 129 | + user.Email = "user@example.com" |
| 130 | + |
| 131 | + wu := newWebAuthnUser(user, nil) |
| 132 | + |
| 133 | + ts.Equal("user@example.com", wu.WebAuthnDisplayName()) |
| 134 | +} |
| 135 | + |
| 136 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnDisplayNameNilMetadata() { |
| 137 | + user := &models.User{ |
| 138 | + ID: uuid.Must(uuid.NewV4()), |
| 139 | + } |
| 140 | + user.Phone = "+1234567890" |
| 141 | + |
| 142 | + wu := newWebAuthnUser(user, nil) |
| 143 | + |
| 144 | + ts.Equal("+1234567890", wu.WebAuthnDisplayName()) |
| 145 | +} |
| 146 | + |
| 147 | +func (ts *PasskeyWebAuthnTestSuite) TestWebAuthnUserWithCredentials() { |
| 148 | + user := &models.User{ |
| 149 | + ID: uuid.Must(uuid.NewV4()), |
| 150 | + } |
| 151 | + user.Email = "user@example.com" |
| 152 | + |
| 153 | + creds := []*models.WebAuthnCredential{ |
| 154 | + { |
| 155 | + ID: uuid.Must(uuid.NewV4()), |
| 156 | + UserID: user.ID, |
| 157 | + CredentialID: []byte("cred-1"), |
| 158 | + PublicKey: []byte("pk-1"), |
| 159 | + AttestationType: "none", |
| 160 | + SignCount: 5, |
| 161 | + Transports: models.WebAuthnTransports{protocol.USB}, |
| 162 | + BackupEligible: true, |
| 163 | + BackedUp: false, |
| 164 | + }, |
| 165 | + { |
| 166 | + ID: uuid.Must(uuid.NewV4()), |
| 167 | + UserID: user.ID, |
| 168 | + CredentialID: []byte("cred-2"), |
| 169 | + PublicKey: []byte("pk-2"), |
| 170 | + AttestationType: "none", |
| 171 | + SignCount: 0, |
| 172 | + Transports: models.WebAuthnTransports{protocol.Internal}, |
| 173 | + BackupEligible: true, |
| 174 | + BackedUp: true, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + wu := newWebAuthnUser(user, creds) |
| 179 | + |
| 180 | + webauthnCreds := wu.WebAuthnCredentials() |
| 181 | + ts.Require().Len(webauthnCreds, 2) |
| 182 | + |
| 183 | + ts.Equal([]byte("cred-1"), webauthnCreds[0].ID) |
| 184 | + ts.Equal([]byte("pk-1"), webauthnCreds[0].PublicKey) |
| 185 | + ts.Equal(uint32(5), webauthnCreds[0].Authenticator.SignCount) |
| 186 | + ts.True(webauthnCreds[0].Flags.BackupEligible) |
| 187 | + ts.False(webauthnCreds[0].Flags.BackupState) |
| 188 | + |
| 189 | + ts.Equal([]byte("cred-2"), webauthnCreds[1].ID) |
| 190 | + ts.True(webauthnCreds[1].Flags.BackupEligible) |
| 191 | + ts.True(webauthnCreds[1].Flags.BackupState) |
| 192 | +} |
0 commit comments