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

Ability to set Transport token #41

Open
wants to merge 3 commits into
base: master
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
4 changes: 2 additions & 2 deletions appsTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func NewAppsTransportFromPrivateKey(tr http.RoundTripper, appID int64, key *rsa.
// RoundTrip implements http.RoundTripper interface.
func (t *AppsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
claims := &jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(time.Minute).Unix(),
IssuedAt: jwt.Now(),
ExpiresAt: jwt.At(time.Now().Add(time.Minute)),
Issuer: strconv.FormatInt(t.appID, 10),
}
bearer := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
Expand Down
13 changes: 10 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type Transport struct {
appsTransport *AppsTransport

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

// accessToken is an installation access token response from GitHub
type accessToken struct {
// AccessToken is an installation access token response from GitHub
type AccessToken struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Permissions github.InstallationPermissions `json:"permissions,omitempty"`
Expand Down Expand Up @@ -189,3 +189,10 @@ func GetReadWriter(i interface{}) (io.ReadWriter, error) {
}
return buf, nil
}

// SetToken sets the underlying Transport AccessToken.
func (t *Transport) SetToken(tok AccessToken) {
t.mu.Lock()
defer t.mu.Unlock()
t.token = &tok
}
4 changes: 2 additions & 2 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestNew(t *testing.T) {
switch r.RequestURI {
case fmt.Sprintf("/app/installations/%d/access_tokens", installationID):
// respond with any token to installation transport
js, _ := json.Marshal(accessToken{
js, _ := json.Marshal(AccessToken{
Token: token,
ExpiresAt: time.Now().Add(5 * time.Minute),
})
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestRefreshTokenWithParameters(t *testing.T) {
}

// Return acceptable access token.
accessToken := accessToken{
accessToken := AccessToken{
Token: "token_string",
ExpiresAt: time.Now(),
Repositories: []github.Repository{{
Expand Down