summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/oauth/oauth_test.go33
-rw-r--r--internal/oauth/token.go9
2 files changed, 41 insertions, 1 deletions
diff --git a/internal/oauth/oauth_test.go b/internal/oauth/oauth_test.go
index bafb7e5..8682e24 100644
--- a/internal/oauth/oauth_test.go
+++ b/internal/oauth/oauth_test.go
@@ -80,7 +80,7 @@ func Test_accessToken(t *testing.T) {
// Set the tokens as expired
o.SetTokenExpired()
- // We should not get an error because expired and no refresh token
+ // We should get an error because expired and no refresh token
_, err = o.AccessToken()
if err == nil {
t.Fatal("Got no error when getting access token on non-empty structure and expired")
@@ -106,6 +106,37 @@ func Test_accessToken(t *testing.T) {
if got != want {
t.Fatalf("Access token not equal, Got: %v, Want: %v", got, want)
}
+
+
+ // Set the tokens as expired
+ o.SetTokenExpired()
+ want = "test3"
+
+ // Now let's act like a 2.x server, we give no refresh token back. When we refresh the previous refresh token should be gotten
+ o.token.t.Refresh = refresh
+ prevRefresh := refresh
+ o.token.t.Refresher = func(refreshToken string) (*TokenResponse, time.Time, error) {
+ if refreshToken != refresh {
+ t.Fatalf("Passed refresh token to refresher not equal to updated refresh token, got: %v, want: %v", refreshToken, refresh)
+ }
+ // Only the access token is returned now
+ r := &TokenResponse{Access: want}
+ return r, expired, nil
+ }
+
+ got, err = o.AccessToken()
+ if err != nil {
+ t.Fatalf("Got error when getting access token on non-empty expired structure and with an empty refresh response: %v", err)
+ }
+ if got != want {
+ t.Fatalf("Access token not equal, Got: %v, Want: %v", got, want)
+ }
+ if o.token.t.Refresh == "" {
+ t.Fatalf("Refresh token is empty after refreshing and getting back an empty refresh")
+ }
+ if o.token.t.Refresh != prevRefresh {
+ t.Fatalf("Refresh token is not equal to previous refresh token after refreshing and getting back an empty refresh token, got: %v, want: %v", o.token.t.Refresh, prevRefresh)
+ }
}
func Test_secretJSON(t *testing.T) {
diff --git a/internal/oauth/token.go b/internal/oauth/token.go
index a9d2c82..58d6136 100644
--- a/internal/oauth/token.go
+++ b/internal/oauth/token.go
@@ -89,10 +89,19 @@ func (l *tokenLock) Access() (string, error) {
log.Logger.Debugf("No token response after refreshing")
return "", errors.New("No token response after refreshing")
}
+ // store the previous refresh token
+ pr := l.t.Refresh
+ // get the response as a non-pointer
r := *tr
e := s.Add(time.Second * time.Duration(r.Expires))
t := Token{Access: r.Access, Refresh: r.Refresh, ExpiredTimestamp: e}
l.updateInternal(t)
+ // set the previous refresh token if the new one is empty
+ // This is for 2.x servers
+ if l.t.Refresh == "" {
+ log.Logger.Debugf("The previous refresh token is set as the response had no refresh token")
+ l.t.Refresh = pr
+ }
return l.t.Access, nil
}