Skip to content

Commit 43213b8

Browse files
authored
Fix CLI allowing creation of access tokens with existing name (go-gitea#26071) (go-gitea#26144)
Backport go-gitea#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 go-gitea#26044 Signed-off-by: Yarden Shoham <git@yardenshoham.com>
1 parent a55924a commit 43213b8

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cmd/admin_user_generate_access_token.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,28 @@ func runGenerateAccessToken(c *cli.Context) error {
5555
return err
5656
}
5757

58-
accessTokenScope, err := auth_model.AccessTokenScope(c.String("scopes")).Normalize()
58+
// construct token with name and user so we can make sure it is unique
59+
t := &auth_model.AccessToken{
60+
Name: c.String("token-name"),
61+
UID: user.ID,
62+
}
63+
64+
exist, err := auth_model.AccessTokenByNameExists(t)
5965
if err != nil {
6066
return err
6167
}
68+
if exist {
69+
return fmt.Errorf("access token name has been used already")
70+
}
6271

63-
t := &auth_model.AccessToken{
64-
Name: c.String("token-name"),
65-
UID: user.ID,
66-
Scope: accessTokenScope,
72+
// make sure the scopes are valid
73+
accessTokenScope, err := auth_model.AccessTokenScope(c.String("scopes")).Normalize()
74+
if err != nil {
75+
return fmt.Errorf("invalid access token scope provided: %w", err)
6776
}
77+
t.Scope = accessTokenScope
6878

79+
// create the token
6980
if err := auth_model.NewAccessToken(t); err != nil {
7081
return err
7182
}

0 commit comments

Comments
 (0)