Skip to content

Commit

Permalink
feat: add id_token to connect() response and return correct refresh_t…
Browse files Browse the repository at this point in the history
…oken (#217)

* Add id_token to connect() response

* Set refresh token to the one returned from auth-api and not owner api
  • Loading branch information
omelhus authored Sep 14, 2021
1 parent e675285 commit 6d837ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions teslajsonpy/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
self.api: Text = "/api/1/"
self.expiration: int = expiration
self.access_token = access_token
self.id_token = None
self.head = None
self.refresh_token = refresh_token
self.websession = websession
Expand Down Expand Up @@ -123,9 +124,7 @@ async def post(self, command, method="post", data=None):
refresh_token=self.sso_oauth.get("refresh_token")
)
elif self.refresh_token:
auth = await self.refresh_access_token(
refresh_token=self.refresh_token
)
auth = await self.refresh_access_token(refresh_token=self.refresh_token)
if auth and all(
(
auth.get(item)
Expand All @@ -137,6 +136,8 @@ async def post(self, command, method="post", data=None):
"refresh_token": auth["refresh_token"],
"expires_in": auth["expires_in"] + now,
}
self.id_token = auth["id_token"]
self.refresh_token = auth["refresh_token"]
_LOGGER.debug("Saved new auth info %s", self.sso_oauth)
else:
_LOGGER.debug("Unable to refresh sso oauth token")
Expand All @@ -159,7 +160,6 @@ async def post(self, command, method="post", data=None):
self.__sethead(
access_token=auth["access_token"], expires_in=auth["expires_in"]
)
self.refresh_token = auth["refresh_token"]
self.token_refreshed = True
_LOGGER.debug("Successfully refreshed oauth")
return await self.__open(
Expand Down
6 changes: 4 additions & 2 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async def connect(
mfa_code (Text, optional): MFA code to use for connection
Returns
Dict[Text, Text]: Returns the refresh_token, access_token, and expires_in time
Dict[Text, Text]: Returns the refresh_token, access_token, id_token and expires_in time
"""

Expand Down Expand Up @@ -336,6 +336,7 @@ async def connect(
"refresh_token": self.__connection.refresh_token,
"access_token": self.__connection.access_token,
"expiration": self.__connection.expiration,
"id_token": self.__connection.id_token,
}

async def disconnect(self) -> None:
Expand All @@ -358,14 +359,15 @@ def get_tokens(self) -> Dict[Text, Text]:
This will set the the self.__connection token_refreshed to False.
Returns
Dict[Text, Text]: Returns the refresh_token, access_token, and expires time
Dict[Text, Text]: Returns the refresh_token, access_token, id_token and expires time
"""
self.__connection.token_refreshed = False
return {
"refresh_token": self.__connection.refresh_token,
"access_token": self.__connection.access_token,
"expiration": self.__connection.expiration,
"id_token": self.__connection.id_token,
}

def get_expiration(self) -> int:
Expand Down

0 comments on commit 6d837ed

Please sign in to comment.