Skip to content

Commit 0c0092a

Browse files
authored
Merge pull request #156 from sil-org/develop
Release 3.1.0 - Return key ID from CreateKey request
2 parents 001dd48 + cab4084 commit 0c0092a

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed

.github/workflows/test-deploy-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
build-and-publish:
101101
name: Build and Publish
102102
needs: [ 'tests', 'lint' ]
103-
if: github.ref_name == 'main' || github.ref_name == 'develop'
103+
if: github.ref_name == 'main' || github.ref_name == 'develop' || startsWith(github.ref, 'refs/tags/')
104104
runs-on: ubuntu-latest
105105
steps:
106106
- name: Checkout code

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+
"apiKeyValue": key.Key,
444+
"createdAt": 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+
APIKeyValue string `json:"apiKeyValue"`
339+
CreatedAt time.Time `json:"createdAt"`
340+
}
341+
ms.NoError(json.Unmarshal(res.Body, &response))
342+
ms.Equal(exampleEmail, response.Email)
343+
ms.Regexp("^[0-9a-z]{40}$", response.APIKeyValue)
344+
ms.WithinDuration(time.Now().UTC(), response.CreatedAt, time.Minute)
335345
})
336346
}
337347
}

auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func AuthenticateRequest(r *http.Request) (User, error) {
5353
return authTOTP(apiKey)
5454

5555
case "api-key":
56-
return nil, nil // no authentication required for api-key
56+
return apiKey, nil
5757

5858
default:
5959
return nil, fmt.Errorf("invalid URL: %s", r.URL)

openapi.yaml

Lines changed: 26 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 address of the requester
376+
required: true
377+
example: "[email protected]"
378+
apiKeyValue:
379+
type: string
380+
description: Unique ID for the new API Key
381+
required: true
382+
example: "0123456789abcdef0123456789abcdef01234567"
383+
createdAt:
384+
type: string
385+
description: Time the key was created
386+
required: true
387+
example: "2006-01-02T15:04:05Z"
369388
400:
370389
description: Bad Request
371390
content:
@@ -379,6 +398,8 @@ paths:
379398
email-required:
380399
value:
381400
error: "email is required"
401+
"401":
402+
$ref: "#/components/responses/UnauthorizedError"
382403
/api-key/activate:
383404
post:
384405
operationId: activateApiKey
@@ -430,6 +451,8 @@ paths:
430451
email-required:
431452
value:
432453
error: "email is required"
454+
"401":
455+
$ref: "#/components/responses/UnauthorizedError"
433456
404:
434457
description: Not Found
435458
content:
@@ -514,6 +537,8 @@ paths:
514537
api-key-required:
515538
value:
516539
error: "apiKeyValue is required"
540+
"401":
541+
$ref: "#/components/responses/UnauthorizedError"
517542
404:
518543
description: Not Found
519544
content:

router/middleware.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"log"
66
"net/http"
7-
"strings"
87

98
mfa "github.com/sil-org/serverless-mfa-api-go"
109
)
@@ -14,11 +13,6 @@ import (
1413
// user from storage and attach to context.
1514
func authenticationMiddleware(next http.Handler) http.Handler {
1615
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17-
if strings.HasPrefix(r.URL.Path, "/api-key") {
18-
next.ServeHTTP(w, r)
19-
return
20-
}
21-
2216
user, err := mfa.AuthenticateRequest(r)
2317
if err != nil {
2418
log.Printf("unable to authenticate request: %s", err)

0 commit comments

Comments
 (0)