Initialize the flask application for the multi-faction authentication end-points, when the SERVER_MODE is set to True, and MFA_ENABLED is set to True in the configuration file. Args: app (Flask): Flask Application object
(app: Flask)
| 44 | |
| 45 | |
| 46 | def init_app(app: Flask): |
| 47 | """ |
| 48 | Initialize the flask application for the multi-faction authentication |
| 49 | end-points, when the SERVER_MODE is set to True, and MFA_ENABLED is set to |
| 50 | True in the configuration file. |
| 51 | |
| 52 | Args: |
| 53 | app (Flask): Flask Application object |
| 54 | """ |
| 55 | |
| 56 | if getattr(config, "SERVER_MODE", False) is False and \ |
| 57 | getattr(config, "MFA_ENABLED", False) is False: |
| 58 | return |
| 59 | |
| 60 | MultiFactorAuthRegistry.load_modules(app) |
| 61 | |
| 62 | def exclude_invalid_mfa_auth_methods(): |
| 63 | """ |
| 64 | Exclude the invalid MFA auth methods specified in MFA_SUPPORTED_METHODS |
| 65 | configuration. |
| 66 | """ |
| 67 | |
| 68 | supported_methods = getattr(config, "MFA_SUPPORTED_METHODS", []) |
| 69 | invalid_auth_methods = [] |
| 70 | |
| 71 | supported_methods, invalid_auth_methods = \ |
| 72 | segregate_valid_and_invalid_mfa_methods(supported_methods) |
| 73 | |
| 74 | for auth_method in invalid_auth_methods: |
| 75 | app.logger.warning(_( |
| 76 | "'{}' is not a valid multi-factor authentication method" |
| 77 | ).format(auth_method)) |
| 78 | |
| 79 | config.MFA_SUPPORTED_METHODS = supported_methods |
| 80 | blueprint = __create_blueprint() |
| 81 | |
| 82 | for mfa_method in supported_methods: |
| 83 | mfa = MultiFactorAuthRegistry.get(mfa_method) |
| 84 | mfa.register_url_endpoints(blueprint) |
| 85 | |
| 86 | app.register_blueprint(blueprint) |
| 87 | app.register_logout_hook(blueprint) |
| 88 | |
| 89 | from flask_login import user_logged_out |
| 90 | |
| 91 | @user_logged_out.connect_via(app) |
| 92 | def clear_session_on_login(sender, user): |
| 93 | session['mfa_authenticated'] = False |
| 94 | |
| 95 | def disable_mfa(): |
| 96 | """ |
| 97 | Set MFA_ENABLED configuration to False. |
| 98 | |
| 99 | Also - log a warning message about no valid authentication method found |
| 100 | during initialization. |
| 101 | """ |
| 102 | if getattr(config, 'MFA_ENABLED', False) is True and \ |
| 103 | getattr(config, 'SERVER_MODE', False) is True: |