Allow CORS requests to token endpoint (#15519)
* Allow CORS requests to token endpoint * Tests * Fuck emulated hue * Clean up * Only cors existing methods
This commit is contained in:
1 parent
ca0d4226aa
commit
2fc0d83085
9 files changed
+59
-23
No files matched your search
@@ -187,8 +187,7 @@ class HomeAssistantHTTP(object):
|
||||
support_legacy=hass.auth.support_legacy,
|
||||
api_password=api_password)
|
||||
|
||||
if cors_origins:
|
||||
setup_cors(app, cors_origins)
|
||||
setup_cors(app, cors_origins)
|
||||
|
||||
app['hass'] = hass
|
||||
|
||||
@@ -226,7 +225,7 @@ class HomeAssistantHTTP(object):
|
||||
'{0} missing required attribute "name"'.format(class_name)
|
||||
)
|
||||
|
||||
view.register(self.app.router)
|
||||
view.register(self.app, self.app.router)
|
||||
|
||||
def register_redirect(self, url, redirect_to):
|
||||
"""Register a redirect with the server.
|
||||
|
||||
@@ -27,6 +27,20 @@ def setup_cors(app, origins):
|
||||
) for host in origins
|
||||
})
|
||||
|
||||
def allow_cors(route, methods):
|
||||
"""Allow cors on a route."""
|
||||
cors.add(route, {
|
||||
'*': aiohttp_cors.ResourceOptions(
|
||||
allow_headers=ALLOWED_CORS_HEADERS,
|
||||
allow_methods=methods,
|
||||
)
|
||||
})
|
||||
|
||||
app['allow_cors'] = allow_cors
|
||||
|
||||
if not origins:
|
||||
return
|
||||
|
||||
async def cors_startup(app):
|
||||
"""Initialize cors when app starts up."""
|
||||
cors_added = set()
|
||||
|
||||
@@ -26,7 +26,9 @@ class HomeAssistantView(object):
|
||||
|
||||
url = None
|
||||
extra_urls = []
|
||||
requires_auth = True # Views inheriting from this class can override this
|
||||
# Views inheriting from this class can override this
|
||||
requires_auth = True
|
||||
cors_allowed = False
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
def json(self, result, status_code=200, headers=None):
|
||||
@@ -51,10 +53,11 @@ class HomeAssistantView(object):
|
||||
data['code'] = message_code
|
||||
return self.json(data, status_code, headers=headers)
|
||||
|
||||
def register(self, router):
|
||||
def register(self, app, router):
|
||||
"""Register the view with a router."""
|
||||
assert self.url is not None, 'No url set for view'
|
||||
urls = [self.url] + self.extra_urls
|
||||
routes = []
|
||||
|
||||
for method in ('get', 'post', 'delete', 'put'):
|
||||
handler = getattr(self, method, None)
|
||||
@@ -65,13 +68,15 @@ class HomeAssistantView(object):
|
||||
handler = request_handler_factory(self, handler)
|
||||
|
||||
for url in urls:
|
||||
router.add_route(method, url, handler)
|
||||
routes.append(
|
||||
(method, router.add_route(method, url, handler))
|
||||
)
|
||||
|
||||
# aiohttp_cors does not work with class based views
|
||||
# self.app.router.add_route('*', self.url, self, name=self.name)
|
||||
if not self.cors_allowed:
|
||||
return
|
||||
|
||||
# for url in self.extra_urls:
|
||||
# self.app.router.add_route('*', url, self)
|
||||
for method, route in routes:
|
||||
app['allow_cors'](route, [method.upper()])
|
||||
|
||||
|
||||
def request_handler_factory(view, handler):
|
||||
|
||||
Reference in new issue
Block a user