Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/auth/providers/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ async def async_will_remove_credentials(self, credentials):
# Can happen if somehow we didn't clean up a credential
pass

async def async_change_password(self, username, new_password):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, just call straight the data object

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still use executor for password hashing though right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

"""Helper to change a user's password."""
if self.data is None:
await self.async_initialize()

await self.hass.async_add_executor_job(
self.data.change_password, username, new_password)


class LoginFlow(data_entry_flow.FlowHandler):
"""Handler for the login flow."""
Expand Down
52 changes: 52 additions & 0 deletions homeassistant/components/config/auth_provider_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
vol.Required('username'): str,
})

WS_TYPE_CHANGE_PASSWORD = 'config/auth_provider/homeassistant/change_password'
SCHEMA_WS_CHANGE_PASSWORD = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
vol.Required('type'): WS_TYPE_CHANGE_PASSWORD,
vol.Required('current_password'): str,
vol.Required('new_password'): str
})


async def async_setup(hass):
"""Enable the Home Assistant views."""
Expand All @@ -31,6 +38,10 @@ async def async_setup(hass):
WS_TYPE_DELETE, websocket_delete,
SCHEMA_WS_DELETE
)
hass.components.websocket_api.async_register_command(
WS_TYPE_CHANGE_PASSWORD, websocket_change_password,
SCHEMA_WS_CHANGE_PASSWORD
)
return True


Expand Down Expand Up @@ -118,3 +129,44 @@ async def delete_creds():
websocket_api.result_message(msg['id']))

hass.async_add_job(delete_creds())


@callback
def websocket_change_password(hass, connection, msg):
"""Change user password."""
async def change_password():
"""Change user password."""
provider = _get_provider(hass)
await provider.async_initialize()

user = connection.request.get('hass_user')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to do user check first

if user is None:
connection.send_message_outside(websocket_api.error_message(
msg['id'], 'user_not_found', 'User not found'))
return

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a break


if username is None:
connection.send_message_outside(websocket_api.error_message(
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_message_outside(websocket_api.error_message(
msg['id'], 'invalid_password', 'Invalid password'))
return

await provider.async_change_password(username, msg['new_password'])
await provider.data.async_save()

connection.to_write.put_nowait(
websocket_api.result_message(msg['id']))

hass.async_add_job(change_password())