@@ -7,7 +7,9 @@ package user
77
88import (
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 ()
0 commit comments