Skip to content

Commit d36ddfe

Browse files
authored
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 <[email protected]>
1 parent 3e4a4f9 commit d36ddfe

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
@@ -57,17 +57,28 @@ func runGenerateAccessToken(c *cli.Context) error {
5757
return err
5858
}
5959

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

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

81+
// create the token
7182
if err := auth_model.NewAccessToken(t); err != nil {
7283
return err
7384
}

0 commit comments

Comments
 (0)