Skip to content

Commit e0b4630

Browse files
committed
return the key in the response to the POST /api-key request
1 parent 8fb13e8 commit e0b4630

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

apikey.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,12 @@ func (a *App) CreateApiKey(w http.ResponseWriter, r *http.Request) {
438438
return
439439
}
440440

441-
jsonResponse(w, nil, http.StatusNoContent)
441+
response := map[string]string{
442+
"email": key.Email,
443+
"id": key.Key,
444+
"created_at": time.Unix(int64(key.CreatedAt)/1000, 0).UTC().Format(time.RFC3339),
445+
}
446+
jsonResponse(w, response, http.StatusOK)
442447
}
443448

444449
// RotateApiKey facilitates the rotation of API Keys. All data in webauthn and totp tables that is encrypted by the old

apikey_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (ms *MfaSuite) TestCreateApiKey() {
308308
body: map[string]interface{}{
309309
"email": exampleEmail,
310310
},
311-
wantStatus: http.StatusNoContent,
311+
wantStatus: http.StatusOK,
312312
},
313313
{
314314
name: "missing email",
@@ -332,6 +332,16 @@ func (ms *MfaSuite) TestCreateApiKey() {
332332
}
333333

334334
ms.Equal(tt.wantStatus, res.Status, fmt.Sprintf("CreateApiKey response: %s", res.Body))
335+
336+
var response struct {
337+
Email string `json:"email"`
338+
ID string `json:"id"`
339+
CreatedAt time.Time `json:"created_at"`
340+
}
341+
ms.NoError(json.Unmarshal(res.Body, &response))
342+
ms.Equal(exampleEmail, response.Email)
343+
ms.Regexp("^[0-9a-z]{40}$", response.ID)
344+
ms.WithinDuration(time.Now().UTC(), response.CreatedAt, time.Minute)
335345
})
336346
}
337347
}

openapi.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,27 @@ paths:
364364
required: true
365365
366366
responses:
367-
204:
367+
200:
368368
description: New API key created
369+
content:
370+
application/json:
371+
schema:
372+
properties:
373+
email:
374+
type: string
375+
description: email
376+
required: true
377+
example: "[email protected]"
378+
id:
379+
type: string
380+
description: id
381+
required: true
382+
example: "0123456789abcdef0123456789abcdef01234567"
383+
created_at:
384+
type: string
385+
description: created_at
386+
required: true
387+
example: "2006-01-02T15:04:05Z"
369388
400:
370389
description: Bad Request
371390
content:

0 commit comments

Comments
 (0)