Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope support to access tokens #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion internal/commands/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package token

import (
"errors"
"fmt"
"io"

Expand All @@ -38,6 +39,19 @@ type createOptions struct {
format.Option
description string
quiet bool
scope string
}

func validScope(scope string) bool {
switch scope {
case
"admin",
"write",
"read",
"public_read":
Comment on lines +48 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract these into a []string and use it with strings.Join to build the description for the flag l.78

return true
}
return false
}

func newCreateCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
Expand All @@ -54,17 +68,23 @@ func newCreateCmd(streams command.Streams, hubClient *hub.Client, parent string)
metrics.Send(parent, createName)
},
RunE: func(_ *cobra.Command, args []string) error {

return runCreate(streams, hubClient, opts)
},
}
opts.AddFormatFlag(cmd.Flags())
cmd.Flags().StringVar(&opts.description, "description", "", "Set token's description")
cmd.Flags().BoolVar(&opts.quiet, "quiet", false, "Display only created token")
cmd.Flags().StringVar(&opts.scope, "scope", "", "Set token's repo scope (admin,write,read,public_read)")

return cmd
}

func runCreate(streams command.Streams, hubClient *hub.Client, opts createOptions) error {
token, err := hubClient.CreateToken(opts.description)
if len(opts.scope) > 0 && !validScope(opts.scope) {
return errors.New("scope must be one of admin,write,read,public_read")
}
token, err := hubClient.CreateToken(opts.description, opts.scope)
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/token/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var (
s := fmt.Sprintf("%v", t.IsActive)
return s, len(s)
}},
{"SCOPE", func(t hub.Token) (string, int) {
if len(t.Scopes) == 0 {
return "", 0
}
return t.Scopes[0][5:], len(t.Scopes[0]) - 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess 5 is because it’s the length of "repo:", could we factor out the string and use len()?

}},
}
)

Expand Down
19 changes: 15 additions & 4 deletions pkg/hub/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ type Token struct {
IsActive bool
Token string
Description string
Scopes []string
}

// CreateToken creates a Personal Access Token and returns the token field only once
func (c *Client) CreateToken(description string) (*Token, error) {
data, err := json.Marshal(hubTokenRequest{Description: description})
func (c *Client) CreateToken(description string, scope string) (*Token, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
func (c *Client) CreateToken(description string, scope string) (*Token, error) {
func (c *Client) CreateToken(description, scope string) (*Token, error) {

tokenRequest := hubTokenRequest{Description: description}
if len(scope) > 0 {
scopes := []string{scope}
scopes[0] = "repo:" + scope
tokenRequest.Scopes = scopes
Comment on lines +56 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something? Why can’t it be

Suggested change
scopes := []string{scope}
scopes[0] = "repo:" + scope
tokenRequest.Scopes = scopes
tokenRequest.Scopes = []string{"repo:" + scope}

?

}

data, err := json.Marshal(tokenRequest)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,8 +198,9 @@ func (c *Client) getTokensPage(url string) ([]Token, int, string, error) {
}

type hubTokenRequest struct {
Description string `json:"token_label,omitempty"`
IsActive bool `json:"is_active"`
Description string `json:"token_label,omitempty"`
Scopes []string `json:"scopes,omitempty"`
IsActive bool `json:"is_active"`
}

type hubTokenResponse struct {
Expand All @@ -212,6 +221,7 @@ type hubTokenResult struct {
IsActive bool `json:"is_active"`
Token string `json:"token"`
TokenLabel string `json:"token_label"`
Scopes []string `json:"scopes"`
}

func convertToken(response hubTokenResult) (Token, error) {
Expand All @@ -230,5 +240,6 @@ func convertToken(response hubTokenResult) (Token, error) {
IsActive: response.IsActive,
Token: response.Token,
Description: response.TokenLabel,
Scopes: response.Scopes,
}, nil
}