From ffbb0f6a60a000e42f26fd1e08925de1f2ff90be Mon Sep 17 00:00:00 2001 From: Frode Aannevik Date: Tue, 15 Oct 2019 23:09:47 +0200 Subject: token: disallow multiple tokens with same name (#5820) * api/v1: don't allow multiple tokens with same name Fail with 422 Unprocessable Entity if the token name already exist ref: https://github.com/gogs/gogs/issues/5587 * Move new token error type to models/errors/token * Remove "useless" ListAccessTokensByName function * Add an i18n entry for token_name_exists --- routes/api/v1/user/app.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'routes/api/v1') diff --git a/routes/api/v1/user/app.go b/routes/api/v1/user/app.go index 864ede54..dcc3fca5 100644 --- a/routes/api/v1/user/app.go +++ b/routes/api/v1/user/app.go @@ -10,6 +10,7 @@ import ( api "github.com/gogs/go-gogs-client" "github.com/gogs/gogs/models" + "github.com/gogs/gogs/models/errors" "github.com/gogs/gogs/pkg/context" ) @@ -33,7 +34,11 @@ func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) Name: form.Name, } if err := models.NewAccessToken(t); err != nil { - c.ServerError("NewAccessToken", err) + if errors.IsAccessTokenNameAlreadyExist(err) { + c.Error(http.StatusUnprocessableEntity, "", err) + } else { + c.ServerError("NewAccessToken", err) + } return } c.JSON(http.StatusCreated, &api.AccessToken{t.Name, t.Sha1}) -- cgit v1.2.3