Skip to content

Commit

Permalink
BREAKING CHANGE: Change IDs from int -> int64.
Browse files Browse the repository at this point in the history
This change makes ghinstallation consistent with
github.com/google/go-github for App and Installation identifiers. See
google/go-github#597 for related discussion.

Fixes bradleyfalzon#27
  • Loading branch information
wlynch committed Oct 9, 2019
1 parent 7bdf9f8 commit 01abf23
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions appsTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type AppsTransport struct {
Client Client // Client to use to refresh tokens, defaults to http.Client with provided transport
tr http.RoundTripper // tr is the underlying roundtripper being wrapped
key *rsa.PrivateKey // key is the GitHub App's private key
appID int // appID is the GitHub App's ID
appID int64 // appID is the GitHub App's ID
}

// NewAppsTransportKeyFromFile returns a AppsTransport using a private key from file.
func NewAppsTransportKeyFromFile(tr http.RoundTripper, appID int, privateKeyFile string) (*AppsTransport, error) {
func NewAppsTransportKeyFromFile(tr http.RoundTripper, appID int64, privateKeyFile string) (*AppsTransport, error) {
privateKey, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read private key: %s", err)
Expand All @@ -43,7 +43,7 @@ func NewAppsTransportKeyFromFile(tr http.RoundTripper, appID int, privateKeyFile
// installations to ensure reuse of underlying TCP connections.
//
// The returned Transport's RoundTrip method is safe to be used concurrently.
func NewAppsTransport(tr http.RoundTripper, appID int, privateKey []byte) (*AppsTransport, error) {
func NewAppsTransport(tr http.RoundTripper, appID int64, privateKey []byte) (*AppsTransport, error) {
t := &AppsTransport{
tr: tr,
appID: appID,
Expand All @@ -63,7 +63,7 @@ func (t *AppsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
claims := &jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(time.Minute).Unix(),
Issuer: strconv.Itoa(t.appID),
Issuer: strconv.FormatInt(t.appID, 10),
}
bearer := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)

Expand Down
26 changes: 13 additions & 13 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/http"
"sync"
"time"

"github.com/google/go-github/github"
)

Expand All @@ -29,30 +29,30 @@ const (
//
// See https://developer.github.com/apps/building-integrations/setting-up-and-registering-github-apps/about-authentication-options-for-github-apps/
type Transport struct {
BaseURL string // BaseURL is the scheme and host for GitHub API, defaults to https://api.github.com
Client Client // Client to use to refresh tokens, defaults to http.Client with provided transport
tr http.RoundTripper // tr is the underlying roundtripper being wrapped
appID int // appID is the GitHub App's ID
installationID int // installationID is the GitHub App Installation ID
BaseURL string // BaseURL is the scheme and host for GitHub API, defaults to https://api.github.com
Client Client // Client to use to refresh tokens, defaults to http.Client with provided transport
tr http.RoundTripper // tr is the underlying roundtripper being wrapped
appID int64 // appID is the GitHub App's ID
installationID int64 // installationID is the GitHub App Installation ID
InstallationTokenOptions *github.InstallationTokenOptions // parameters restrict a token's access
appsTransport *AppsTransport
appsTransport *AppsTransport

mu *sync.Mutex // mu protects token
token *accessToken // token is the installation's access token
}

// accessToken is an installation access token response from GitHub
type accessToken struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Permissions github.InstallationPermissions `json:"permissions,omitempty"`
Repositories []github.Repository `json:"repositories,omitempty"`
}

var _ http.RoundTripper = &Transport{}

// NewKeyFromFile returns a Transport using a private key from file.
func NewKeyFromFile(tr http.RoundTripper, appID, installationID int, privateKeyFile string) (*Transport, error) {
func NewKeyFromFile(tr http.RoundTripper, appID, installationID int64, privateKeyFile string) (*Transport, error) {
privateKey, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read private key: %s", err)
Expand All @@ -73,7 +73,7 @@ type Client interface {
// installations to ensure reuse of underlying TCP connections.
//
// The returned Transport's RoundTrip method is safe to be used concurrently.
func New(tr http.RoundTripper, appID, installationID int, privateKey []byte) (*Transport, error) {
func New(tr http.RoundTripper, appID, installationID int64, privateKey []byte) (*Transport, error) {
t := &Transport{
tr: tr,
appID: appID,
Expand Down Expand Up @@ -140,12 +140,12 @@ func (t *Transport) refreshToken(ctx context.Context) error {
if err != nil {
return fmt.Errorf("could not convert installation token parameters into json: %s", err)
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/app/installations/%v/access_tokens", t.BaseURL, t.installationID), body)
if err != nil {
return fmt.Errorf("could not create request: %s", err)
}

// Set Content and Accept headers.
if body != nil {
req.Header.Set("Content-Type", "application/json")
Expand Down

0 comments on commit 01abf23

Please sign in to comment.