diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 89338d6977..8e1ccdc5e2 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -908,9 +908,9 @@ func Routes() *web.Route { m.Get("/repos", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), reqExploreSignIn(), user.ListUserRepos) m.Group("/tokens", func() { m.Combo("").Get(user.ListAccessTokens). - Post(bind(api.CreateAccessTokenOption{}), reqToken(), user.CreateAccessToken) - m.Combo("/{id}").Delete(reqToken(), user.DeleteAccessToken) - }, reqSelfOrAdmin(), reqBasicOrRevProxyAuth()) + Post(bind(api.CreateAccessTokenOption{}), reqBasicOrRevProxyAuth(), reqToken(), user.CreateAccessToken) + m.Combo("/{id}").Delete(reqBasicOrRevProxyAuth(), reqToken(), user.DeleteAccessToken) + }, reqSelfOrAdmin()) m.Get("/activities/feeds", user.ListUserActivityFeeds) }, context.UserAssignmentAPI(), checkTokenPublicOnly(), individualPermsChecker) diff --git a/tests/integration/api_token_test.go b/tests/integration/api_token_test.go index 01d18ef6f1..f94a0986f2 100644 --- a/tests/integration/api_token_test.go +++ b/tests/integration/api_token_test.go @@ -30,6 +30,23 @@ func TestAPICreateAndDeleteToken(t *testing.T) { deleteAPIAccessToken(t, newAccessToken, user) } +func TestAPIGetTokens(t *testing.T) { + defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + + // with basic auth... + req := NewRequest(t, "GET", "/api/v1/users/user2/tokens"). + AddBasicAuth(user.Name) + MakeRequest(t, req, http.StatusOK) + + // ... or with a token. + newAccessToken := createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user, []auth_model.AccessTokenScope{auth_model.AccessTokenScopeAll}) + req = NewRequest(t, "GET", "/api/v1/users/user2/tokens"). + AddTokenAuth(newAccessToken.Token) + MakeRequest(t, req, http.StatusOK) + deleteAPIAccessToken(t, newAccessToken, user) +} + // TestAPIDeleteMissingToken ensures that error is thrown when token not found func TestAPIDeleteMissingToken(t *testing.T) { defer tests.PrepareTestEnv(t)()