88
99from 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
1616def 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