-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi-factor auth module setup flow #16141
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92e6abc
Add mfa setup flow
awarecan 7d87659
Lint
awarecan 1a68117
Address code review comment
awarecan 8f7c5b2
Fix unit test
awarecan d4e01f9
Add assertion for WS response ordering
awarecan af963a3
Missed a return
awarecan 409a76d
Remove setup_schema from MFA base class
awarecan 58e6b4a
Move auth.util.validate_current_user -> webscoket_api.ws_require_user
awarecan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"""Helpers to setup multi-factor auth module.""" | ||
import logging | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant import data_entry_flow | ||
from homeassistant.components import websocket_api | ||
from homeassistant.core import callback, HomeAssistant | ||
|
||
WS_TYPE_SETUP_MFA = 'auth/setup_mfa' | ||
SCHEMA_WS_SETUP_MFA = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ | ||
vol.Required('type'): WS_TYPE_SETUP_MFA, | ||
vol.Exclusive('mfa_module_id', 'module_or_flow_id'): str, | ||
vol.Exclusive('flow_id', 'module_or_flow_id'): str, | ||
vol.Optional('user_input'): object, | ||
}) | ||
|
||
WS_TYPE_DEPOSE_MFA = 'auth/depose_mfa' | ||
SCHEMA_WS_DEPOSE_MFA = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({ | ||
vol.Required('type'): WS_TYPE_DEPOSE_MFA, | ||
vol.Required('mfa_module_id'): str, | ||
}) | ||
|
||
DATA_SETUP_FLOW_MGR = 'auth_mfa_setup_flow_manager' | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass): | ||
"""Init mfa setup flow manager.""" | ||
async def _async_create_setup_flow(handler, context, data): | ||
"""Create a setup flow. hanlder is a mfa module.""" | ||
mfa_module = hass.auth.get_auth_mfa_module(handler) | ||
if mfa_module is None: | ||
raise ValueError('Mfa module {} is not found'.format(handler)) | ||
|
||
user_id = data.pop('user_id') | ||
return await mfa_module.async_setup_flow(user_id) | ||
|
||
async def _async_finish_setup_flow(flow, flow_result): | ||
_LOGGER.debug('flow_result: %s', flow_result) | ||
return flow_result | ||
|
||
hass.data[DATA_SETUP_FLOW_MGR] = data_entry_flow.FlowManager( | ||
hass, _async_create_setup_flow, _async_finish_setup_flow) | ||
|
||
hass.components.websocket_api.async_register_command( | ||
WS_TYPE_SETUP_MFA, websocket_setup_mfa, SCHEMA_WS_SETUP_MFA) | ||
|
||
hass.components.websocket_api.async_register_command( | ||
WS_TYPE_DEPOSE_MFA, websocket_depose_mfa, SCHEMA_WS_DEPOSE_MFA) | ||
|
||
|
||
@callback | ||
@websocket_api.ws_require_user(allow_system_user=False) | ||
def websocket_setup_mfa( | ||
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg): | ||
"""Return a setup flow for mfa auth module.""" | ||
async def async_setup_flow(msg): | ||
"""Return a setup flow for mfa auth module.""" | ||
flow_manager = hass.data[DATA_SETUP_FLOW_MGR] | ||
|
||
flow_id = msg.get('flow_id') | ||
if flow_id is not None: | ||
result = await flow_manager.async_configure( | ||
flow_id, msg.get('user_input')) | ||
connection.send_message_outside( | ||
websocket_api.result_message( | ||
msg['id'], _prepare_result_json(result))) | ||
return | ||
|
||
mfa_module_id = msg.get('mfa_module_id') | ||
mfa_module = hass.auth.get_auth_mfa_module(mfa_module_id) | ||
if mfa_module is None: | ||
connection.send_message_outside(websocket_api.error_message( | ||
msg['id'], 'no_module', | ||
'MFA module {} is not found'.format(mfa_module_id))) | ||
return | ||
|
||
result = await flow_manager.async_init( | ||
mfa_module_id, data={'user_id': connection.user.id}) | ||
|
||
connection.send_message_outside( | ||
websocket_api.result_message( | ||
msg['id'], _prepare_result_json(result))) | ||
|
||
hass.async_create_task(async_setup_flow(msg)) | ||
|
||
|
||
@callback | ||
@websocket_api.ws_require_user(allow_system_user=False) | ||
def websocket_depose_mfa( | ||
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg): | ||
"""Remove user from mfa module.""" | ||
async def async_depose(msg): | ||
"""Remove user from mfa auth module.""" | ||
mfa_module_id = msg['mfa_module_id'] | ||
try: | ||
await hass.auth.async_disable_user_mfa( | ||
connection.user, msg['mfa_module_id']) | ||
except ValueError as err: | ||
connection.send_message_outside(websocket_api.error_message( | ||
msg['id'], 'disable_failed', | ||
'Cannot disable MFA Module {}: {}'.format( | ||
mfa_module_id, err))) | ||
return | ||
|
||
connection.send_message_outside( | ||
websocket_api.result_message( | ||
msg['id'], 'done')) | ||
|
||
hass.async_create_task(async_depose(msg)) | ||
|
||
|
||
def _prepare_result_json(result): | ||
"""Convert result to JSON.""" | ||
if result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY: | ||
data = result.copy() | ||
return data | ||
|
||
if result['type'] != data_entry_flow.RESULT_TYPE_FORM: | ||
return result | ||
|
||
import voluptuous_serialize | ||
|
||
data = result.copy() | ||
|
||
schema = data['data_schema'] | ||
if schema is None: | ||
data['data_schema'] = [] | ||
else: | ||
data['data_schema'] = voluptuous_serialize.convert(schema) | ||
|
||
return data |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is optional, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: it is required. MFA module has to be set up to enable.