Add auth/authorize endpoint (#15887)

This commit is contained in:
Paulus Schoutsen 2018-08-09 09:27:54 +02:00 committed by GitHub
parent 61901496ec
commit 99c4c65f69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -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."""

View File

@ -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'