Skip to content

Commit aa70891

Browse files
authored
Merge pull request #218 from SkynetLabs/ivo/apikeys
Allow API keys on additional endpoints.
2 parents 32db213 + 97a4f9f commit aa70891

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

api/routes.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func (api *API) buildHTTPRoutes() {
6464
api.staticRouter.GET("/user/downloads", api.withAuth(api.userDownloadsGET, false))
6565

6666
// Endpoints for user API keys.
67-
api.staticRouter.POST("/user/apikeys", api.WithDBSession(api.withAuth(api.userAPIKeyPOST, false)))
68-
api.staticRouter.GET("/user/apikeys", api.withAuth(api.userAPIKeyLIST, false))
69-
api.staticRouter.GET("/user/apikeys/:id", api.withAuth(api.userAPIKeyGET, false))
70-
api.staticRouter.PUT("/user/apikeys/:id", api.WithDBSession(api.withAuth(api.userAPIKeyPUT, false)))
71-
api.staticRouter.PATCH("/user/apikeys/:id", api.WithDBSession(api.withAuth(api.userAPIKeyPATCH, false)))
72-
api.staticRouter.DELETE("/user/apikeys/:id", api.withAuth(api.userAPIKeyDELETE, false))
67+
api.staticRouter.POST("/user/apikeys", api.WithDBSession(api.withAuth(api.userAPIKeyPOST, true)))
68+
api.staticRouter.GET("/user/apikeys", api.withAuth(api.userAPIKeyLIST, true))
69+
api.staticRouter.GET("/user/apikeys/:id", api.withAuth(api.userAPIKeyGET, true))
70+
api.staticRouter.PUT("/user/apikeys/:id", api.WithDBSession(api.withAuth(api.userAPIKeyPUT, true)))
71+
api.staticRouter.PATCH("/user/apikeys/:id", api.WithDBSession(api.withAuth(api.userAPIKeyPATCH, true)))
72+
api.staticRouter.DELETE("/user/apikeys/:id", api.withAuth(api.userAPIKeyDELETE, true))
7373

7474
// Endpoints for email communication with the user.
7575
api.staticRouter.GET("/user/confirm", api.WithDBSession(api.noAuth(api.userConfirmGET))) // TODO POST

test/api/apikeys_test.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func testAPIKeysAcceptance(t *testing.T, at *test.AccountsTester) {
331331
// Stop using the cookie, use the public API key instead.
332332
at.SetAPIKey(pakWithKey.Key.String())
333333

334-
// Call all routes that shouldn't accept API keys and make sure they return
334+
// Call all routes that should NOT accept API keys and make sure they return
335335
// the right error.
336336
tests := []struct {
337337
verb string
@@ -348,19 +348,38 @@ func testAPIKeysAcceptance(t *testing.T, at *test.AccountsTester) {
348348
{verb: http.MethodGet, endpoint: "/user/uploads"},
349349
{verb: http.MethodDelete, endpoint: "/user/uploads/someSkylink"},
350350
{verb: http.MethodGet, endpoint: "/user/downloads"},
351+
{verb: http.MethodPost, endpoint: "/user/reconfirm"},
352+
}
353+
354+
for _, tt := range tests {
355+
r, err = at.Request(tt.verb, tt.endpoint, nil, nil, nil, nil)
356+
if err == nil || r.StatusCode != http.StatusUnauthorized || !strings.Contains(err.Error(), api.ErrAPIKeyNotAllowed.Error()) {
357+
t.Errorf("Expected error '%s' with status %d, got '%s' with status %d. Endpoint %s %s", api.ErrAPIKeyNotAllowed, http.StatusUnauthorized, err, r.StatusCode, tt.verb, tt.endpoint)
358+
}
359+
}
360+
361+
// Call all routes that SHOULD accept API keys and make sure they don't
362+
// return an API key acceptance error.
363+
tests = []struct {
364+
verb string
365+
endpoint string
366+
}{
367+
{verb: http.MethodPost, endpoint: "/track/upload/:skylink"},
368+
{verb: http.MethodPost, endpoint: "/track/download/:skylink"},
369+
{verb: http.MethodPost, endpoint: "/track/registry/read"},
370+
{verb: http.MethodPost, endpoint: "/track/registry/write"},
351371
{verb: http.MethodPost, endpoint: "/user/apikeys"},
352372
{verb: http.MethodGet, endpoint: "/user/apikeys"},
353373
{verb: http.MethodGet, endpoint: "/user/apikeys/someId"},
354374
{verb: http.MethodPut, endpoint: "/user/apikeys/someId"},
355375
{verb: http.MethodPatch, endpoint: "/user/apikeys/someId"},
356376
{verb: http.MethodDelete, endpoint: "/user/apikeys/someId"},
357-
{verb: http.MethodPost, endpoint: "/user/reconfirm"},
358377
}
359378

360379
for _, tt := range tests {
361380
r, err = at.Request(tt.verb, tt.endpoint, nil, nil, nil, nil)
362-
if err == nil || r.StatusCode != http.StatusUnauthorized || !strings.Contains(err.Error(), api.ErrAPIKeyNotAllowed.Error()) {
363-
t.Errorf("Expected error '%s' with status %d, got '%s' with status %d. Endpoint %s %s", api.ErrAPIKeyNotAllowed, http.StatusUnauthorized, err, r.StatusCode, tt.verb, tt.endpoint)
381+
if err != nil && strings.Contains(err.Error(), api.ErrAPIKeyNotAllowed.Error()) {
382+
t.Errorf("Unexpected error '%s'. Endpoint %s %s", err, tt.verb, tt.endpoint)
364383
}
365384
}
366385
}

0 commit comments

Comments
 (0)