Hass.io auth/sso part2 (#17324)

* Update discovery.py

* Create const.py

* Update auth.py

* Update const.py

* Update const.py

* Update http.py

* Update handler.py

* Update auth.py

* Update auth.py

* Update test_auth.py
pull/17164/merge
Pascal Vizeli 2018-10-11 10:55:38 +02:00 committed by Paulus Schoutsen
parent cffb704311
commit f5d3aa1826
6 changed files with 54 additions and 17 deletions

View File

@ -14,16 +14,16 @@ from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.const import KEY_REAL_IP
from homeassistant.components.http.data_validator import RequestDataValidator
_LOGGER = logging.getLogger(__name__)
from .const import ATTR_USERNAME, ATTR_PASSWORD, ATTR_ADDON
ATTR_USERNAME = 'username'
ATTR_PASSWORD = 'password'
_LOGGER = logging.getLogger(__name__)
SCHEMA_API_AUTH = vol.Schema({
vol.Required(ATTR_USERNAME): cv.string,
vol.Required(ATTR_PASSWORD): cv.string,
})
vol.Required(ATTR_ADDON): cv.string,
}, extra=vol.ALLOW_EXTRA)
@callback

View File

@ -0,0 +1,12 @@
"""Hass.io const variables."""
ATTR_DISCOVERY = 'discovery'
ATTR_ADDON = 'addon'
ATTR_NAME = 'name'
ATTR_SERVICE = 'service'
ATTR_CONFIG = 'config'
ATTR_UUID = 'uuid'
ATTR_USERNAME = 'username'
ATTR_PASSWORD = 'password'
X_HASSIO = 'X-HASSIO-KEY'

View File

@ -10,16 +10,12 @@ from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.components.http import HomeAssistantView
from .handler import HassioAPIError
from .const import (
ATTR_DISCOVERY, ATTR_ADDON, ATTR_NAME, ATTR_SERVICE, ATTR_CONFIG,
ATTR_UUID)
_LOGGER = logging.getLogger(__name__)
ATTR_DISCOVERY = 'discovery'
ATTR_ADDON = 'addon'
ATTR_NAME = 'name'
ATTR_SERVICE = 'service'
ATTR_CONFIG = 'config'
ATTR_UUID = 'uuid'
@callback
def async_setup_discovery(hass, hassio, config):

View File

@ -16,9 +16,9 @@ from homeassistant.components.http import (
CONF_SSL_CERTIFICATE)
from homeassistant.const import CONF_TIME_ZONE, SERVER_PORT
_LOGGER = logging.getLogger(__name__)
from .const import X_HASSIO
X_HASSIO = 'X-HASSIO-KEY'
_LOGGER = logging.getLogger(__name__)
class HassioAPIError(RuntimeError):

View File

@ -18,9 +18,10 @@ from aiohttp.web_exceptions import HTTPBadGateway
from homeassistant.const import CONTENT_TYPE_TEXT_PLAIN
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
from .const import X_HASSIO
_LOGGER = logging.getLogger(__name__)
X_HASSIO = 'X-HASSIO-KEY'
NO_TIMEOUT = re.compile(
r'^(?:'

View File

@ -19,7 +19,8 @@ async def test_login_success(hass, hassio_client):
'/api/hassio_auth',
json={
"username": "test",
"password": "123456"
"password": "123456",
"addon": "samba",
},
headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
@ -42,7 +43,8 @@ async def test_login_error(hass, hassio_client):
'/api/hassio_auth',
json={
"username": "test",
"password": "123456"
"password": "123456",
"addon": "samba",
},
headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
@ -83,7 +85,8 @@ async def test_login_no_username(hass, hassio_client):
resp = await hassio_client.post(
'/api/hassio_auth',
json={
"password": "123456"
"password": "123456",
"addon": "samba",
},
headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
@ -93,3 +96,28 @@ async def test_login_no_username(hass, hassio_client):
# Check we got right response
assert resp.status == 400
assert not mock_login.called
async def test_login_success_extra(hass, hassio_client):
"""Test auth with extra data."""
await register_auth_provider(hass, {'type': 'homeassistant'})
with patch('homeassistant.auth.providers.homeassistant.'
'HassAuthProvider.async_validate_login',
Mock(return_value=mock_coro())) as mock_login:
resp = await hassio_client.post(
'/api/hassio_auth',
json={
"username": "test",
"password": "123456",
"addon": "samba",
"path": "/share",
},
headers={
HTTP_HEADER_HA_AUTH: API_PASSWORD
}
)
# Check we got right response
assert resp.status == 200
mock_login.assert_called_with("test", "123456")