Skip to content

Commit

Permalink
Merge pull request #880 from flobz/851-cannot-get-remote-refresh-toke…
Browse files Browse the repository at this point in the history
…n-error

fix: Cannot get remote refresh token error #851
  • Loading branch information
flobz authored May 16, 2024
2 parents 6d80315 + 7877de9 commit 1179ec3
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions psa_car_controller/psa/RemoteClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, account_info: AccountInformation, vehicles_list: Cars, manage
self.last_request = None
self.mqtt_client = None
self.otp = None
self._lock = threading.Lock()

def __on_mqtt_connect(self, client, userdata, result_code, _): # pylint: disable=unused-argument
logger.info("Connected with result code %s", result_code)
Expand Down Expand Up @@ -146,39 +147,40 @@ def mqtt_request(self, vin, req_parameters, topic):
return MQTTRequest(topic, vin, req_parameters, self.account_info.get_mqtt_customer_id())

def _refresh_remote_token(self, force=False):
bad_remote_token = self.remoteCredentials.refresh_token is None
if not force and not bad_remote_token and self.remoteCredentials.last_update:
last_update: datetime = self.remoteCredentials.last_update
if (datetime.now() - last_update).total_seconds() < MQTT_TOKEN_TTL:
return True
try:
self.manager.refresh_token_now()
if bad_remote_token:
logger.error("remote_refresh_token isn't defined")
else:
res = self.manager.post(REMOTE_URL + self.account_info.client_id,
json={"grant_type": "refresh_token",
"refresh_token": self.remoteCredentials.refresh_token},
headers=self.headers)
data = res.json()
logger.debug("refresh_remote_token: %s", data)
if "access_token" in data:
self.remoteCredentials.access_token = data["access_token"]
self.remoteCredentials.refresh_token = data["refresh_token"]
bad_remote_token = False
with self._lock:
bad_remote_token = self.remoteCredentials.refresh_token is None
if not force and not bad_remote_token and self.remoteCredentials.last_update:
last_update: datetime = self.remoteCredentials.last_update
if (datetime.now() - last_update).total_seconds() < MQTT_TOKEN_TTL:
return True
try:
self.manager.refresh_token_now()
if bad_remote_token:
logger.warning("remote_refresh_token isn't defined")
else:
logger.error("can't refresh_remote_token: %s\n Create a new one", data)
bad_remote_token = True
if bad_remote_token:
otp_code = self.get_otp_code()
self._get_remote_access_token(otp_code)
self.remote_token_last_update = datetime.now()
self.mqtt_client.username_pw_set("IMA_OAUTH_ACCESS_TOKEN", self.remoteCredentials.access_token)
return True
except (RequestException, RateLimitException, KeyError) as e:
logger.exception("Can't refresh remote token %s", e)
time.sleep(60)
return False
res = self.manager.post(REMOTE_URL + self.account_info.client_id,
json={"grant_type": "refresh_token",
"refresh_token": self.remoteCredentials.refresh_token},
headers=self.headers)
data = res.json()
logger.debug("refresh_remote_token: %s", data)
if "access_token" in data:
self.remoteCredentials.access_token = data["access_token"]
bad_remote_token = False
if "refresh_token" in data:
self.remoteCredentials.refresh_token = data["refresh_token"]
else:
logger.error("can't refresh_remote_token: %s\n Create a new one", data)
bad_remote_token = True
if bad_remote_token:
otp_code = self.get_otp_code()
self._get_remote_access_token(otp_code)
self.remote_token_last_update = datetime.now()
self.mqtt_client.username_pw_set("IMA_OAUTH_ACCESS_TOKEN", self.remoteCredentials.access_token)
return True
except (RequestException, RateLimitException, KeyError):
logger.exception("Can't refresh remote token, please redo otp procedure")
return False

def get_sms_otp_code(self):
res = self.manager.post(
Expand Down

0 comments on commit 1179ec3

Please sign in to comment.