Skip to content

Commit d5b6931

Browse files
6543zeripathlafriks
authored
[API] Delete Token accept names too (#12366)
* Delete Token accept names too * better description Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent eb1bf23 commit d5b6931

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

integrations/api_token_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ func TestAPICreateAndDeleteToken(t *testing.T) {
3737
MakeRequest(t, req, http.StatusNoContent)
3838

3939
models.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID})
40+
41+
req = NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{
42+
"name": "test-key-2",
43+
})
44+
req = AddBasicAuthHeader(req, user.Name)
45+
resp = MakeRequest(t, req, http.StatusCreated)
46+
DecodeJSON(t, resp, &newAccessToken)
47+
48+
req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%s", newAccessToken.Name)
49+
req = AddBasicAuthHeader(req, user.Name)
50+
MakeRequest(t, req, http.StatusNoContent)
51+
52+
models.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID})
4053
}
4154

4255
// TestAPIDeleteMissingToken ensures that error is thrown when token not found

models/token.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,27 @@ func AccessTokenByNameExists(token *AccessToken) (bool, error) {
8282
return x.Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
8383
}
8484

85+
// ListAccessTokensOptions contain filter options
86+
type ListAccessTokensOptions struct {
87+
ListOptions
88+
Name string
89+
UserID int64
90+
}
91+
8592
// ListAccessTokens returns a list of access tokens belongs to given user.
86-
func ListAccessTokens(uid int64, listOptions ListOptions) ([]*AccessToken, error) {
87-
sess := x.
88-
Where("uid=?", uid).
89-
Desc("id")
93+
func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
94+
sess := x.Where("uid=?", opts.UserID)
95+
96+
if len(opts.Name) != 0 {
97+
sess = sess.Where("name=?", opts.Name)
98+
}
99+
100+
sess = sess.Desc("id")
90101

91-
if listOptions.Page != 0 {
92-
sess = listOptions.setSessionPagination(sess)
102+
if opts.Page != 0 {
103+
sess = opts.setSessionPagination(sess)
93104

94-
tokens := make([]*AccessToken, 0, listOptions.PageSize)
105+
tokens := make([]*AccessToken, 0, opts.PageSize)
95106
return tokens, sess.Find(&tokens)
96107
}
97108

models/token_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestGetAccessTokenBySHA(t *testing.T) {
8383

8484
func TestListAccessTokens(t *testing.T) {
8585
assert.NoError(t, PrepareTestDatabase())
86-
tokens, err := ListAccessTokens(1, ListOptions{})
86+
tokens, err := ListAccessTokens(ListAccessTokensOptions{UserID: 1})
8787
assert.NoError(t, err)
8888
if assert.Len(t, tokens, 2) {
8989
assert.Equal(t, int64(1), tokens[0].UID)
@@ -92,14 +92,14 @@ func TestListAccessTokens(t *testing.T) {
9292
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
9393
}
9494

95-
tokens, err = ListAccessTokens(2, ListOptions{})
95+
tokens, err = ListAccessTokens(ListAccessTokensOptions{UserID: 2})
9696
assert.NoError(t, err)
9797
if assert.Len(t, tokens, 1) {
9898
assert.Equal(t, int64(2), tokens[0].UID)
9999
assert.Equal(t, "Token A", tokens[0].Name)
100100
}
101101

102-
tokens, err = ListAccessTokens(100, ListOptions{})
102+
tokens, err = ListAccessTokens(ListAccessTokensOptions{UserID: 100})
103103
assert.NoError(t, err)
104104
assert.Empty(t, tokens)
105105
}

routers/api/v1/user/app.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package user
77

88
import (
99
"errors"
10+
"fmt"
1011
"net/http"
12+
"strconv"
1113

1214
"code.gitea.io/gitea/models"
1315
"code.gitea.io/gitea/modules/context"
@@ -41,7 +43,7 @@ func ListAccessTokens(ctx *context.APIContext) {
4143
// "200":
4244
// "$ref": "#/responses/AccessTokenList"
4345

44-
tokens, err := models.ListAccessTokens(ctx.User.ID, utils.GetListOptions(ctx))
46+
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID, ListOptions: utils.GetListOptions(ctx)})
4547
if err != nil {
4648
ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err)
4749
return
@@ -128,15 +130,44 @@ func DeleteAccessToken(ctx *context.APIContext) {
128130
// required: true
129131
// - name: token
130132
// in: path
131-
// description: token to be deleted
132-
// type: integer
133-
// format: int64
133+
// description: token to be deleted, identified by ID and if not available by name
134+
// type: string
134135
// required: true
135136
// responses:
136137
// "204":
137138
// "$ref": "#/responses/empty"
139+
// "422":
140+
// "$ref": "#/responses/error"
141+
142+
token := ctx.Params(":id")
143+
tokenID, _ := strconv.ParseInt(token, 0, 64)
144+
145+
if tokenID == 0 {
146+
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{
147+
Name: token,
148+
UserID: ctx.User.ID,
149+
})
150+
if err != nil {
151+
ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err)
152+
return
153+
}
154+
155+
switch len(tokens) {
156+
case 0:
157+
ctx.NotFound()
158+
return
159+
case 1:
160+
tokenID = tokens[0].ID
161+
default:
162+
ctx.Error(http.StatusUnprocessableEntity, "DeleteAccessTokenByID", fmt.Errorf("multible matches for token name '%s'", token))
163+
return
164+
}
165+
}
166+
if tokenID == 0 {
167+
ctx.Error(http.StatusInternalServerError, "Invalid TokenID", nil)
168+
return
169+
}
138170

139-
tokenID := ctx.ParamsInt64(":id")
140171
if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil {
141172
if models.IsErrAccessTokenNotExist(err) {
142173
ctx.NotFound()

routers/user/setting/applications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func DeleteApplication(ctx *context.Context) {
8080
}
8181

8282
func loadApplicationsData(ctx *context.Context) {
83-
tokens, err := models.ListAccessTokens(ctx.User.ID, models.ListOptions{})
83+
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID})
8484
if err != nil {
8585
ctx.ServerError("ListAccessTokens", err)
8686
return

routers/user/setting/security.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func loadSecurityData(ctx *context.Context) {
7171
ctx.Data["RequireU2F"] = true
7272
}
7373

74-
tokens, err := models.ListAccessTokens(ctx.User.ID, models.ListOptions{})
74+
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID})
7575
if err != nil {
7676
ctx.ServerError("ListAccessTokens", err)
7777
return

templates/swagger/v1_json.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10635,9 +10635,8 @@
1063510635
"required": true
1063610636
},
1063710637
{
10638-
"type": "integer",
10639-
"format": "int64",
10640-
"description": "token to be deleted",
10638+
"type": "string",
10639+
"description": "token to be deleted, identified by ID and if not available by name",
1064110640
"name": "token",
1064210641
"in": "path",
1064310642
"required": true
@@ -10646,6 +10645,9 @@
1064610645
"responses": {
1064710646
"204": {
1064810647
"$ref": "#/responses/empty"
10648+
},
10649+
"422": {
10650+
"$ref": "#/responses/error"
1064910651
}
1065010652
}
1065110653
}

0 commit comments

Comments
 (0)