Skip to content

Commit 4a48579

Browse files
committed
fix: block login behind certain proxies
1 parent 518aff6 commit 4a48579

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

custom_components/openid/authorize.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ window.fetch = async (...args) => {
1212
window.fetch = originalFetch;
1313

1414
const responseBody = await response.clone().json();
15-
console.log('Response from /auth/login_flow:', responseBody);
15+
16+
if (responseBody.block_login) {
17+
console.info('Home Assistant login methods are blocked by hass-openid. Redirecting to OpenID login.');
18+
redirect_openid_login();
19+
return response;
20+
}
1621

1722
const authFlow = document.getElementsByClassName('card-content')[0];
1823

@@ -21,15 +26,7 @@ window.fetch = async (...args) => {
2126
listItemNode.setAttribute('hasmeta', '');
2227
listItemNode.setAttribute('mwc-list-item', '');
2328
listItemNode.innerHTML = 'OpenID / OAuth2 Authentication <ha-icon-next slot="meta"></ha-icon-next>';
24-
listItemNode.onclick = () => {
25-
const urlParams = new URLSearchParams(window.location.search);
26-
const clientId = encodeURIComponent(urlParams.get('client_id'));
27-
const redirectUri = encodeURIComponent(urlParams.get('redirect_uri'));
28-
const baseUrl = window.location.origin;
29-
30-
const encodedUrl = encodeURIComponent(`/auth/openid/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&base_url=${baseUrl}`);
31-
window.location.href = decodeURIComponent(encodedUrl);
32-
};
29+
listItemNode.onclick = redirect_openid_login;
3330

3431
listNode.appendChild(listItemNode);
3532
authFlow.append(listNode);
@@ -48,3 +45,13 @@ window.fetch = async (...args) => {
4845

4946
return response;
5047
};
48+
49+
function redirect_openid_login() {
50+
const urlParams = new URLSearchParams(window.location.search);
51+
const clientId = encodeURIComponent(urlParams.get('client_id'));
52+
const redirectUri = encodeURIComponent(urlParams.get('redirect_uri'));
53+
const baseUrl = window.location.origin;
54+
55+
const encodedUrl = encodeURIComponent(`/auth/openid/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&base_url=${baseUrl}`);
56+
window.location.href = decodeURIComponent(encodedUrl);
57+
}

custom_components/openid/http_helper.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import json
55
import logging
66

7-
from aiohttp.web import HTTPFound, Request, Response
7+
from aiohttp.web import Request, Response
88

9-
from homeassistant.const import CONF_CLIENT_ID
109
from homeassistant.core import HomeAssistant
1110

1211
from .const import CONF_BLOCK_LOGIN, DOMAIN
@@ -28,6 +27,7 @@ async def get(request: Request) -> Response:
2827
"last_step": None,
2928
"preview": None,
3029
"step_id": "init",
30+
"block_login": hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False),
3131
}
3232

3333
return Response(
@@ -52,17 +52,6 @@ def override_authorize_route(hass: HomeAssistant) -> None:
5252
"""Patch the built-in /auth/authorize page to load our JS helper."""
5353

5454
async def get(request: Request) -> Response:
55-
if hass.data[DOMAIN].get(CONF_BLOCK_LOGIN, False):
56-
get_params = request.rel_url.query
57-
client_id = get_params.get(CONF_CLIENT_ID)
58-
redirect_uri = get_params.get("redirect_uri", "/")
59-
60-
return HTTPFound(
61-
location=str(
62-
f"/auth/openid/authorize?client_id={client_id}&redirect_uri={redirect_uri}"
63-
)
64-
)
65-
6655
content = hass.data[DOMAIN]["authorize_template"]
6756

6857
# Inject script before </head>

custom_components/openid/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"iot_class": "cloud_polling",
1212
"issue_tracker": "https://github.com/cavefire/hass-openid/issues",
1313
"requirements": [],
14-
"version": "1.1.2"
14+
"version": "1.1.6"
1515
}

0 commit comments

Comments
 (0)