summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2023-07-25 21:30:50 -0400
committerGitHub <noreply@github.com>2023-07-26 01:30:50 +0000
commit43213b816d4cc4de9dd46a7b667925516e305443 (patch)
treec62f49a2c17b62ac4cebdbda734852dd58420337
parenta55924aaf48de3ae9c32e1910c62c2b7f6e4f0ad (diff)
downloadgitea-43213b816d4cc4de9dd46a7b667925516e305443.tar.gz
gitea-43213b816d4cc4de9dd46a7b667925516e305443.zip
Fix CLI allowing creation of access tokens with existing name (#26071) (#26144)
Backport #26071 by @yardenshoham We are now: - Making sure there is no existing access token with the same name - Making sure the given scopes are valid (we already did this before but now we have a message) The logic is mostly taken from https://github.com/go-gitea/gitea/blob/a12a5f3652c339b17b187ff424a480631a3c1e1e/routers/api/v1/user/app.go#L101-L123 Closes #26044 Signed-off-by: Yarden Shoham <git@yardenshoham.com>
-rw-r--r--cmd/admin_user_generate_access_token.go21
1 files changed, 16 insertions, 5 deletions
diff --git a/cmd/admin_user_generate_access_token.go b/cmd/admin_user_generate_access_token.go
index 822bc5c2bc..c3b2f7d9a4 100644
--- a/cmd/admin_user_generate_access_token.go
+++ b/cmd/admin_user_generate_access_token.go
@@ -55,17 +55,28 @@ func runGenerateAccessToken(c *cli.Context) error {
return err
}
- accessTokenScope, err := auth_model.AccessTokenScope(c.String("scopes")).Normalize()
+ // construct token with name and user so we can make sure it is unique
+ t := &auth_model.AccessToken{
+ Name: c.String("token-name"),
+ UID: user.ID,
+ }
+
+ exist, err := auth_model.AccessTokenByNameExists(t)
if err != nil {
return err
}
+ if exist {
+ return fmt.Errorf("access token name has been used already")
+ }
- t := &auth_model.AccessToken{
- Name: c.String("token-name"),
- UID: user.ID,
- Scope: accessTokenScope,
+ // make sure the scopes are valid
+ accessTokenScope, err := auth_model.AccessTokenScope(c.String("scopes")).Normalize()
+ if err != nil {
+ return fmt.Errorf("invalid access token scope provided: %w", err)
}
+ t.Scope = accessTokenScope
+ // create the token
if err := auth_model.NewAccessToken(t); err != nil {
return err
}