From 99c4c65f6926ace28a69f72f9c7759339fda7b29 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 9 Aug 2018 09:27:54 +0200 Subject: [PATCH] Add auth/authorize endpoint (#15887) --- homeassistant/components/frontend/__init__.py | 30 +++++++++++++++++++ tests/components/frontend/test_init.py | 11 +++++++ 2 files changed, 41 insertions(+) diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 540341c68f2..0dcf7526262 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -249,6 +249,7 @@ async def async_setup(hass, config): index_view = IndexView(repo_path, js_version, hass.auth.active) hass.http.register_view(index_view) + hass.http.register_view(AuthorizeView(repo_path, js_version)) @callback def async_finalize_panel(panel): @@ -334,6 +335,35 @@ def _async_setup_themes(hass, themes): hass.services.async_register(DOMAIN, SERVICE_RELOAD_THEMES, reload_themes) +class AuthorizeView(HomeAssistantView): + """Serve the frontend.""" + + url = '/auth/authorize' + name = 'auth:authorize' + requires_auth = False + + def __init__(self, repo_path, js_option): + """Initialize the frontend view.""" + self.repo_path = repo_path + self.js_option = js_option + + async def get(self, request: web.Request): + """Redirect to the authorize page.""" + latest = self.repo_path is not None or \ + _is_latest(self.js_option, request) + + if latest: + location = '/frontend_latest/authorize.html' + else: + location = '/frontend_es5/authorize.html' + + location += '?{}'.format(request.query_string) + + return web.Response(status=302, headers={ + 'location': location + }) + + class IndexView(HomeAssistantView): """Serve the frontend.""" diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index dfa67f48614..17bf3d953ef 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -348,3 +348,14 @@ async def test_onboarding_load(mock_http_client): """Test onboarding component loaded by default.""" resp = await mock_http_client.get('/api/onboarding') assert resp.status == 200 + + +async def test_auth_authorize(mock_http_client): + """Test the authorize endpoint works.""" + resp = await mock_http_client.get('/auth/authorize?hello=world') + assert resp.url.query_string == 'hello=world' + assert resp.url.path == '/frontend_es5/authorize.html' + + resp = await mock_http_client.get('/auth/authorize?latest&hello=world') + assert resp.url.query_string == 'latest&hello=world' + assert resp.url.path == '/frontend_latest/authorize.html'