Skip to content

Commit 0387ff2

Browse files
committed
handle empty scope
1 parent 8d893a6 commit 0387ff2

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

cmd/admin_user_create.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ func runCreateUser(c *cli.Context) error {
212212
if err != nil {
213213
return fmt.Errorf("invalid access token scope provided: %w", err)
214214
}
215+
if !accessTokenScope.HasPermissionScope() {
216+
return errors.New("access token does not have any permission")
217+
}
215218
t.Scope = accessTokenScope
216219

217220
if err := auth_model.NewAccessToken(ctx, t); err != nil {

cmd/admin_user_create_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,17 @@ func TestAdminUserCreate(t *testing.T) {
9595

9696
// using "--access-token-name" without "--access-token"
9797
reset()
98-
assert.ErrorContains(t, createUser("u", "--random-password --access-token-name new-token-name"), "access-token-name and access-token-scopes flags are only valid when access-token flag is set")
98+
err = createUser("u", "--random-password --access-token-name new-token-name")
99+
assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")
99100

100101
// using "--access-token-scopes" without "--access-token"
101102
reset()
102-
assert.ErrorContains(t, createUser("u", "--random-password --access-token-scopes read:issue"), "access-token-name and access-token-scopes flags are only valid when access-token flag is set")
103+
err = createUser("u", "--random-password --access-token-scopes read:issue")
104+
assert.ErrorContains(t, err, "access-token-name and access-token-scopes flags are only valid when access-token flag is set")
105+
106+
// empty permission
107+
reset()
108+
err = createUser("u", "--random-password --access-token --access-token-scopes public-only")
109+
assert.ErrorContains(t, err, "access token does not have any permission")
103110
})
104111
}

cmd/admin_user_generate_access_token.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ var microcmdUserGenerateAccessToken = &cli.Command{
3434
},
3535
&cli.StringFlag{
3636
Name: "scopes",
37-
Value: "",
38-
Usage: "Comma separated list of scopes to apply to access token",
37+
Value: "all",
38+
Usage: `Comma separated list of scopes to apply to access token, examples: "all", "public-only,read:issue", "write:repository,write:user"`,
3939
},
4040
},
4141
Action: runGenerateAccessToken,
4242
}
4343

4444
func runGenerateAccessToken(c *cli.Context) error {
4545
if !c.IsSet("username") {
46-
return errors.New("You must provide a username to generate a token for")
46+
return errors.New("you must provide a username to generate a token for")
4747
}
4848

4949
ctx, cancel := installSignals()
@@ -77,6 +77,9 @@ func runGenerateAccessToken(c *cli.Context) error {
7777
if err != nil {
7878
return fmt.Errorf("invalid access token scope provided: %w", err)
7979
}
80+
if !accessTokenScope.HasPermissionScope() {
81+
return errors.New("access token does not have any permission")
82+
}
8083
t.Scope = accessTokenScope
8184

8285
// create the token

models/auth/access_token_scope.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ func (s AccessTokenScope) Normalize() (AccessTokenScope, error) {
295295
return bitmap.toScope(), nil
296296
}
297297

298+
func (s AccessTokenScope) HasPermissionScope() bool {
299+
return s != "" && s != AccessTokenScopePublicOnly
300+
}
301+
298302
// PublicOnly checks if this token scope is limited to public resources
299303
func (s AccessTokenScope) PublicOnly() (bool, error) {
300304
bitmap, err := s.parse()

routers/web/user/setting/applications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ApplicationsPost(ctx *context.Context) {
5454
ctx.ServerError("GetScope", err)
5555
return
5656
}
57-
if scope == "" || scope == auth_model.AccessTokenScopePublicOnly {
57+
if !scope.HasPermissionScope() {
5858
ctx.Flash.Error(ctx.Tr("settings.at_least_one_permission"), true)
5959
}
6060

0 commit comments

Comments
 (0)