Skip to content

Commit

Permalink
also check that the AccessTokenExpiry has not passed (#181)
Browse files Browse the repository at this point in the history
* also check that the AccessTokenExpiry has not passed

* added changelog

* fix existing test case with new check

---------

Co-authored-by: lursu <leland.ursu@hashicorp.com>
  • Loading branch information
lursu and lursu committed Apr 13, 2023
1 parent abc76c9 commit 4df81c8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/181.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Fixed issue of not regenerating auth token when AccessToken expires
```
6 changes: 3 additions & 3 deletions auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (s *UserSession) GetToken(ctx context.Context, conf *oauth2.Config) (*oauth
var tok *oauth2.Token
var err error

// Check the session expiry of the retrieved token.
// If session expiry has passed, then reauthenticate with browser login and reassign token.
if readErr != nil || cache.SessionExpiry.Before(time.Now()) {
// Check the expiry of the retrieved token.
// If session expiry or the AccessTokenExpiry has passed, then reauthenticate with browser login and reassign token.
if readErr != nil || cache.SessionExpiry.Before(time.Now()) || cache.AccessTokenExpiry.Before(time.Now()) {

// Login with browser.
log.Print("No credentials found, proceeding with browser login.")
Expand Down
2 changes: 1 addition & 1 deletion auth/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestGetToken_ExistingToken(t *testing.T) {
cache := Cache{
AccessToken: "Test.Access.Token",
RefreshToken: "TestRefreshToken",
AccessTokenExpiry: now,
AccessTokenExpiry: now.Add(time.Hour * 2),
SessionExpiry: time.Now().Add(time.Hour * 24),
}

Expand Down

0 comments on commit 4df81c8

Please sign in to comment.