Home Assistant Unofficial Reference 2024.12.1
application_credentials.py
Go to the documentation of this file.
1 """application_credentials platform for Withings."""
2 
3 from typing import Any
4 
5 from aiowithings import AUTHORIZATION_URL, TOKEN_URL
6 
8  AuthImplementation,
9  AuthorizationServer,
10  ClientCredential,
11 )
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import config_entry_oauth2_flow
14 
15 from .const import DOMAIN
16 
17 
19  hass: HomeAssistant, auth_domain: str, credential: ClientCredential
20 ) -> config_entry_oauth2_flow.AbstractOAuth2Implementation:
21  """Return auth implementation."""
23  hass,
24  DOMAIN,
25  credential,
26  authorization_server=AuthorizationServer(
27  authorize_url=AUTHORIZATION_URL,
28  token_url=TOKEN_URL,
29  ),
30  )
31 
32 
34  """Oauth2 implementation that only uses the external url."""
35 
36  async def _token_request(self, data: dict) -> dict:
37  """Make a token request and adapt Withings API reply."""
38  new_token = await super()._token_request(data)
39  # Withings API returns habitual token data under json key "body":
40  # {
41  # "status": [{integer} Withings API response status],
42  # "body": {
43  # "access_token": [{string} Your new access_token],
44  # "expires_in": [{integer} Access token expiry delay in seconds],
45  # "token_type": [{string] HTTP Authorization Header format: Bearer],
46  # "scope": [{string} Scopes the user accepted],
47  # "refresh_token": [{string} Your new refresh_token],
48  # "userid": [{string} The Withings ID of the user]
49  # }
50  # }
51  # so we copy that to token root.
52  if body := new_token.pop("body", None):
53  new_token.update(body)
54  return new_token
55 
56  async def async_resolve_external_data(self, external_data: Any) -> dict:
57  """Resolve the authorization code to tokens."""
58  return await self._token_request_token_request(
59  {
60  "action": "requesttoken",
61  "grant_type": "authorization_code",
62  "code": external_data["code"],
63  "redirect_uri": external_data["state"]["redirect_uri"],
64  }
65  )
66 
67  async def _async_refresh_token(self, token: dict) -> dict:
68  """Refresh tokens."""
69  new_token = await self._token_request_token_request(
70  {
71  "action": "requesttoken",
72  "grant_type": "refresh_token",
73  "client_id": self.client_id,
74  "refresh_token": token["refresh_token"],
75  }
76  )
77  return {**token, **new_token}
config_entry_oauth2_flow.AbstractOAuth2Implementation async_get_auth_implementation(HomeAssistant hass, str auth_domain, ClientCredential credential)