|
@ -7,7 +7,9 @@ package user |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"errors" |
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"strconv" |
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models" |
|
|
"code.gitea.io/gitea/models" |
|
|
"code.gitea.io/gitea/modules/context" |
|
|
"code.gitea.io/gitea/modules/context" |
|
@ -41,7 +43,7 @@ func ListAccessTokens(ctx *context.APIContext) { |
|
|
// "200":
|
|
|
// "200":
|
|
|
// "$ref": "#/responses/AccessTokenList"
|
|
|
// "$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 { |
|
|
if err != nil { |
|
|
ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) |
|
|
ctx.Error(http.StatusInternalServerError, "ListAccessTokens", err) |
|
|
return |
|
|
return |
|
@ -128,15 +130,44 @@ func DeleteAccessToken(ctx *context.APIContext) { |
|
|
// required: true
|
|
|
// required: true
|
|
|
// - name: token
|
|
|
// - name: token
|
|
|
// in: path
|
|
|
// 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
|
|
|
// required: true
|
|
|
// responses:
|
|
|
// responses:
|
|
|
// "204":
|
|
|
// "204":
|
|
|
// "$ref": "#/responses/empty"
|
|
|
// "$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 err := models.DeleteAccessTokenByID(tokenID, ctx.User.ID); err != nil { |
|
|
if models.IsErrAccessTokenNotExist(err) { |
|
|
if models.IsErrAccessTokenNotExist(err) { |
|
|
ctx.NotFound() |
|
|
ctx.NotFound() |
|
|