Skip to content

Commit 99b18f8

Browse files
evantobinGiteaBot
authored andcommitted
Fix permissions for Token DELETE endpoint to match GET and POST (go-gitea#27610)
Fixes go-gitea#27598 In go-gitea#27080, the logic for the tokens endpoints were updated to allow admins to create and view tokens in other accounts. However, the same functionality was not added to the DELETE endpoint. This PR makes the DELETE endpoint function the same as the other token endpoints and adds unit tests
1 parent 9f63d27 commit 99b18f8

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

routers/api/v1/user/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func DeleteAccessToken(ctx *context.APIContext) {
193193
return
194194
}
195195

196-
if err := auth_model.DeleteAccessTokenByID(ctx, tokenID, ctx.Doer.ID); err != nil {
196+
if err := auth_model.DeleteAccessTokenByID(ctx, tokenID, ctx.ContextUser.ID); err != nil {
197197
if auth_model.IsErrAccessTokenNotExist(err) {
198198
ctx.NotFound()
199199
} else {

tests/integration/api_token_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ func TestAPIGetTokensPermission(t *testing.T) {
6363
MakeRequest(t, req, http.StatusForbidden)
6464
}
6565

66+
// TestAPIDeleteTokensPermission ensures that only the admin can delete tokens from other users
67+
func TestAPIDeleteTokensPermission(t *testing.T) {
68+
defer tests.PrepareTestEnv(t)()
69+
70+
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
71+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
72+
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
73+
74+
// admin can delete tokens for other users
75+
createAPIAccessTokenWithoutCleanUp(t, "test-key-1", user2, nil)
76+
req := NewRequestf(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-1")
77+
req = AddBasicAuthHeader(req, admin.Name)
78+
MakeRequest(t, req, http.StatusNoContent)
79+
80+
// non-admin can delete tokens for himself
81+
createAPIAccessTokenWithoutCleanUp(t, "test-key-2", user2, nil)
82+
req = NewRequestf(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-2")
83+
req = AddBasicAuthHeader(req, user2.Name)
84+
MakeRequest(t, req, http.StatusNoContent)
85+
86+
// non-admin can't delete tokens for other users
87+
createAPIAccessTokenWithoutCleanUp(t, "test-key-3", user2, nil)
88+
req = NewRequestf(t, "DELETE", "/api/v1/users/"+user2.LoginName+"/tokens/test-key-3")
89+
req = AddBasicAuthHeader(req, user4.Name)
90+
MakeRequest(t, req, http.StatusForbidden)
91+
}
92+
6693
type permission struct {
6794
category auth_model.AccessTokenScopeCategory
6895
level auth_model.AccessTokenScopeLevel
@@ -526,7 +553,7 @@ func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *us
526553
}
527554
}
528555
log.Debug("Requesting creation of token with scopes: %v", scopes)
529-
req := NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", payload)
556+
req := NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", payload)
530557

531558
req = AddBasicAuthHeader(req, user.Name)
532559
resp := MakeRequest(t, req, http.StatusCreated)
@@ -546,7 +573,7 @@ func createAPIAccessTokenWithoutCleanUp(t *testing.T, tokenName string, user *us
546573
// createAPIAccessTokenWithoutCleanUp Delete an API access token and assert that
547574
// deletion succeeded.
548575
func deleteAPIAccessToken(t *testing.T, accessToken api.AccessToken, user *user_model.User) {
549-
req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", accessToken.ID)
576+
req := NewRequestf(t, "DELETE", "/api/v1/users/"+user.LoginName+"/tokens/%d", accessToken.ID)
550577
req = AddBasicAuthHeader(req, user.Name)
551578
MakeRequest(t, req, http.StatusNoContent)
552579

0 commit comments

Comments
 (0)