summaryrefslogtreecommitdiff
path: root/src/oauth.go
diff options
context:
space:
mode:
authorJeroen Wijenbergh <jeroenwijenbergh@protonmail.com>2022-04-19 12:28:54 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-19 12:28:54 +0200
commitfb2f57cfcbb6408130e1cc75bd36c896502b78e0 (patch)
treea8ce43925443d9152e7408edce7adb3307204361 /src/oauth.go
parent1b798f8da29ad90506c6d716858ecb2dd782507f (diff)
OAuth improvements: Also ensure tokens based on config state
Diffstat (limited to 'src/oauth.go')
-rw-r--r--src/oauth.go30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/oauth.go b/src/oauth.go
index 836165a..eb4e13f 100644
--- a/src/oauth.go
+++ b/src/oauth.go
@@ -303,15 +303,29 @@ func (oauth *OAuth) Login() error {
return state.LoginOAuth()
}
+func (oauth *OAuth) NeedsRelogin() bool {
+ // The tokens are not expired yet
+ // No relogin is needed
+ if !oauth.isTokensExpired() {
+ GetVPNState().Log(LOG_INFO, "OAuth: Tokens are not expired, re-login not needed")
+ return false
+ }
+
+ refreshErr := oauth.getTokensWithRefresh()
+ // We have obtained new tokens with refresh
+ if refreshErr == nil {
+ GetVPNState().Log(LOG_INFO, "OAuth: Tokens could be re-acquired using the refresh token, re-login not needed")
+ return false
+ }
+
+ // Otherwise relogin is really needed
+ return true
+}
+
func (oauth *OAuth) EnsureTokens() error {
- if oauth.isTokensExpired() {
- GetVPNState().Log(LOG_INFO, "OAuth: Tokens are expired, retrying with refresh tokens")
- err := oauth.getTokensWithRefresh()
- if err != nil {
- GetVPNState().Log(LOG_INFO, fmt.Sprintf("OAuth: Refresh tokens with error %v, retrying with a new login phase", err))
- // log that we're getting tokens using login
- return oauth.Login()
- }
+ if oauth.NeedsRelogin() {
+ GetVPNState().Log(LOG_INFO, "OAuth: Tokens are invalid, relogging in")
+ return oauth.Login()
}
return nil
}