Skip to content

Commit 21ee80f

Browse files
committed
add the full object response to ActivateApiKey
1 parent 38730bb commit 21ee80f

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

apikey.go

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

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

408415
// 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: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,55 +210,55 @@ func (ms *MfaSuite) TestActivateApiKey() {
210210
localStorage, err := NewStorage(awsConfig)
211211
must(err)
212212

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

220220
tests := []struct {
221221
name string
222-
body any
222+
body map[string]string
223223
wantStatus int
224224
wantError error
225225
}{
226226
{
227227
name: "not previously activated",
228-
body: map[string]any{
229-
"email": exampleEmail,
228+
body: map[string]string{
229+
"email": key1.Email,
230230
"apiKeyValue": key1.Key,
231231
},
232232
wantStatus: http.StatusOK,
233233
},
234234
{
235235
name: "already activated",
236-
body: map[string]any{
237-
"email": exampleEmail,
236+
body: map[string]string{
237+
"email": key2.Email,
238238
"apiKeyValue": key2.Key,
239239
},
240240
wantStatus: http.StatusBadRequest,
241241
wantError: ErrKeyAlreadyActivated,
242242
},
243243
{
244244
name: "missing email",
245-
body: map[string]any{
245+
body: map[string]string{
246246
"apiKeyValue": key3.Key,
247247
},
248248
wantStatus: http.StatusBadRequest,
249249
wantError: errors.New("email is required"),
250250
},
251251
{
252-
name: "missing apiKey",
253-
body: map[string]any{
252+
name: "missing apiKeyValue",
253+
body: map[string]string{
254254
"email": exampleEmail,
255255
},
256256
wantStatus: http.StatusBadRequest,
257257
wantError: errors.New("apiKeyValue is required"),
258258
},
259259
{
260260
name: "key not found",
261-
body: map[string]any{
261+
body: map[string]string{
262262
"email": exampleEmail,
263263
"apiKeyValue": "not a key",
264264
},
@@ -283,10 +283,17 @@ func (ms *MfaSuite) TestActivateApiKey() {
283283
ms.Equal(http.StatusOK, res.Status, fmt.Sprintf("ActivateApiKey response: %s", res.Body))
284284

285285
var response struct {
286-
ApiSecret string `json:"apiSecret"`
286+
Email string `json:"email"`
287+
ApiKeyValue string `json:"apiKeyValue"`
288+
ApiSecret string `json:"apiSecret"`
289+
ActivatedAt time.Time `json:"activatedAt"`
290+
CreatedAt time.Time `json:"createdAt"`
287291
}
288292
ms.NoError(json.Unmarshal(res.Body, &response))
289-
ms.Len(response.ApiSecret, 44)
293+
ms.Regexp("^[A-Za-z0-9+/]{43}=$", response.ApiSecret, "apiSecret isn't correct")
294+
ms.Equal(tt.body["email"], response.Email, "email isn't correct")
295+
ms.Equal(tt.body["apiKeyValue"], response.ApiKeyValue, "apiKeyValue isn't correct")
296+
ms.WithinDuration(time.Now().UTC(), response.ActivatedAt, time.Minute, "activatedAt isn't correct")
290297
})
291298
}
292299
}

openapi.yaml

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

0 commit comments

Comments
 (0)