Skip to content

Commit

Permalink
Merge pull request #53 from shogo82148/migrate-jwt-go
Browse files Browse the repository at this point in the history
migrate dgrijalva/jwt-go to golang-jwt/jwt-go
  • Loading branch information
bradleyfalzon authored Sep 18, 2021
2 parents 68db4d7 + 3a063a5 commit 6aab955
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
- name: Set up Go 1.17
uses: actions/setup-go@v2
with:
go-version: 1.13
go-version: 1.17

- name: Check out code
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ WebHook request

# Dependencies

- [github.com/dgrijalva/jwt-go](https://github.com/dgrijalva/jwt-go)
- [github.com/golang-jwt/jwt-go](https://github.com/golang-jwt/jwt-go)
6 changes: 3 additions & 3 deletions appsTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

jwt "github.com/dgrijalva/jwt-go/v4"
jwt "github.com/golang-jwt/jwt/v4"
)

// AppsTransport provides a http.RoundTripper by wrapping an existing
Expand Down Expand Up @@ -70,8 +70,8 @@ func (t *AppsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
iss := time.Now().Add(-30 * time.Second).Truncate(time.Second)
exp := iss.Add(2 * time.Minute)
claims := &jwt.StandardClaims{
IssuedAt: jwt.At(iss),
ExpiresAt: jwt.At(exp),
IssuedAt: iss.Unix(),
ExpiresAt: exp.Unix(),
Issuer: strconv.FormatInt(t.appID, 10),
}
bearer := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
Expand Down
15 changes: 9 additions & 6 deletions appsTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package ghinstallation

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

jwt "github.com/dgrijalva/jwt-go/v4"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -80,16 +80,19 @@ func TestJWTExpiry(t *testing.T) {
check := RoundTrip{
rt: func(req *http.Request) (*http.Response, error) {
token := strings.Fields(req.Header.Get("Authorization"))[1]
tok, err := jwt.ParseWithClaims(token, &jwt.StandardClaims{}, jwt.KnownKeyfunc(jwt.SigningMethodRS256, key))
tok, err := jwt.ParseWithClaims(token, &jwt.StandardClaims{}, func(t *jwt.Token) (interface{}, error) {
if t.Header["alg"] != "RS256" {
return nil, fmt.Errorf("unexpected signing method: %v, expected: %v", t.Header["alg"], "RS256")
}
return &key.PublicKey, nil
})
if err != nil {
t.Fatalf("jwt parse: %v", err)
}

c := tok.Claims.(*jwt.StandardClaims)
if c.ExpiresAt == nil {
if c.ExpiresAt == 0 {
t.Fatalf("missing exp claim")
} else if c.ExpiresAt.Time != c.ExpiresAt.Truncate(time.Second) {
t.Fatalf("bad exp %v: not truncated to whole seconds", c.ExpiresAt.Time)
}
return nil, nil
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bradleyfalzon/ghinstallation/v2
go 1.13

require (
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
github.com/golang-jwt/jwt/v4 v4.0.0
github.com/google/go-cmp v0.5.6
github.com/google/go-github/v39 v39.0.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
3 changes: 3 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func TestRefreshTokenWithParameters(t *testing.T) {
tr.InstallationTokenOptions = installationTokenOptions

req, err := http.NewRequest("POST", fmt.Sprintf("%s/app/installations/%v/access_tokens", tr.BaseURL, tr.installationID), body)
if err != nil {
t.Fatal("unexpected error:", err)
}
if _, err := tr.RoundTrip(req); err != nil {
t.Fatalf("error calling RoundTrip: %v", err)
}
Expand Down

0 comments on commit 6aab955

Please sign in to comment.