Skip to content

Commit

Permalink
Merge pull request #45 from docker/revert-scope-support
Browse files Browse the repository at this point in the history
Revert PAT scope support
  • Loading branch information
silvin-lubecki authored Oct 16, 2020
2 parents 188ffd6 + a4ded83 commit 5375be7
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 42 deletions.
4 changes: 1 addition & 3 deletions internal/commands/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type createOptions struct {
format.Option
description string
quiet bool
scopes []string
}

func newCreateCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
Expand All @@ -57,13 +56,12 @@ func newCreateCmd(streams command.Streams, hubClient *hub.Client, parent string)
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().StringArrayVar(&opts.scopes, "scope", nil, "Add a specific token scope (repo:public_read)")
cmd.Flags().SetInterspersed(false)
return cmd
}

func runCreate(streams command.Streams, hubClient *hub.Client, opts createOptions) error {
token, err := hubClient.CreateToken(opts.description, opts.scopes)
token, err := hubClient.CreateToken(opts.description)
if err != nil {
return err
}
Expand Down
3 changes: 0 additions & 3 deletions internal/commands/token/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ func printInspectToken(out io.Writer, value interface{}) error {
fmt.Fprintf(w, color.Key("%sDescription:")+"\t%s\n", prefix, token.Description)
}
fmt.Fprintf(w, color.Key("%sIs Active:")+"\t%v\n", prefix, token.IsActive)
if len(token.Scopes) > 0 {
fmt.Fprintf(w, color.Key("%sScopes:")+"\t%s\n", prefix, strings.Join(token.Scopes, ", "))
}
fmt.Fprintf(w, color.Key("%sCreated:")+"\t%s\n", prefix, fmt.Sprintf("%s ago", units.HumanDuration(time.Since(token.CreatedAt))))
fmt.Fprintf(w, color.Key("%sLast Used:")+"\t%s\n", prefix, getLastUsed(token.LastUsed))
fmt.Fprintf(w, color.Key("%sCreator User Agent:")+"\t%s\n", prefix, token.CreatorUA)
Expand Down
9 changes: 0 additions & 9 deletions internal/commands/token/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ var (
return fmt.Sprintf("%v", t.IsActive)
}},
}
scopeColumn = column{
"SCOPES", func(t hub.Token) string { return strings.Join(t.Scopes, ",") },
}
)

type column struct {
Expand Down Expand Up @@ -104,12 +101,6 @@ func runList(streams command.Streams, hubClient *hub.Client, opts listOptions) e

func printTokens(out io.Writer, values interface{}) error {
h := values.(*helper)
for _, t := range h.tokens {
if len(t.Scopes) > 0 {
defaultColumns = append(defaultColumns, scopeColumn)
break
}
}
w := ansiterm.NewTabWriter(out, 20, 1, 3, ' ', 0)
var headers []string
for _, column := range defaultColumns {
Expand Down
31 changes: 4 additions & 27 deletions internal/hub/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ const (
TokenURL = "/v2/api_tokens/%s"
)

var (
validScopes = map[string]struct{}{
// Allow only public pulls
"repo:public_read": {},
}
)

//Token is a personal access token. The token field will only be filled at creation and can never been accessed again.
type Token struct {
UUID uuid.UUID
Expand All @@ -53,15 +46,11 @@ 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, scopes []string) (*Token, error) {
if err := validateScopes(scopes); err != nil {
return nil, err
}
data, err := json.Marshal(hubTokenRequest{Description: description, Scopes: scopes})
func (c *Client) CreateToken(description string) (*Token, error) {
data, err := json.Marshal(hubTokenRequest{Description: description})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,9 +186,8 @@ func (c *Client) getTokensPage(url string) ([]Token, int, string, error) {
}

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

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

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

func validateScopes(scopes []string) error {
for _, scope := range scopes {
if _, ok := validScopes[scope]; !ok {
return fmt.Errorf("invalid scope %q", scope)
}
}
return nil
}

0 comments on commit 5375be7

Please sign in to comment.