Skip to content

Commit 45e52a9

Browse files
committed
Merge branch 'develop' into simplify-rotate
2 parents c5950bc + bddd35a commit 45e52a9

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

apikey.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,14 @@ func (a *App) ActivateApiKey(w http.ResponseWriter, r *http.Request) {
400400
return
401401
}
402402

403-
jsonResponse(w, map[string]string{"apiSecret": newKey.Secret}, http.StatusOK)
403+
response := map[string]string{
404+
"email": newKey.Email,
405+
"apiKeyValue": newKey.Key,
406+
"apiSecret": newKey.Secret,
407+
"activatedAt": time.Unix(int64(newKey.ActivatedAt)/1000, 0).UTC().Format(time.RFC3339),
408+
"createdAt": time.Unix(int64(newKey.CreatedAt)/1000, 0).UTC().Format(time.RFC3339),
409+
}
410+
jsonResponse(w, response, http.StatusOK)
404411
}
405412

406413
// CreateApiKey is the handler for the POST /api-key endpoint. It creates a new API Key and saves it to the database.

apikey_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,55 +212,55 @@ func (ms *MfaSuite) TestActivateApiKey() {
212212
localStorage, err := NewStorage(awsConfig)
213213
must(err)
214214

215-
key1 := ApiKey{Key: "key1"}
215+
key1 := ApiKey{Key: "key1", Email: "1" + exampleEmail, CreatedAt: 1744799133000}
216216
must(localStorage.Store(envConfig.ApiKeyTable, &key1))
217-
key2 := ApiKey{Key: "key2", ActivatedAt: 1744799134000}
217+
key2 := ApiKey{Key: "key2", Email: "2" + exampleEmail, CreatedAt: 1744799133000, ActivatedAt: 1744799134000}
218218
must(localStorage.Store(envConfig.ApiKeyTable, &key2))
219-
key3 := ApiKey{Key: "key3"}
219+
key3 := ApiKey{Key: "key3", Email: "3" + exampleEmail, CreatedAt: 1744799133000}
220220
must(localStorage.Store(envConfig.ApiKeyTable, &key3))
221221

222222
tests := []struct {
223223
name string
224-
body any
224+
body map[string]string
225225
wantStatus int
226226
wantError error
227227
}{
228228
{
229229
name: "not previously activated",
230-
body: map[string]any{
231-
"email": exampleEmail,
230+
body: map[string]string{
231+
"email": key1.Email,
232232
"apiKeyValue": key1.Key,
233233
},
234234
wantStatus: http.StatusOK,
235235
},
236236
{
237237
name: "already activated",
238-
body: map[string]any{
239-
"email": exampleEmail,
238+
body: map[string]string{
239+
"email": key2.Email,
240240
"apiKeyValue": key2.Key,
241241
},
242242
wantStatus: http.StatusBadRequest,
243243
wantError: ErrKeyAlreadyActivated,
244244
},
245245
{
246246
name: "missing email",
247-
body: map[string]any{
247+
body: map[string]string{
248248
"apiKeyValue": key3.Key,
249249
},
250250
wantStatus: http.StatusBadRequest,
251251
wantError: errors.New("email is required"),
252252
},
253253
{
254-
name: "missing apiKey",
255-
body: map[string]any{
254+
name: "missing apiKeyValue",
255+
body: map[string]string{
256256
"email": exampleEmail,
257257
},
258258
wantStatus: http.StatusBadRequest,
259259
wantError: errors.New("apiKeyValue is required"),
260260
},
261261
{
262262
name: "key not found",
263-
body: map[string]any{
263+
body: map[string]string{
264264
"email": exampleEmail,
265265
"apiKeyValue": "not a key",
266266
},
@@ -285,10 +285,18 @@ func (ms *MfaSuite) TestActivateApiKey() {
285285
ms.Equal(http.StatusOK, res.Status, fmt.Sprintf("ActivateApiKey response: %s", res.Body))
286286

287287
var response struct {
288-
ApiSecret string `json:"apiSecret"`
288+
Email string `json:"email"`
289+
ApiKeyValue string `json:"apiKeyValue"`
290+
ApiSecret string `json:"apiSecret"`
291+
ActivatedAt time.Time `json:"activatedAt"`
292+
CreatedAt time.Time `json:"createdAt"`
289293
}
290294
ms.NoError(json.Unmarshal(res.Body, &response))
291-
ms.Len(response.ApiSecret, 44)
295+
ms.Regexp("^[A-Za-z0-9+/]{43}=$", response.ApiSecret, "apiSecret isn't correct")
296+
ms.Equal(tt.body["email"], response.Email, "email isn't correct")
297+
ms.Equal(tt.body["apiKeyValue"], response.ApiKeyValue, "apiKeyValue isn't correct")
298+
ms.Equal(time.Date(2025, 4, 16, 10, 25, 33, 0, time.UTC), response.CreatedAt, "createdAt isn't correct")
299+
ms.WithinDuration(time.Now().UTC(), response.ActivatedAt, time.Minute, "activatedAt isn't correct")
292300
})
293301
}
294302
}

openapi.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,27 @@ paths:
437437
description: >
438438
a random string associated with the key, when paired with the key value can be used to
439439
authenticate against API endpoints
440+
required: false
441+
email:
442+
type: string
443+
description: Email address of the requester
440444
required: true
445+
example: "[email protected]"
446+
apiKeyValue:
447+
type: string
448+
description: Unique ID for the new API Key
449+
required: true
450+
example: "0123456789abcdef0123456789abcdef01234567"
451+
activatedAt:
452+
type: string
453+
description: Time the key was activated
454+
required: true
455+
example: "2006-01-02T15:04:05Z"
456+
createdAt:
457+
type: string
458+
description: Time the key was created
459+
required: true
460+
example: "2006-01-02T15:04:05Z"
441461
400:
442462
description: Bad Request
443463
content:

0 commit comments

Comments
 (0)