def test_token_expiery(self): cc1 = {"access_token": expired_token, "refresh_token": expired_token} cc2 = { "access_token": not_expired_token, "refresh_token": not_expired_token } assert is_token_expired(cc1["access_token"]) assert is_token_expired(cc1["refresh_token"]) assert not is_token_expired(cc2["access_token"]) assert not is_token_expired(cc2["refresh_token"])
def refresh_token(self): if not self.token_provider: return None # If expired, relog if is_token_expired(self._refresh_token): self.token_provider.new_token() return self._refresh_token
def access_token(self): if not self.token_provider: return None if not self._access_token: self.login() # If expired, refresh if is_token_expired(self._access_token): self.refresh_access_token() return self._access_token
def refresh_access_token(self): if not self.token_provider: return if is_token_expired(self._refresh_token): tokens = self.token_provider.new_token() else: tokens = self.token_provider.refresh_token(self.refresh_token) self._access_token = tokens["access_token"] self.file_cache.write_credentials(credentials=self)
def login(self, force=False): if not self.token_provider: return cached = self.file_cache.read_credentials() if cached: self._access_token = cached["access_token"] self._refresh_token = cached["refresh_token"] if self._access_token and not is_token_expired(self._access_token): log.info("Token not expired, skipping") return tokens = self.token_provider.new_token() if "access_token" not in tokens: raise ApiAuthenticateError self._access_token = tokens["access_token"] self._refresh_token = tokens["refresh_token"] self.file_cache.write_credentials(credentials=self)