Skip to content

Commit f982e93

Browse files
committed
Merge branch 'develop' into feature/rotate-api-key
2 parents 62fd1b1 + fc195ab commit f982e93

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

apikey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (k *ApiKey) ReEncryptLegacy(oldKey ApiKey, v *string) error {
239239
// the updated data back to the database.
240240
func (k *ApiKey) ReEncryptTables(oldSecret string) error {
241241
var users []WebauthnUser
242-
err := k.Store.QueryApiKey(envConfig.WebauthnTable, k.Key, &users)
242+
err := k.Store.ScanApiKey(envConfig.WebauthnTable, k.Key, &users)
243243
if err != nil {
244244
return fmt.Errorf("failed to query %s table for key %s: %w", envConfig.WebauthnTable, k.Key, err)
245245
}

apikey_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (ms *MfaSuite) TestApiKey_EncryptDecryptLegacy() {
151151
func (ms *MfaSuite) TestApiKeyActivate() {
152152
notActive := ApiKey{
153153
Key: "0000000000000000000000000000000000000000",
154-
Email: "email@example.com",
154+
Email: exampleEmail,
155155
CreatedAt: 1744788331000,
156156
}
157157
active := notActive
@@ -216,41 +216,41 @@ func (ms *MfaSuite) TestActivateApiKey() {
216216
}{
217217
{
218218
name: "not previously activated",
219-
body: map[string]interface{}{
220-
"email": "email@example.com",
219+
body: map[string]any{
220+
"email": exampleEmail,
221221
"apiKeyValue": key1.Key,
222222
},
223223
wantStatus: http.StatusOK,
224224
},
225225
{
226226
name: "already activated",
227-
body: map[string]interface{}{
228-
"email": "email@example.com",
227+
body: map[string]any{
228+
"email": exampleEmail,
229229
"apiKeyValue": key2.Key,
230230
},
231231
wantStatus: http.StatusBadRequest,
232232
wantError: "failed to activate key: key already activated",
233233
},
234234
{
235235
name: "missing email",
236-
body: map[string]interface{}{
236+
body: map[string]any{
237237
"apiKeyValue": key3.Key,
238238
},
239239
wantStatus: http.StatusBadRequest,
240240
wantError: "email is required",
241241
},
242242
{
243243
name: "missing apiKey",
244-
body: map[string]interface{}{
245-
"email": "email@example.com",
244+
body: map[string]any{
245+
"email": exampleEmail,
246246
},
247247
wantStatus: http.StatusBadRequest,
248248
wantError: "apiKeyValue is required",
249249
},
250250
{
251251
name: "key not found",
252-
body: map[string]interface{}{
253-
"email": "email@example.com",
252+
body: map[string]any{
253+
"email": exampleEmail,
254254
"apiKeyValue": "not a key",
255255
},
256256
wantStatus: http.StatusNotFound,
@@ -264,14 +264,14 @@ func (ms *MfaSuite) TestActivateApiKey() {
264264
ActivateApiKey(res, req)
265265

266266
if tt.wantStatus != http.StatusOK {
267-
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("response: %s", res.Body))
267+
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("ActivateApiKey response: %s", res.Body))
268268
var se simpleError
269269
ms.decodeBody(res.Body, &se)
270270
ms.Equal(tt.wantError, se.Error)
271271
return
272272
}
273273

274-
ms.Equal(http.StatusOK, res.Status, fmt.Sprintf("response: %s", res.Body))
274+
ms.Equal(http.StatusOK, res.Status, fmt.Sprintf("ActivateApiKey response: %s", res.Body))
275275

276276
var response struct {
277277
ApiSecret string `json:"apiSecret"`
@@ -297,7 +297,7 @@ func (ms *MfaSuite) TestCreateApiKey() {
297297
{
298298
name: "success",
299299
body: map[string]interface{}{
300-
"email": "email@example.com",
300+
"email": exampleEmail,
301301
},
302302
wantStatus: http.StatusNoContent,
303303
},
@@ -315,20 +315,20 @@ func (ms *MfaSuite) TestCreateApiKey() {
315315
CreateApiKey(res, req)
316316

317317
if tt.wantError != "" {
318-
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("response: %s", res.Body))
318+
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("CreateApiKey response: %s", res.Body))
319319
var se simpleError
320320
ms.decodeBody(res.Body, &se)
321321
ms.Equal(tt.wantError, se.Error)
322322
return
323323
}
324324

325-
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("response: %s", res.Body))
325+
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("CreateApiKey response: %s", res.Body))
326326
})
327327
}
328328
}
329329

330330
func (ms *MfaSuite) TestNewApiKey() {
331-
got, err := NewApiKey("email@example.com")
331+
got, err := NewApiKey(exampleEmail)
332332
ms.NoError(err)
333333
ms.Regexp(regexp.MustCompile("[a-f0-9]{40}"), got)
334334
}

storage.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,22 @@ func (s *Storage) Delete(table, attrName, attrVal string) error {
107107
return err
108108
}
109109

110-
// QueryApiKey a table using apiKey-index
111-
func (s *Storage) QueryApiKey(table, apiKey string, items any) error {
110+
// ScanApiKey a table using apiKey-index
111+
func (s *Storage) ScanApiKey(table, apiKey string, items any) error {
112112
if table == "" {
113113
return tableNameMissingError
114114
}
115115

116-
input := &dynamodb.QueryInput{
117-
IndexName: aws.String("apiKey-index"),
118-
KeyConditionExpression: aws.String("apiKey = :val"),
116+
input := &dynamodb.ScanInput{
117+
FilterExpression: aws.String("apiKey = :val"),
119118
ExpressionAttributeValues: map[string]types.AttributeValue{
120119
":val": &types.AttributeValueMemberS{Value: apiKey},
121120
},
122121
TableName: aws.String(table),
123122
}
124123

125124
ctx := context.Background()
126-
result, err := s.client.Query(ctx, input)
125+
result, err := s.client.Scan(ctx, input)
127126
if err != nil {
128127
return err
129128
}

storage_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (ms *MfaSuite) TestStorage_StoreLoad() {
7373
}
7474
}
7575

76-
func (ms *MfaSuite) TestStorage_QueryApiKey() {
76+
func (ms *MfaSuite) TestStorageScanApiKey() {
7777
cfg, err := config.LoadDefaultConfig(
7878
context.Background(),
7979
config.WithRegion("local"),
@@ -95,7 +95,7 @@ func (ms *MfaSuite) TestStorage_QueryApiKey() {
9595
}))
9696

9797
var users []WebauthnUser
98-
err = s.QueryApiKey(TestTableName, "key1", &users)
98+
err = s.ScanApiKey(TestTableName, "key1", &users)
9999
ms.NoError(err)
100100
ms.Len(users, 1)
101101
ms.Equal("user1", users[0].ID)

utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
const localAppID = "http://localhost"
2424

25+
const exampleEmail = "email@example.com"
26+
2527
func testAwsConfig() aws.Config {
2628
cfg, err := config.LoadDefaultConfig(
2729
context.Background(),

0 commit comments

Comments
 (0)