Ejemplo n.º 1
0
async def websocket_create(hass, connection, msg):
    """Create credentials and attach to a user."""
    provider = auth_ha.async_get_provider(hass)
    user = await hass.auth.async_get_user(msg["user_id"])

    if user is None:
        connection.send_error(msg["id"], "not_found", "User not found")
        return

    if user.system_generated:
        connection.send_error(
            msg["id"],
            "system_generated",
            "Cannot add credentials to a system generated user.",
        )
        return

    try:
        await provider.async_add_auth(msg["username"], msg["password"])
    except auth_ha.InvalidUser:
        connection.send_error(msg["id"], "username_exists", "Username already exists")
        return

    credentials = await provider.async_get_or_create_credentials(
        {"username": msg["username"]}
    )
    await hass.auth.async_link_user(user, credentials)

    connection.send_result(msg["id"])
Ejemplo n.º 2
0
async def websocket_change_password(hass, connection, msg):
    """Change current user password."""
    user = connection.user
    if user is None:
        connection.send_error(msg["id"], "user_not_found", "User not found")
        return

    provider = auth_ha.async_get_provider(hass)
    username = None
    for credential in user.credentials:
        if credential.auth_provider_type == provider.type:
            username = credential.data["username"]
            break

    if username is None:
        connection.send_error(
            msg["id"], "credentials_not_found", "Credentials not found"
        )
        return

    try:
        await provider.async_validate_login(username, msg["current_password"])
    except auth_ha.InvalidAuth:
        connection.send_error(msg["id"], "invalid_password", "Invalid password")
        return

    await provider.async_change_password(username, msg["new_password"])

    connection.send_result(msg["id"])
Ejemplo n.º 3
0
async def websocket_admin_change_password(hass, connection, msg):
    """Change password of any user."""
    if not connection.user.is_owner:
        raise Unauthorized(context=connection.context(msg))

    user = await hass.auth.async_get_user(msg["user_id"])

    if user is None:
        connection.send_error(msg["id"], "user_not_found", "User not found")
        return

    provider = auth_ha.async_get_provider(hass)

    username = None
    for credential in user.credentials:
        if credential.auth_provider_type == provider.type:
            username = credential.data["username"]
            break

    if username is None:
        connection.send_error(
            msg["id"], "credentials_not_found", "Credentials not found"
        )
        return

    try:
        await provider.async_change_password(username, msg["password"])
        connection.send_result(msg["id"])
    except auth_ha.InvalidUser:
        connection.send_error(
            msg["id"], "credentials_not_found", "Credentials not found"
        )
        return
async def websocket_create(hass, connection, msg):
    """Create credentials and attach to a user."""
    provider = auth_ha.async_get_provider(hass)

    if (user := await hass.auth.async_get_user(msg["user_id"])) is None:
        connection.send_error(msg["id"], "not_found", "User not found")
        return
Ejemplo n.º 5
0
    async def post(self, request, data):
        """Handle auth requests."""
        self._check_access(request)
        provider = auth_ha.async_get_provider(request.app["hass"])

        try:
            await provider.async_validate_login(data[ATTR_USERNAME],
                                                data[ATTR_PASSWORD])
        except auth_ha.InvalidAuth:
            raise HTTPUnauthorized() from None

        return web.Response(status=HTTP_OK)
Ejemplo n.º 6
0
    async def post(self, request, data):
        """Handle password reset requests."""
        self._check_access(request)
        provider = auth_ha.async_get_provider(request.app["hass"])

        try:
            await provider.async_change_password(data[ATTR_USERNAME],
                                                 data[ATTR_PASSWORD])
        except auth_ha.InvalidUser as err:
            raise HTTPNotFound() from err

        return web.Response(status=HTTP_OK)
Ejemplo n.º 7
0
async def websocket_delete(hass, connection, msg):
    """Delete username and related credential."""
    provider = auth_ha.async_get_provider(hass)
    credentials = await provider.async_get_or_create_credentials(
        {"username": msg["username"]})

    # if not new, an existing credential exists.
    # Removing the credential will also remove the auth.
    if not credentials.is_new:
        await hass.auth.async_remove_credentials(credentials)

        connection.send_result(msg["id"])
        return

    try:
        await provider.async_remove_auth(msg["username"])
    except auth_ha.InvalidUser:
        connection.send_error(msg["id"], "auth_not_found",
                              "Given username was not found.")
        return

    connection.send_result(msg["id"])
    connection.send_result(msg["id"])


@decorators.websocket_command({
    vol.Required("type"): "config/auth_provider/homeassistant/change_password",
    vol.Required("current_password"): str,
    vol.Required("new_password"): str,
})
@websocket_api.async_response
async def websocket_change_password(hass, connection, msg):
    """Change current user password."""
    if (user := connection.user) is None:
        connection.send_error(msg["id"], "user_not_found", "User not found")
        return

    provider = auth_ha.async_get_provider(hass)
    username = None
    for credential in user.credentials:
        if credential.auth_provider_type == provider.type:
            username = credential.data["username"]
            break

    if username is None:
        connection.send_error(msg["id"], "credentials_not_found",
                              "Credentials not found")
        return

    try:
        await provider.async_validate_login(username, msg["current_password"])
    except auth_ha.InvalidAuth:
        connection.send_error(msg["id"], "invalid_current_password",