Skip to content

Commit 71cc397

Browse files
committed
feat: add option to configure button text on login screen
1 parent 4a48579 commit 71cc397

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Note the client id and client secret as you will need it setting up this integra
3636
username_field: "preferred_username" # Adjust based on your IdP's user info response
3737
scope: "openid profile email"
3838
block_login: false
39+
openid_text: "Login with OpenID / OAuth2" # Text to display on the login page
3940
```
4041
2. Replace the placeholders (`YOUR_CLIENT_ID`, `YOUR_CLIENT_SECRET`, etc.) with the details provided by your Identity Provider.
4142
3. Restart Home Assistant.

custom_components/openid/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
CONF_BLOCK_LOGIN,
2323
CONF_CONFIGURE_URL,
2424
CONF_CREATE_USER,
25+
CONF_OPENID_TEXT,
2526
CONF_SCOPE,
2627
CONF_TOKEN_URL,
2728
CONF_USER_INFO_URL,
@@ -49,6 +50,9 @@
4950
): cv.string,
5051
vol.Optional(CONF_CREATE_USER, default=False): cv.boolean,
5152
vol.Optional(CONF_BLOCK_LOGIN, default=False): cv.boolean,
53+
vol.Optional(
54+
CONF_OPENID_TEXT, default="OpenID / OAuth2 Authentication"
55+
): cv.string,
5256
}
5357
)
5458
},
@@ -101,8 +105,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
101105
# Patch /auth/authorize to inject our JS file.
102106
override_authorize_route(hass)
103107

104-
if hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False):
105-
override_authorize_login_flow(hass)
108+
# Patch the login flow to include additional OpenID data.
109+
override_authorize_login_flow(hass)
106110

107111
return True
108112

custom_components/openid/authorize.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ window.fetch = async (...args) => {
1919
return response;
2020
}
2121

22+
const openIdText = responseBody.openid_text;
23+
2224
const authFlow = document.getElementsByClassName('card-content')[0];
2325

2426
const listNode = document.createElement('ha-list');
2527
const listItemNode = document.createElement('ha-list-item');
2628
listItemNode.setAttribute('hasmeta', '');
2729
listItemNode.setAttribute('mwc-list-item', '');
28-
listItemNode.innerHTML = 'OpenID / OAuth2 Authentication <ha-icon-next slot="meta"></ha-icon-next>';
30+
listItemNode.innerHTML = `${openIdText} <ha-icon-next slot="meta"></ha-icon-next>`;
2931
listItemNode.onclick = redirect_openid_login;
3032

3133
listNode.appendChild(listItemNode);

custom_components/openid/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
CONF_CREATE_USER = "create_user"
1616
CONF_BLOCK_LOGIN = "block_login"
1717
CONF_USE_HEADER_AUTH = "use_header_auth"
18+
CONF_OPENID_TEXT = "openid_text"

custom_components/openid/http_helper.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@
88

99
from homeassistant.core import HomeAssistant
1010

11-
from .const import CONF_BLOCK_LOGIN, DOMAIN
11+
from .const import CONF_BLOCK_LOGIN, CONF_OPENID_TEXT, DOMAIN
1212

1313
_LOGGER = logging.getLogger(__name__)
1414

1515

1616
def override_authorize_login_flow(hass: HomeAssistant) -> None:
1717
"""Patch the build-in /auth/login_flow page to not return any actual login data."""
1818

19-
async def get(request: Request) -> Response:
20-
content = {
21-
"type": "form",
22-
"flow_id": None,
23-
"handler": [None],
24-
"data_schema": [],
25-
"errors": {},
26-
"description_placeholders": None,
27-
"last_step": None,
28-
"preview": None,
29-
"step_id": "init",
30-
"block_login": hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False),
31-
}
19+
_original_post_function = None
20+
21+
async def post(request: Request) -> Response:
22+
if not hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False):
23+
content = json.loads((await _original_post_function(request)).text)
24+
else:
25+
content = {
26+
"type": "form",
27+
"flow_id": None,
28+
"handler": [None],
29+
"data_schema": [],
30+
"errors": {},
31+
"description_placeholders": None,
32+
"last_step": None,
33+
"preview": None,
34+
"step_id": "init",
35+
}
36+
37+
content[CONF_BLOCK_LOGIN] = hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False)
38+
content[CONF_OPENID_TEXT] = hass.data[DOMAIN].get(
39+
CONF_OPENID_TEXT, "OpenID / OAuth2 Authentication"
40+
)
3241

3342
return Response(
3443
status=HTTPStatus.OK,
@@ -39,11 +48,12 @@ async def get(request: Request) -> Response:
3948
# Swap out the existing GET handler on /auth/authorize
4049
for resource in hass.http.app.router._resources: # noqa: SLF001
4150
if getattr(resource, "canonical", None) == "/auth/login_flow":
42-
get_handler = resource._routes.get("GET") # noqa: SLF001
51+
post_handler = resource._routes.get("POST") # noqa: SLF001
4352
# Replace the underlying coroutine fn.
44-
get_handler._handler = get # noqa: SLF001
53+
_original_post_function = post_handler._handler # noqa: SLF001
54+
post_handler._handler = post # noqa: SLF001
4555
# Reset the routes map to ensure only our GET exists.
46-
resource._routes = {"GET": get_handler, "POST": get_handler} # noqa: SLF001
56+
resource._routes = {"POST": post_handler} # noqa: SLF001
4757
_LOGGER.debug("Overrode /auth/login_flow route")
4858
break
4959

0 commit comments

Comments
 (0)