Add permission checks to Rest API (#18639)
* Add permission checks to Rest API * Clean up unnecessary method * Remove all the tuple stuff from entity check * Simplify perms * Correct param name for owner permission * Hass.io make/update user to be admin * Types
This commit is contained in:
1 parent
f387cdec59
commit
8b8629a5f4
15 files changed
+282
-145
No files matched your search
@@ -20,7 +20,8 @@ from homeassistant.const import (
|
||||
URL_API_SERVICES, URL_API_STATES, URL_API_STATES_ENTITY, URL_API_STREAM,
|
||||
URL_API_TEMPLATE, __version__)
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.auth.permissions.const import POLICY_READ
|
||||
from homeassistant.exceptions import TemplateError, Unauthorized
|
||||
from homeassistant.helpers import template
|
||||
from homeassistant.helpers.service import async_get_all_descriptions
|
||||
from homeassistant.helpers.state import AsyncTrackStates
|
||||
@@ -81,6 +82,8 @@ class APIEventStream(HomeAssistantView):
|
||||
|
||||
async def get(self, request):
|
||||
"""Provide a streaming interface for the event bus."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized()
|
||||
hass = request.app['hass']
|
||||
stop_obj = object()
|
||||
to_write = asyncio.Queue(loop=hass.loop)
|
||||
@@ -185,7 +188,13 @@ class APIStatesView(HomeAssistantView):
|
||||
@ha.callback
|
||||
def get(self, request):
|
||||
"""Get current states."""
|
||||
return self.json(request.app['hass'].states.async_all())
|
||||
user = request['hass_user']
|
||||
entity_perm = user.permissions.check_entity
|
||||
states = [
|
||||
state for state in request.app['hass'].states.async_all()
|
||||
if entity_perm(state.entity_id, 'read')
|
||||
]
|
||||
return self.json(states)
|
||||
|
||||
|
||||
class APIEntityStateView(HomeAssistantView):
|
||||
@@ -197,6 +206,10 @@ class APIEntityStateView(HomeAssistantView):
|
||||
@ha.callback
|
||||
def get(self, request, entity_id):
|
||||
"""Retrieve state of entity."""
|
||||
user = request['hass_user']
|
||||
if not user.permissions.check_entity(entity_id, POLICY_READ):
|
||||
raise Unauthorized(entity_id=entity_id)
|
||||
|
||||
state = request.app['hass'].states.get(entity_id)
|
||||
if state:
|
||||
return self.json(state)
|
||||
@@ -204,6 +217,8 @@ class APIEntityStateView(HomeAssistantView):
|
||||
|
||||
async def post(self, request, entity_id):
|
||||
"""Update state of entity."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized(entity_id=entity_id)
|
||||
hass = request.app['hass']
|
||||
try:
|
||||
data = await request.json()
|
||||
@@ -236,6 +251,8 @@ class APIEntityStateView(HomeAssistantView):
|
||||
@ha.callback
|
||||
def delete(self, request, entity_id):
|
||||
"""Remove entity."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized(entity_id=entity_id)
|
||||
if request.app['hass'].states.async_remove(entity_id):
|
||||
return self.json_message("Entity removed.")
|
||||
return self.json_message("Entity not found.", HTTP_NOT_FOUND)
|
||||
@@ -261,6 +278,8 @@ class APIEventView(HomeAssistantView):
|
||||
|
||||
async def post(self, request, event_type):
|
||||
"""Fire events."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized()
|
||||
body = await request.text()
|
||||
try:
|
||||
event_data = json.loads(body) if body else None
|
||||
@@ -346,6 +365,8 @@ class APITemplateView(HomeAssistantView):
|
||||
|
||||
async def post(self, request):
|
||||
"""Render a template."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized()
|
||||
try:
|
||||
data = await request.json()
|
||||
tpl = template.Template(data['template'], request.app['hass'])
|
||||
@@ -363,6 +384,8 @@ class APIErrorLog(HomeAssistantView):
|
||||
|
||||
async def get(self, request):
|
||||
"""Retrieve API error log."""
|
||||
if not request['hass_user'].is_admin:
|
||||
raise Unauthorized()
|
||||
return web.FileResponse(request.app['hass'].data[DATA_LOGGING])
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user