Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from overmindtech/36-extreme-slowness-over-time
Browse files Browse the repository at this point in the history
Fixed token refresh on expiry
  • Loading branch information
dylanratcliffe authored Feb 13, 2023
2 parents 3acd17e + a68fa4b commit 06e29e3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (o *OAuthTokenClient) GetJWT() (string, error) {

claims.Validate(&vr)

if len(vr.Errors()) != 0 {
if vr.IsBlocking(true) {
// Regenerate the token
err := o.generateJWT(ctx)

Expand Down
45 changes: 45 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"testing"
"time"

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/overmindtech/sdp-go"
)

Expand Down Expand Up @@ -312,6 +314,49 @@ func TestNATSConnect(t *testing.T) {
})
}

func TestTokenRefresh(t *testing.T) {
tk := GetTestOAuthTokenClient(t)

// Get a token
token, err := tk.GetJWT()

if err != nil {
t.Fatal(err)
}

// Artificially set the expiry and replace the token
claims, err := jwt.DecodeUserClaims(token)

if err != nil {
t.Fatal(err)
}

pair, err := nkeys.CreateAccount()

if err != nil {
t.Fatal(err)
}

claims.Expires = time.Now().Add(-10 * time.Second).Unix()
tk.jwt, err = claims.Encode(pair)
expiredToken := tk.jwt

if err != nil {
t.Error(err)
}

// Get the token again
newToken, err := tk.GetJWT()

if err != nil {
t.Error(err)
}

if expiredToken == newToken {
t.Error("token is unchanged")
}
}

func ValidateNATSConnection(t *testing.T, ec sdp.EncodedConnection) {
t.Helper()
done := make(chan struct{})
Expand Down

0 comments on commit 06e29e3

Please sign in to comment.