Skip to content

Commit 09d0754

Browse files
committed
Update the implementation details of the external bus for the apps
1 parent a07ecbc commit 09d0754

2 files changed

Lines changed: 466 additions & 55 deletions

File tree

docs/frontend/external-authentication.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,36 @@ By default, the frontend will take care of its own authentication tokens. If non
66

77
If you want to embed the Home Assistant frontend in an external app, you will want to store the authentication inside the app but make it available to the frontend. To support this, Home Assistant exposes an external authentication API.
88

9-
To activate this API, load the frontend with `?external_auth=1` appended to the URL. If this is passed in, Home Assistant will expect either `window.externalApp` (for Android) or `window.webkit.messageHandlers` (for iOS) to be defined containing the methods described below.
9+
To activate this API, load the frontend with `?external_auth=1` appended to the URL. If this is passed in, Home Assistant will expect either `window.externalAppV2` (Android V2, recommended), `window.externalApp` (Android V1, fallback) or `window.webkit.messageHandlers` (iOS) to be defined containing the methods described below.
1010

11-
## Get access token
11+
:::note
12+
V2 (`window.externalAppV2`) is only available when the WebView supports [`WebViewFeature.WEB_MESSAGE_LISTENER`][web-message-listener]. The app should fall back to V1 otherwise.
13+
:::
1214

13-
_This API has been introduced in Home Assistant 0.78._
15+
## Get access token
1416

1517
When the frontend loads, it will request an access token from the external authentication. It does so by calling one of the following methods with an options object. The options object defines the callback method to be called with the response and an optional `force` boolean which is set to `true` if the access token should be refreshed, regardless if it has expired or not.
1618

1719
The `force` boolean has been introduced in Home Assistant 0.104 and might not always be available.
1820

1921
```js
20-
window.externalApp.getExternalAuth({
21-
callback: "externalAuthSetToken",
22-
force: true
23-
});
24-
// or
22+
// V2 (recommended)
23+
window.externalAppV2.postMessage(
24+
JSON.stringify({
25+
type: "getExternalAuth",
26+
payload: { callback: "externalAuthSetToken", force: true },
27+
})
28+
);
29+
30+
// V1 (fallback)
31+
window.externalApp.getExternalAuth(
32+
JSON.stringify({ callback: "externalAuthSetToken", force: true })
33+
);
34+
35+
// iOS
2536
window.webkit.messageHandlers.getExternalAuth.postMessage({
2637
callback: "externalAuthSetToken",
27-
force: true
38+
force: true,
2839
});
2940
```
3041

@@ -45,17 +56,25 @@ The frontend will call this method when the page first loads and whenever it nee
4556

4657
## Revoke token
4758

48-
_This API has been introduced in Home Assistant 0.78._
49-
5059
When the user presses the logout button on the profile page, the external app will have to [revoke the refresh token](auth_api.md#revoking-a-refresh-token), and log the user out.
5160

5261
```js
53-
window.externalApp.revokeExternalAuth({
54-
callback: "externalAuthRevokeToken"
55-
});
56-
// or
62+
// V2 (recommended)
63+
window.externalAppV2.postMessage(
64+
JSON.stringify({
65+
type: "revokeExternalAuth",
66+
payload: { callback: "externalAuthRevokeToken" },
67+
})
68+
);
69+
70+
// V1 (fallback)
71+
window.externalApp.revokeExternalAuth(
72+
JSON.stringify({ callback: "externalAuthRevokeToken" })
73+
);
74+
75+
// iOS
5776
window.webkit.messageHandlers.revokeExternalAuth.postMessage({
58-
callback: "externalAuthRevokeToken"
77+
callback: "externalAuthRevokeToken",
5978
});
6079
```
6180

@@ -68,3 +87,5 @@ window.externalAuthRevokeToken(true);
6887
// If unable to logout
6988
window.externalAuthRevokeToken(false);
7089
```
90+
91+
[web-message-listener]: https://developer.android.com/reference/androidx/webkit/WebViewCompat.WebMessageListener

0 commit comments

Comments
 (0)