diff options
author | Yarden Shoham <git@yardenshoham.com> | 2023-07-26 02:06:11 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-25 23:06:11 +0000 |
commit | d36ddfe26c940b7820e866b87d18b425d206389f (patch) | |
tree | 83c814b23ef978fe4dc867edbd7acf6ca9896c28 /cmd/admin_user_generate_access_token.go | |
parent | 3e4a4f9c67ebc08fed0b4dff90c707ddbc608832 (diff) | |
download | gitea-d36ddfe26c940b7820e866b87d18b425d206389f.tar.gz gitea-d36ddfe26c940b7820e866b87d18b425d206389f.zip |
Fix CLI allowing creation of access tokens with existing name (#26071)
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>
Diffstat (limited to 'cmd/admin_user_generate_access_token.go')
-rw-r--r-- | cmd/admin_user_generate_access_token.go | 21 |
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 9971c2ec91..0febb91661 100644 --- a/cmd/admin_user_generate_access_token.go +++ b/cmd/admin_user_generate_access_token.go @@ -57,17 +57,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 } |