diff options
author | 6543 <6543@obermui.de> | 2020-08-28 10:09:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-28 11:09:33 +0300 |
commit | d5b6931dbefff674fd84237555a7c2e284b63c5a (patch) | |
tree | 8e68a4f12b16ed1ae782668459cfcb19e78fa7d4 /routers | |
parent | eb1bf2377be15deb593a3e2426558d92c1973107 (diff) | |
download | gitea-d5b6931dbefff674fd84237555a7c2e284b63c5a.tar.gz gitea-d5b6931dbefff674fd84237555a7c2e284b63c5a.zip |
[API] Delete Token accept names too (#12366)
* Delete Token accept names too
* better description
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/user/app.go | 41 | ||||
-rw-r--r-- | routers/user/setting/applications.go | 2 | ||||
-rw-r--r-- | routers/user/setting/security.go | 2 |
3 files changed, 38 insertions, 7 deletions
diff --git a/routers/api/v1/user/app.go b/routers/api/v1/user/app.go index 624beff5bb..d02b8cea21 100644 --- a/routers/api/v1/user/app.go +++ b/routers/api/v1/user/app.go @@ -7,7 +7,9 @@ package user import ( "errors" + "fmt" "net/http" + "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -41,7 +43,7 @@ func ListAccessTokens(ctx *context.APIContext) { // "200": // "$ref": "#/responses/AccessTokenList" - tokens, err := models.ListAccessTokens(ctx.User.ID, utils.GetListOptions(ctx)) + tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID, ListOptions: utils.GetListOptions(ctx)}) if err != nil { ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) return @@ -128,15 +130,44 @@ func DeleteAccessToken(ctx *context.APIContext) { // required: true // - name: token // in: path - // description: token to be deleted - // type: integer - // format: int64 + // description: token to be deleted, identified by ID and if not available by name + // type: string // required: true // responses: // "204": // "$ref": "#/responses/empty" + // "422": + // "$ref": "#/responses/error" + + token := ctx.Params(":id") + tokenID, _ := strconv.ParseInt(token, 0, 64) + + if tokenID == 0 { + tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{ + Name: token, + UserID: ctx.User.ID, + }) + if err != nil { + ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) + return + } + + switch len(tokens) { + case 0: + ctx.NotFound() + return + case 1: + tokenID = tokens[0].ID + default: + ctx.Error(http.StatusUnprocessableEntity, "DeleteAccessTokenByID", fmt.Errorf("multible matches for token name '%s'", token)) + return + } + } + if tokenID == 0 { + ctx.Error(http.StatusInternalServerError, "Invalid TokenID", nil) + return + } - tokenID := ctx.ParamsInt64(":id") if err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil { if models.IsErrAccessTokenNotExist(err) { ctx.NotFound() diff --git a/routers/user/setting/applications.go b/routers/user/setting/applications.go index febb5b0c19..04f9d9f7f9 100644 --- a/routers/user/setting/applications.go +++ b/routers/user/setting/applications.go @@ -80,7 +80,7 @@ func DeleteApplication(ctx *context.Context) { } func loadApplicationsData(ctx *context.Context) { - tokens, err := models.ListAccessTokens(ctx.User.ID, models.ListOptions{}) + tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID}) if err != nil { ctx.ServerError("ListAccessTokens", err) return diff --git a/routers/user/setting/security.go b/routers/user/setting/security.go index c7c3226c9b..787ac922ec 100644 --- a/routers/user/setting/security.go +++ b/routers/user/setting/security.go @@ -71,7 +71,7 @@ func loadSecurityData(ctx *context.Context) { ctx.Data["RequireU2F"] = true } - tokens, err := models.ListAccessTokens(ctx.User.ID, models.ListOptions{}) + tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID}) if err != nil { ctx.ServerError("ListAccessTokens", err) return |