Skip to content

Commit

Permalink
Use Authorization header for Github PAT
Browse files Browse the repository at this point in the history
PATs are oauth tokens so there's no reason to use BasicAuth here.

I read the GH docs as recommending `Authorization: Bearer` if possible.

GH understands what kind of token is being used, e.g. I see:
`Added on Aug 20, 2020 via personal access token owned by
@michaelbeaumont`
when I authenticate this way and add a DeployKey
  • Loading branch information
michaelbeaumont committed Aug 20, 2020
1 parent 92426c1 commit db66a66
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
17 changes: 1 addition & 16 deletions github/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ import (
const (
// DefaultDomain specifies the default domain used as the backend.
DefaultDomain = "github.com"
// patUsername is the "username" for the basic auth authentication flow
// when using a personal access token as the "password". This string could
// be arbitrary, even unset, as it is not respected server-side. For conventions'
// sake, we'll set this to "git".
patUsername = "git"
)

// ClientOption is the interface to implement for passing options to NewClient.
Expand Down Expand Up @@ -200,17 +195,7 @@ func WithPersonalAccessToken(patToken string) ClientOption {
return optionError(fmt.Errorf("patToken cannot be empty: %w", gitprovider.ErrInvalidClientOptions))
}

return &clientOptions{AuthTransport: patTransport(patToken)}
}

func patTransport(patToken string) gitprovider.ChainableRoundTripperFunc {
return func(in http.RoundTripper) http.RoundTripper {
return &github.BasicAuthTransport{
Username: patUsername,
Password: patToken,
Transport: in,
}
}
return &clientOptions{AuthTransport: oauth2Transport(patToken)}
}

// WithConditionalRequests instructs the client to use Conditional Requests to GitHub, asking GitHub
Expand Down
2 changes: 1 addition & 1 deletion github/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func Test_makeOptions(t *testing.T) {
{
name: "WithPersonalAccessToken",
opts: []ClientOption{WithPersonalAccessToken("foo")},
want: &clientOptions{AuthTransport: patTransport("foo")},
want: &clientOptions{AuthTransport: oauth2Transport("foo")},
},
{
name: "WithPersonalAccessToken, empty",
Expand Down

0 comments on commit db66a66

Please sign in to comment.