diff --git a/docs/en_US/getting_started.rst b/docs/en_US/getting_started.rst index 700e8d48b..11ae656dc 100644 --- a/docs/en_US/getting_started.rst +++ b/docs/en_US/getting_started.rst @@ -33,6 +33,7 @@ Mode is pre-configured for security. deployment login + mfa user_management change_user_password restore_locked_user diff --git a/docs/en_US/mfa.rst b/docs/en_US/mfa.rst new file mode 100644 index 000000000..96e34d5d7 --- /dev/null +++ b/docs/en_US/mfa.rst @@ -0,0 +1,35 @@ +.. _mfa: + +************************************************* +`Enabling two-factor authentication (2FA)`:index: +************************************************* + +About two-factor authentication +=============================== +Two-factor authentication (2FA) is an extra layer of security used when logging +into websites or apps. With 2FA, you have to log in with your username and +password and provide another form of authentication that only you know or have +access to. + + +Setup two-factor authentication +=============================== +To set up 2FA for pgAdmin 4, you must configure the Two-factor Authentication +settings in *config_local.py* or *config_system.py* (see the +:ref:`config.py ` documentation) on the system where pgAdmin is +installed in Server mode. You can copy these settings from *config.py* file and +modify the values for the following parameters. + +.. csv-table:: + :header: "**Parameter**", "**Description**" + :class: longtable + :widths: 35, 55 + + "MFA_ENABLED","The default value for this parameter isĀ False. + To enable 2FA, set the value to *True*" + "SUPPORTED_MFA_LIST", "Set the authentication methods to be supported " + "MFA_EMAIL_SUBJECT", " - Verification Code" + +*NOTE: You must set the 'Mail server settings' in config_local.py or +config_system.py in order to use 'email' as two-factor authentication method +(see the* :ref:`config.py ` *documentation).* diff --git a/docs/en_US/release_notes_5_7.rst b/docs/en_US/release_notes_5_7.rst index e102b6954..9c0196c72 100644 --- a/docs/en_US/release_notes_5_7.rst +++ b/docs/en_US/release_notes_5_7.rst @@ -13,6 +13,7 @@ New features | `Issue #4264 `_ - Make code folding case insensitive in the code mirror. | `Issue #4629 `_ - Added database and server information on the Maintenance process watcher dialog. | `Issue #6495 `_ - Allow the referenced table to be the same as the local table in one to many relationship for ERD Tool. +| `Issue #6495 `_ - Added support for Two-factor authentication for improving security. | `Issue #6691 `_ - Set PSQLRC and PSQL_HISTORY env vars to apt. user storage path in the server mode. Housekeeping diff --git a/requirements.txt b/requirements.txt index d45566a0b..535736738 100644 --- a/requirements.txt +++ b/requirements.txt @@ -43,3 +43,6 @@ user-agents==2.2.0 pywinpty==1.1.1; sys_platform=="win32" Authlib==0.15.* requests==2.25.* +pyotp==2.* +qrcode==7.* +Pillow==8.3.* diff --git a/web/.eslintignore b/web/.eslintignore index 676a4f03f..eab18762b 100644 --- a/web/.eslintignore +++ b/web/.eslintignore @@ -4,3 +4,4 @@ vendor templates/ templates\ ycache +regression/htmlcov diff --git a/web/config.py b/web/config.py index d797e26f7..faeb459fd 100644 --- a/web/config.py +++ b/web/config.py @@ -724,6 +724,29 @@ OAUTH2_CONFIG = [ OAUTH2_AUTO_CREATE_USER = True +########################################################################## +# Two-factor Authentication Configuration +########################################################################## + +# Set it to True, to enable the two-factor authentication +MFA_ENABLED = False + +# Set it to True, to ask the users to register forcefully for the +# two-authentication methods on logged-in. +MFA_FORCE_REGISTRATION = False + +# pgAdmin supports Two-factor authentication by either sending an one-time code +# to an email, or using the TOTP based application like Google Authenticator. +SUPPORTED_MFA_LIST = ["email", "authenticator"] + +# NOTE: Please set the 'Mail server settings' to use 'email' as two-factor +# authentication method. + +# Subject for the email verification code +# Default: - Verification Code +# e.g. pgAdmin 4 - Verification Code +MFA_EMAIL_SUBJECT = None + ########################################################################## # PSQL tool settings ########################################################################## diff --git a/web/migrations/script.py.mako b/web/migrations/script.py.mako index 8cda71380..340757c99 100644 --- a/web/migrations/script.py.mako +++ b/web/migrations/script.py.mako @@ -14,8 +14,7 @@ Revises: ${down_revision | comma,n} Create Date: ${create_date} """ -from alembic import op -import sqlalchemy as sa +from pgadmin.model import db ${imports if imports else ""} # revision identifiers, used by Alembic. diff --git a/web/migrations/versions/15c88f765bc8_.py b/web/migrations/versions/15c88f765bc8_.py new file mode 100644 index 000000000..9775cd63d --- /dev/null +++ b/web/migrations/versions/15c88f765bc8_.py @@ -0,0 +1,44 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""Update DB to version 14 + +Added a table `user_mfa` for saving the options on MFA for different sources. + +Revision ID: 15c88f765bc8 +Revises: 6650c52670c2 +Create Date: 2021-06-22 17:33:12.533825 + +""" +from pgadmin.model import db + + +# revision identifiers, used by Alembic. +revision = '15c88f765bc8' +down_revision = '6650c52670c2' +branch_labels = None +depends_on = None + + +def upgrade(): + db.engine.execute(""" +CREATE TABLE user_mfa( + user_id INTEGER NOT NULL, + mfa_auth VARCHAR(256) NOT NULL, + options TEXT, + PRIMARY KEY (user_id, mfa_auth), + FOREIGN KEY(user_id) REFERENCES user (id) +) + """) + # ### end Alembic commands ### + + +def downgrade(): + # pgAdmin only upgrades, downgrade not implemented. + pass diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 37eb26ccc..bd7f07ece 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -34,6 +34,7 @@ from werkzeug.datastructures import ImmutableDict from werkzeug.local import LocalProxy from werkzeug.utils import find_modules +from pgadmin.authenticate.mfa.utils import is_mfa_enabled from pgadmin.model import db, Role, Server, SharedServer, ServerGroup, \ User, Keys, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION from pgadmin.utils import PgAdminModule, driver, KeyManager diff --git a/web/pgadmin/authenticate/__init__.py b/web/pgadmin/authenticate/__init__.py index 82c5ea9b0..a74aa094c 100644 --- a/web/pgadmin/authenticate/__init__.py +++ b/web/pgadmin/authenticate/__init__.py @@ -19,7 +19,7 @@ from flask_security.views import _security from flask_security.utils import get_post_logout_redirect, \ get_post_login_redirect, logout_user -from pgadmin import db, User +from pgadmin.model import db, User from pgadmin.utils import PgAdminModule from pgadmin.utils.constants import KERBEROS, INTERNAL, OAUTH2, LDAP from pgadmin.authenticate.registry import AuthSourceRegistry @@ -27,6 +27,20 @@ from pgadmin.authenticate.registry import AuthSourceRegistry MODULE_NAME = 'authenticate' auth_obj = None +_URL_WITH_NEXT_PARAM = "{0}?next={1}" + + +def get_logout_url(): + BROWSER_INDEX = 'browser.index' + if config.SERVER_MODE and\ + session['auth_source_manager']['current_source'] == \ + KERBEROS: + return _URL_WITH_NEXT_PARAM.format(url_for( + 'authenticate.kerberos_logout'), url_for(BROWSER_INDEX)) + + return _URL_WITH_NEXT_PARAM.format( + url_for('security.logout'), url_for(BROWSER_INDEX)) + class AuthenticateModule(PgAdminModule): def get_exposed_url_endpoints(self): diff --git a/web/pgadmin/authenticate/mfa/__init__.py b/web/pgadmin/authenticate/mfa/__init__.py new file mode 100644 index 000000000..2eeba08ab --- /dev/null +++ b/web/pgadmin/authenticate/mfa/__init__.py @@ -0,0 +1,85 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +"""Multi-factor Authentication (MFA) implementation""" + +from flask import Blueprint, session +from flask_babelex import gettext as _ + +import config +from .utils import mfa_enabled, segregate_valid_and_invalid_mfa_list + +from .registry import MultiFactorAuthRegistry +from .views import validate_view, registration_view + + +def __create_blueprint(): + blueprint = Blueprint( + "mfa", __name__, url_prefix="/mfa", + static_folder="static", + template_folder="templates" + ) + + blueprint.add_url_rule( + "/validate", "validate", validate_view, methods=("GET", "POST",) + ) + + blueprint.add_url_rule( + "/register", "register", registration_view, methods=("GET", "POST",) + ) + + return blueprint + + +def init_app(app): + + if getattr(config, "MFA_ENABLED", False) is False: + return + + MultiFactorAuthRegistry.load_modules(app) + + def remove_invalid_mfa_auth_methods(): + + supported_methods = getattr(config, "SUPPORTED_MFA_LIST", []) + invalid_auth_methods = [] + + supported_methods, invalid_auth_methods = \ + segregate_valid_and_invalid_mfa_list(supported_methods) + + for auth_method in invalid_auth_methods: + app.logger.warning(_( + "'{}' is not a valid multi-factor authentication method" + ).format(auth_method)) + + config.SUPPORTED_MFA_LIST = supported_methods + blueprint = __create_blueprint() + + for mfa_method in supported_methods: + mfa = MultiFactorAuthRegistry.get(mfa_method) + mfa.register_url_endpoints(blueprint) + + app.register_blueprint(blueprint) + app.register_logout_hook(blueprint) + + from flask_login import user_logged_in, user_logged_out + + @user_logged_in.connect_via(app) + @user_logged_out.connect_via(app) + def clear_session_on_login(sender, user): + session['mfa_authenticated'] = False + + def disable_mfa(): + if getattr(config, 'MFA_ENABLED', False) is True and \ + getattr(config, 'SERVER_MODE', False) is not False: + app.logger.warning(_( + "No valid multi-factor authentication found, hence - " + "disabling it." + )) + config.MFA_ENABLED = False + + mfa_enabled(remove_invalid_mfa_auth_methods, disable_mfa) diff --git a/web/pgadmin/authenticate/mfa/authenticator.py b/web/pgadmin/authenticate/mfa/authenticator.py new file mode 100644 index 000000000..01bdcbaf1 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/authenticator.py @@ -0,0 +1,145 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +"""Multi-factor Authentication implementation for Google Authenticator""" + +import base64 +from io import BytesIO + +from flask import url_for, session, flash +from flask_babelex import gettext as _ +from flask_login import current_user +import pyotp +import qrcode + +import config +from pgadmin.model import UserMFA + +from .registry import BaseMFAuth +from .utils import ValidationException, fetch_auth_option, mfa_add + + +_GOOGLE_AUTH_METHOD = "authenticator" +_GOOGLE_AUTHENTICATOR = _("Google Authenticator") + + +class GoogleAuthenticator(BaseMFAuth): + + @classmethod + def __fetch_user_topt(cls): + options, found = fetch_auth_option(GOOGLE_AUTH_METHOD) + + if found is False: + raise ValidationException(_( + "User has not registered the Google Authenticator for " + "authentication." + )) + + if options is None or options == '': + raise ValidationException(_( + "User does not have valid HASH to generate the OTP." + )) + + return pyotp.TOTP(options) + + @property + def name(self): + return GOOGLE_AUTH_METHOD + + @property + def label(self): + return _(_GOOGLE_AUTHENTICATOR) + + def validate(self, **kwargs): + code = kwargs.get('code', None) + totp = GoogleAuthenticator.__fetch_user_topt() + + if totp.verify(code) is False: + raise ValidationException("Invalid Code") + + def validation_view(self): + return ( + "
" + + _(_GOOGLE_AUTHENTICATOR) + + "
" + + "
" + + _("Enter your Google Authenticator passcode") + + "
" + + "
" + ) + + def _registration_view(self): + + option = session.pop('mfa_authenticator_opt', None) + if option is None: + option = pyotp.random_base32() + session['mfa_authenticator_opt'] = option + totp = pyotp.TOTP(option) + + uri = totp.provisioning_uri( + current_user.username, issuer_name=getattr( + config, "APP_NAME", "pgAdmin 4" + ) + ) + + img = qrcode.make(uri) + buffered = BytesIO() + img.save(buffered, format="JPEG") + img_base64 = base64.b64encode(buffered.getvalue()) + + return ( + "
" + + _(_GOOGLE_AUTHENTICATOR) + + "
" + + "" + + "" + + "" + _("Google Authenticator QRCode") +
+            "
" + + _( + "Scan the QR code and the enter the code from the " + "Google Authenticator" + ) + + "
" + + "
" + + "
" + ) + + def registration_view(self, form_data): + + if 'VALIDATE' not in form_data: + return self._registration_view() + + code = form_data.get('code', None) + totp = pyotp.TOTP(session['mfa_authenticator_opt']) + + if totp.verify(code) is False: + flash(_("Failed to validate the code"), "danger") + return self._registration_view() + + mfa_add(GOOGLE_AUTH_METHOD, session['mfa_authenticator_opt']) + flash(_( + "Google Authenticator registered successfully for authentication." + ), "success") + session.pop('mfa_authenticator_opt', None) + + return None + + @property + def icon(self): + # Reference: + # https://upload.wikimedia.org/wikipedia/commons/6/6e/ \ + # Google_Authenticator_for_Android_icon.svg + # Changed: height -> 30px, width -> 30px + return url_for("mfa.static", filename="ga.svg") diff --git a/web/pgadmin/authenticate/mfa/email.py b/web/pgadmin/authenticate/mfa/email.py new file mode 100644 index 000000000..aeb30598f --- /dev/null +++ b/web/pgadmin/authenticate/mfa/email.py @@ -0,0 +1,248 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +"""Multi-factor Authentication implementation by sending OTP through email""" + +from flask import url_for, session, Response, render_template, current_app, \ + flash +from flask_babelex import gettext as _ +from flask_login import current_user +from flask_security import send_mail + +import config +from pgadmin.utils.csrf import pgCSRFProtect +from .registry import BaseMFAuth +from .utils import ValidationException, mfa_add, fetch_auth_option + + +def __generate_code(): + import time + import base64 + import codecs + import random + + code = codecs.encode("{}{}{}".format( + time.time(), current_user.username, random.randint(1000, 9999) + ).encode(), "hex") + + res = 0 + idx = 0 + + while idx < len(code): + res += int((code[idx:idx + 6]).decode('utf-8'), base=16) + res %= 1000000 + idx += 5 + + return str(res).zfill(6) + + +def _send_code_to_email(_email=None): + + if not current_user.is_authenticated: + return False, 401, _("Not accessible") + + if _email is None: + _email = getattr(current_user, 'email', None) + + if _email is None: + return False, 401, _("No email-id unavailable.") + + try: + session["mfa_email_code"] = __generate_code() + subject = getattr(config, 'MFA_EMAIL_SUBJECT', None) + + if subject is None: + subject = _("{} - Verification Code").format(config.APP_NAME) + + send_mail( + subject, + _email, + "send_email_otp", + user=current_user, + code=session["mfa_email_code"] + ) + except OSError as ose: + current_app.logger.exception(ose) + return False, 503, _("Failed to send the code to email.") + \ + "\n" + str(ose) + + message = _( + "A verification code was sent to {}. Check your email and enter " + "the code." + ).format(_mask_email(_email)) + + return True, 200, message + + +def _mask_email(_email): + import re + email_split = re.split('@', _email) + username, domain = email_split + domain_front, *domain_back_list = re.split('[.]', domain) + users = re.split('[.]', username) + + def _mask_except_first_char(_str): + return _str[0] + '*' * (len(_str) - 1) + + return '.'.join([_mask_except_first_char(user) for user in users]) + \ + '@' + _mask_except_first_char(domain_front) + '.' + \ + '.'.join(domain_back_list) + + +def send_email_code(): + + options, found = fetch_auth_option(EMAIL_AUTH_METHOD) + + if found is False: + raise ValidationException(_( + "User has not registered for email authentication" + )) + + success, http_code, message = _send_code_to_email(options) + + if success is False: + return Response(message, http_code, mimetype='text/html') + + return Response(render_template( + "mfa/email_code_sent.html", _=_, + message=message, + ), http_code, mimetype='text/html') + + +@pgCSRFProtect.exempt +def javascript(): + if not current_user.is_authenticated: + return Response(_("Not accessible"), 401, mimetype="text/plain") + + return Response(render_template( + "mfa/email.js", _=_, url_for=url_for, + ), 200, mimetype="text/javascript") + + +EMAIL_AUTH_METHOD = 'email' + + +class EmailAuthenticator(BaseMFAuth): + + @property + def name(self): + return EMAIL_AUTH_METHOD + + @property + def label(self): + return _("Email Authentication") + + def validate(self, **kwargs): + code = kwargs.get('code', None) + if code == session.get("mfa_email_code", 'unknown'): + session.pop("mfa_email_code") + return + raise ValidationException("Invalid code") + + def validation_view(self): + if session.get("code_sent", False) is True: + return render_template( + "mfa/email_view.html", _=_ + ) + return render_template( + "mfa/email_view.html", _=_ + ) + + def _registration_view(self): + email = getattr(current_user, 'email', '') + return "\n".join([ + "
", + _("Email Authenticator"), + "
", + "", + "", + "
", + _("Enter the email-id for the user."), + "
", + "
", + "", + "
", + ]) + + def _registration_view_after_code_sent(self, _form_data): + + session['mfa_email_id'] = _form_data.get('send_to', None) + success, http_code, message = _send_code_to_email( + session['mfa_email_id'] + ) + + if success is False: + flash(message, 'danger') + return None + + return ( + "
" + + _("Email Authenticator") + + "
" + + "" + + "" + + "
" + message + + "
" + + "
" + ) + + def registration_view(self, _form_data): + + if 'validate' in _form_data: + if _form_data['validate'] == 'send_code': + return self._registration_view_after_code_sent(_form_data) + + code = _form_data.get('code', None) + + if code == session.get("mfa_email_code", None): + mfa_add(EMAIL_AUTH_METHOD, session['mfa_email_id']) + + flash(_( + "Email Authenticator registered successfully for " + "authentication." + ), "success") + + session.pop('mfa_email_code', None) + + return None + + flash(_('Invalid code'), 'danger') + + return self._registration_view() + + def register_url_endpoints(self, blueprint): + blueprint.add_url_rule( + "/send_email_code", "send_email_code", send_email_code, + methods=("POST", ) + ) + blueprint.add_url_rule( + "/email.js", "email_js", javascript, methods=("GET", ) + ) + + @property + def icon(self): + # Reference: https://commons.wikimedia.org/wiki/File:At_(@)_symbol.svg + # Changed: height -> 30px, width -> 30px + return url_for("mfa.static", filename="at.svg") + + @property + def validate_script(self): + return url_for("mfa.email_js") diff --git a/web/pgadmin/authenticate/mfa/registry.py b/web/pgadmin/authenticate/mfa/registry.py new file mode 100644 index 000000000..037efe11d --- /dev/null +++ b/web/pgadmin/authenticate/mfa/registry.py @@ -0,0 +1,80 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""External 2FA Authentication Registry.""" +from abc import abstractmethod, abstractproperty +import six + +from pgadmin.utils.dynamic_registry import create_registry_metaclass + + +MultiFactorAuthRegistry = create_registry_metaclass( + 'MultiFactorAuthRegistry', __package__, decorate_as_module=True +) + + +@six.add_metaclass(MultiFactorAuthRegistry) +class BaseMFAuth(): + + @abstractproperty + def name(self): + """Name must not contain special characters""" + pass + + @abstractproperty + def label(self): + pass + + @property + def icon(self): + return "" + + @property + def validate_script(self): + return None + + @abstractmethod + def validate(self, **kwargs): + pass + + @abstractmethod + def validation_view(self): + pass + + @abstractmethod + def registration_view(self, form_data): + pass + + def register_url_endpoints(self, blueprint): + """ + Override this method in case, you would like to expose some url + end-points for this auth method. + """ + pass + + def to_dict(self): + return { + "id": self.name, + "label": self.label, + "icon": self.icon, + } + + def validation_view_dict(self, selected_mfa): + res = self.to_dict() + + res['view'] = self.validation_view() + res['selected'] = selected_mfa == self.name + res['script'] = self.validate_script + + return res + + def deleted(self): + # Override this method in case, you need to do any action to do on + # authentication deletion. + pass diff --git a/web/pgadmin/authenticate/mfa/static/at.svg b/web/pgadmin/authenticate/mfa/static/at.svg new file mode 100644 index 000000000..9ef836880 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/static/at.svg @@ -0,0 +1,459 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + at + Novembe 2008 + + + Beate Kaspa + + + + + Beate Kaspa + + + + + RRZE + + + + + at + @ + symbol + icon + + + + + Hendik Egges + + + deived fom <http://webcvs.feedesktop.og/tango/tango-icon-theme/scalable/actions/addess-book-new.svg> + + + + + + + + + + + + + + + + + + + + diff --git a/web/pgadmin/authenticate/mfa/static/ga.svg b/web/pgadmin/authenticate/mfa/static/ga.svg new file mode 100644 index 000000000..859bf7dfc --- /dev/null +++ b/web/pgadmin/authenticate/mfa/static/ga.svg @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/web/pgadmin/authenticate/mfa/templates/mfa/email.js b/web/pgadmin/authenticate/mfa/templates/mfa/email.js new file mode 100644 index 000000000..f29a78b66 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/mfa/email.js @@ -0,0 +1,54 @@ +var mfa_form_elem = document.getElementById('mfa_form'); + +if (mfa_form_elem) + mfa_form_elem.setAttribute('class', ''); + +function sendCodeToEmail(data, _json, _callback) { + const URL = '{{ url_for('mfa.send_email_code') }}'; + let accept = 'text/html; charset=utf-8;'; + + document.getElementById('btn_send_code').disabled = true; + + if (!data) { + data = {'code': ''}; + } + + if (_json) { + accept = 'application/json; charset=utf-8;'; + } + + clear_error(); + + fetch(URL, { + method: 'POST', + mode: 'cors', + cache: 'no-cache', + headers: { + 'Accept': accept, + 'Content-Type': 'application/json; charset=utf-8;', + '{{ current_app.config.get('WTF_CSRF_HEADERS')[0] }}': '{{ csrf_token() }}' + }, + redirect: 'follow', + body: JSON.stringify(data) + }).then((resp) => { + if (_callback) { + setTimeout(() => (_callback(resp)), 1); + return null; + } + if (!resp.ok) { + document.getElementById('btn_send_code').disabled = false; + resp.text().then(msg => render_error(msg)); + return; + } + if (_json) return resp.json(); + return resp.text(); + }).then((string) => { + if (!string) + return; + document.getElementById("mfa_email_auth").innerHTML = string; + document.getElementById("mfa_form").classList = ["show_validate_btn"]; + setTimeout(() => { + document.getElementById("showme").classList = []; + }, 20000); + }); +} diff --git a/web/pgadmin/authenticate/mfa/templates/mfa/email_code_sent.html b/web/pgadmin/authenticate/mfa/templates/mfa/email_code_sent.html new file mode 100644 index 000000000..35810906a --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/mfa/email_code_sent.html @@ -0,0 +1,19 @@ +
+
{{ message }}
+ +
+ +
+
diff --git a/web/pgadmin/authenticate/mfa/templates/mfa/email_view.html b/web/pgadmin/authenticate/mfa/templates/mfa/email_view.html new file mode 100644 index 000000000..3ecf635fb --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/mfa/email_view.html @@ -0,0 +1,7 @@ +
+
{{ _("Verify with Email Authentication") }}
+
+ +
+
diff --git a/web/pgadmin/authenticate/mfa/templates/mfa/register.html b/web/pgadmin/authenticate/mfa/templates/mfa/register.html new file mode 100644 index 000000000..63fd288a0 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/mfa/register.html @@ -0,0 +1,77 @@ +{% if next_url == 'internal' %} +{% set auth_page = true %} +{% endif %} +{% extends "security/panel.html" %} +{% block panel_image %} +
+ {{ _('Registration') }} +
+{% endblock %} +{% block panel_title %}{{ _('Authentication registration') }}{% endblock %} +{% block panel_body %} + + +
+
+{% if mfa_view is not defined or mfa_view is none %} + {% for mfa in mfa_list %} +
+ +
+ {% endfor %} +
+ {% if next_url != 'internal' %} + + {% endif %} +{% else %} + {{ mfa_view | safe }} + +
+ + +
+{% endif %} + +
+{% else %} +
+
+
+
+ +
+ {{ error_message }}
+
+
+
+
+{% endif %} +{% endblock %} diff --git a/web/pgadmin/authenticate/mfa/templates/mfa/validate.html b/web/pgadmin/authenticate/mfa/templates/mfa/validate.html new file mode 100644 index 000000000..249b4a03a --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/mfa/validate.html @@ -0,0 +1,119 @@ +{% extends "security/panel.html" %} +{% block panel_image %} +
+ {{ _('Authentication') }} +
+{% endblock %} +{% block panel_title %}{{ _('Authentication') }}{% endblock %} +{% block panel_body %} + + +
+
+
+
+
+ +
+ +
+
+
+ + + +{% endblock %} diff --git a/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.html b/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.html new file mode 100644 index 000000000..f9bf492d2 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.html @@ -0,0 +1,2 @@ +Please use the following code for authentication. +{{ code }} diff --git a/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.txt b/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.txt new file mode 100644 index 000000000..f9bf492d2 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/templates/security/email/send_email_otp.txt @@ -0,0 +1,2 @@ +Please use the following code for authentication. +{{ code }} diff --git a/web/pgadmin/authenticate/mfa/tests/test_config.py b/web/pgadmin/authenticate/mfa/tests/test_config.py new file mode 100644 index 000000000..f8ca5bad5 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/tests/test_config.py @@ -0,0 +1,153 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +from pgadmin.authenticate.mfa import mfa_enabled +import config + + +__MFA_ENABLED = 'MFA Enabled' +__MFA_DISABLED = 'MFA Disabled' + + +def __mfa_is_enabled(): + return __MFA_ENABLED + + +def __mfa_is_disabled(): + return __MFA_DISABLED + + +def check_mfa_enabled(test): + config.MFA_ENABLED = test.enabled + config.SUPPORTED_MFA_LIST = test.supported_list + + if mfa_enabled(__mfa_is_enabled, __mfa_is_disabled) != test.expected: + test.fail(test.fail_msg) + + +def log_message_in_init_app(test): + import types + from unittest.mock import patch + from .. import init_app + from .utils import test_create_dummy_app + + auth_method_msg = "'xyz' is not a valid multi-factor authentication method" + disabled_msg = \ + "No valid multi-factor authentication found, hence - disabling it." + warning_invalid_auth_found = False + warning_disable_auth = False + + dummy_app = test_create_dummy_app(test.name) + + def _log_warning_msg(_msg): + nonlocal warning_invalid_auth_found + nonlocal warning_disable_auth + + if auth_method_msg == _msg: + warning_invalid_auth_found = True + return + + if _msg == disabled_msg: + warning_disable_auth = True + + with patch.object( + dummy_app.logger, + 'warning', + new=_log_warning_msg + ): + config.MFA_ENABLED = True + config.SUPPORTED_MFA_LIST = test.supported_list + init_app(dummy_app) + + if warning_invalid_auth_found is not test.warning_invalid_auth_found \ + or warning_disable_auth is not test.warning_disable_auth: + test.fail(test.fail_msg) + test.fail() + + +config_scenarios = [ + ( + "Check MFA enabled with no authenticators?", + dict( + check=check_mfa_enabled, enabled=True, supported_list=list(), + expected=__MFA_DISABLED, + fail_msg="MFA is enabled with no authenticators, but - " + "'execute_if_disabled' function is not called." + ), + ), + ( + "Check MFA enabled?", + dict( + check=check_mfa_enabled, enabled=True, + supported_list=["tests.utils"], expected=__MFA_ENABLED, + fail_msg="MFA is enable, but - 'execute_if_enabled' function " + "is not called." + ), + ), + ( + "Check MFA disabled check functionality works?", + dict( + check=check_mfa_enabled, enabled=False, + supported_list=list(), + expected=__MFA_DISABLED, + fail_msg="MFA is disabled, but - 'execute_if_enabled' function " + "is called." + ), + ), + ( + "Check MFA in the supported MFA LIST is part of the registered one", + dict( + check=check_mfa_enabled, enabled=True, + supported_list=["not-in-list"], + expected=__MFA_DISABLED, + fail_msg="MFA is enabled with invalid authenticators, but - " + "'execute_if_enabled' function is called" + ), + ), + ( + "Check warning message with invalid method appended during " + "init_app(...)", + dict( + check=log_message_in_init_app, + supported_list=["xyz", "tests.utils"], + name="warning_app_having_invalid_method", + warning_invalid_auth_found=True, warning_disable_auth=False, + fail_msg="Warning for invalid auth is not found", + ), + ), + ( + "Check warning message with invalid method during " + "init_app(...) ", + dict( + check=log_message_in_init_app, supported_list=["xyz"], + name="warning_app_with_invalid_method", + warning_invalid_auth_found=False, warning_disable_auth=True, + fail_msg="Warning for invalid auth is not found", + ), + ), + ( + "Check warning message when empty supported mfa list during " + "init_app(...)", + dict( + check=log_message_in_init_app, supported_list=[""], + name="warning_app_with_empty_supported_list", + warning_invalid_auth_found=False, warning_disable_auth=True, + fail_msg="Warning not found with empty supported mfa methods", + ), + ), + ( + "No warning message should found with valid configurations during " + "init_app(...)", + dict( + check=log_message_in_init_app, name="no_warning_app", + supported_list=["tests.utils"], + warning_invalid_auth_found=False, warning_disable_auth=False, + fail_msg="Warning found with valid configure", + ), + ), +] diff --git a/web/pgadmin/authenticate/mfa/tests/test_mfa.py b/web/pgadmin/authenticate/mfa/tests/test_mfa.py new file mode 100644 index 000000000..f63aea08e --- /dev/null +++ b/web/pgadmin/authenticate/mfa/tests/test_mfa.py @@ -0,0 +1,56 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +from pgadmin.utils.route import BaseTestGenerator +import config +from .test_config import config_scenarios +from .test_user_execution import user_execution_scenarios +from .test_mfa_view import validation_view_scenarios +from .utils import init_dummy_auth_class + + +test_scenarios = list() +test_scenarios += config_scenarios +test_scenarios += user_execution_scenarios +test_scenarios += validation_view_scenarios + + +class TestMFATests(BaseTestGenerator): + + scenarios = test_scenarios + + @classmethod + def setUpClass(cls): + config.MFA_ENABLED = True + init_dummy_auth_class() + + @classmethod + def tearDownClass(cls): + config.MFA_ENABLED = False + config.SUPPORTED_MFA_LIST = [] + + def setUp(self): + config.SUPPORTED_MFA_LIST = ['tests.utils'] + + start = getattr(self, 'start', None) + if start is not None: + start(self) + + super(BaseTestGenerator, self).setUp() + + def tearDown(self): + + finish = getattr(self, 'finish', None) + if finish is not None: + finish(self) + + config.SUPPORTED_MFA_LIST = [] + super(BaseTestGenerator, self).tearDown() + + def runTest(self): + self.check(self) diff --git a/web/pgadmin/authenticate/mfa/tests/test_mfa_view.py b/web/pgadmin/authenticate/mfa/tests/test_mfa_view.py new file mode 100644 index 000000000..4e3930739 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/tests/test_mfa_view.py @@ -0,0 +1,65 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +from unittest.mock import patch +import config + +from .utils import setup_mfa_app, MockCurrentUserId, MockUserMFA + + +__MFA_PACKAGE = '.'.join((__package__.split('.'))[:-1]) +__AUTH_PACKAGE = '.'.join((__package__.split('.'))[:-2]) + + +def check_validation_view_content(test): + user_mfa_test_data = [ + MockUserMFA(1, "dummy", ""), + MockUserMFA(1, "no-present-in-list", None), + ] + + def MockLogException(ex): + test.assertTrue(type(ex) == ValidationException) + + with patch( + __MFA_PACKAGE + ".utils.current_user", return_value=MockCurrentUserId() + ): + with patch(__MFA_PACKAGE + ".utils.UserMFA") as mock_user_mfa: + with test.app.test_request_context(): + with patch("flask.current_app") as mock_current_app: + mock_user_mfa.query.filter_by.return_value \ + .all.return_value = user_mfa_test_data + mock_current_app.logger.exception = MockLogException + + with patch(__AUTH_PACKAGE + ".session") as mock_session: + session = { + 'auth_source_manager': { + 'current_source': getattr( + test, 'auth_method', 'internal' + ) + } + } + + mock_session.__getitem__.side_effect = \ + session.__getitem__ + + response = test.tester.get("/mfa/validate") + + test.assertEquals(response.status_code, 200) + test.assertEquals( + response.headers["Content-Type"], "text/html; charset=utf-8" + ) + # test.assertTrue('Dummy' in response.data.decode('utf8')) + # End of test case - check_validation_view_content + + +validation_view_scenarios = [ + ( + "Validation view of a MFA method should return a HTML tags", + dict(start=setup_mfa_app, check=check_validation_view_content), + ), +] diff --git a/web/pgadmin/authenticate/mfa/tests/test_user_execution.py b/web/pgadmin/authenticate/mfa/tests/test_user_execution.py new file mode 100644 index 000000000..9502250c8 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/tests/test_user_execution.py @@ -0,0 +1,125 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +from unittest.mock import patch +import config +from pgadmin.authenticate.mfa.utils import \ + mfa_user_force_registration_required +from pgadmin.authenticate.mfa.utils import mfa_user_registered, \ + user_supported_mfa_methods +from .utils import MockUserMFA, MockCurrentUserId + + +__MFA_PACKAGE = '.'.join((__package__.split('.'))[:-1]) + + +def __return_true(): + return True + + +def __return_false(): + return False + + +def check_user_registered(test): + + user_mfa_test_data = [ + MockUserMFA(1, "dummy", "Hello guys"), + MockUserMFA(1, "no-present-in-list", None), + ] + + with patch( + __MFA_PACKAGE + ".utils.current_user", return_value=MockCurrentUserId() + ): + with patch(__MFA_PACKAGE + ".utils.UserMFA") as mock_user_mfa: + mock_user_mfa.query.filter_by.return_value.all.return_value = \ + user_mfa_test_data + + ret = mfa_user_registered(__return_true, __return_false) + + if ret is None: + test.fail( + "User registration check has not called either " + "'is_registered' or 'is_not_registered' function" + ) + + if ret is False: + test.fail( + "Not expected to be called 'is_not_registered' function " + "as 'dummy' is in the supported MFA methods" + ) + + methods = user_supported_mfa_methods() + if "dummy" not in methods: + test.fail( + "User registration methods are not valid: {}".format( + methods + ) + ) + + # Removed the 'dummy' from the user's registered MFA list + user_mfa_test_data.pop(0) + ret = mfa_user_registered(__return_true, __return_false) + + if ret is None: + test.fail( + "User registration check has not called either " + "'is_registered' or 'is_not_registered' function" + ) + + if ret is True: + test.fail( + "Not expected to be called 'is_registered' function as " + "'not-present-in-list' is not a valid multi-factor " + "authentication method" + ) + + # End of test case - check_user_registered + + +def check_force_registration_required(test): + + if mfa_user_force_registration_required( + __return_false, __return_true + ) is None: + test.fail( + "User registration check did not call either register or " + "do_not_register function" + ) + + config.MFA_FORCE_USE_REGISTRATION = False + if mfa_user_force_registration_required( + __return_true, __return_false + ) is True: + test.fail( + "User registration function should not be called, when " + "config.MFA_FORCE_USE_REGISTRATION is True" + ) + + config.MFA_FORCE_USE_REGISTRATION = True + if mfa_user_force_registration_required( + __return_true, __return_false + ) is False: + test.fail( + "'do_not_registration' function should not be called, when " + "config.MFA_FORCE_USE_REGISTRATION is True" + ) + + # End of test case - check_force_registration_required + + +user_execution_scenarios = [ + ( + "Check user is registered to do MFA", + dict(check=check_user_registered), + ), + ( + "Require the forcefull registration for MFA?", + dict(check=check_force_registration_required), + ), +] diff --git a/web/pgadmin/authenticate/mfa/tests/utils.py b/web/pgadmin/authenticate/mfa/tests/utils.py new file mode 100644 index 000000000..e1b1894eb --- /dev/null +++ b/web/pgadmin/authenticate/mfa/tests/utils.py @@ -0,0 +1,101 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +import types + +from flask import Flask, Response +import config + +from pgadmin.authenticate.mfa import init_app as mfa_init_app + + +def init_dummy_auth_class(): + from pgadmin.authenticate.mfa.registry import BaseMFAuth + + class DummyAuth(BaseMFAuth): + + @property + def name(self): + return "dummy" + + @property + def label(self): + return "Dummy" + + def validate(self, **kwargs): + return true + + def validation_view(self): + return "View" + + def registration_view(self): + return "Registration" + + def register_url_endpoints(self, blueprint): + pass + + +def test_create_dummy_app(name=__name__): + import os + import pgadmin + from pgadmin.misc.themes import themes + + def index(): + return Response("logged in") + + template_folder = os.path.join( + os.path.dirname(os.path.realpath(pgadmin.__file__)), 'templates' + ) + app = Flask(name, template_folder=template_folder) + config.MFA_ENABLED = True + config.SUPPORTED_MFA_LIST = ['tests.utils'] + app.config.from_object(config) + app.config.update(dict(LOGIN_DISABLED=True)) + app.add_url_rule("/", "index", index, methods=("GET",)) + app.add_url_rule( + "/favicon.ico", "redirects.favicon", index, methods=("GET",) + ) + app.add_url_rule("/browser", "browser.index", index, methods=("GET",)) + app.add_url_rule("/tools", "tools.index", index, methods=("GET",)) + app.add_url_rule( + "/users", "user_management.index", index, methods=("GET",) + ) + app.add_url_rule( + "/login", "security.logout", index, methods=("GET",) + ) + app.add_url_rule( + "/kerberos_logout", "authenticate.kerberos_logout", index, + methods=("GET",) + ) + + def __dummy_logout_hook(self, blueprint): + pass + + app.register_logout_hook = types.MethodType(__dummy_logout_hook, app) + + themes(app) + + return app + + +def setup_mfa_app(test): + test.app = test_create_dummy_app() + mfa_init_app(test.app) + test.tester = test.app.test_client() + + +class MockUserMFA(): + """Mock user for UserMFA""" + def __init__(self, user_id, mfa_auth, options): + self.user_id = user_id + self.mfa_auth = mfa_auth + self.options = options + + +class MockCurrentUserId(): + id = 1 diff --git a/web/pgadmin/authenticate/mfa/utils.py b/web/pgadmin/authenticate/mfa/utils.py new file mode 100644 index 000000000..983f1dc4d --- /dev/null +++ b/web/pgadmin/authenticate/mfa/utils.py @@ -0,0 +1,217 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +"""Multi-factor Authentication (MFA) utility functions""" + +from functools import wraps + +from flask import url_for, session, request, redirect +from flask_login.utils import login_url +from flask_security import current_user + +import config +from pgadmin.model import UserMFA, db +from .registry import MultiFactorAuthRegistry + + +class ValidationException(Exception): + pass + + +def segregate_valid_and_invalid_mfa_list(current): + + invalid_auth_methods = [] + valid_auth_methods = [] + + for mfa in current: + # Put invalid MFA method in separate list + if mfa not in MultiFactorAuthRegistry._registry: + if mfa not in invalid_auth_methods: + invalid_auth_methods.append(mfa) + continue + # Remove duplicate entries + if mfa in valid_auth_methods: + continue + + valid_auth_methods.append(mfa) + + return valid_auth_methods, invalid_auth_methods + + +def mfa_enabled(execute_if_enabled, execute_if_disabled): + + is_server_mode = getattr(config, 'SERVER_MODE', False) + enabled = getattr(config, "MFA_ENABLED", False) + supported_list = getattr(config, "SUPPORTED_MFA_LIST", []) + + if is_server_mode is True and enabled is True and \ + type(supported_list) == list: + supported_list, _ = segregate_valid_and_invalid_mfa_list( + supported_list + ) + + if len(supported_list) > 0: + return execute_if_enabled() + + return execute_if_disabled() + + +def mfa_user_force_registration_required(register, do_not_register): + return register() \ + if getattr(config, "MFA_FORCE_REGISTRATION", False) is True else \ + do_not_register() + + +def mfa_suppored_methods(): + supported_mfa_auth_methods = dict() + + for auth_method in config.SUPPORTED_MFA_LIST: + registry = MultiFactorAuthRegistry.get(auth_method) + supported_mfa_auth_methods[registry.name] = { + "mfa": registry, "registered": False + } + + auths = UserMFA.query.filter_by(user_id=current_user.id).all() + + for auth in auths: + if auth.mfa_auth in supported_mfa_auth_methods: + supported_mfa_auth_methods[auth.mfa_auth]['registered'] = True + + return supported_mfa_auth_methods + + +def user_supported_mfa_methods(return_true_on_found=False): + auths = UserMFA.query.filter_by(user_id=current_user.id).all() + res = dict() + supported_mfa_auth_methods = dict() + + if len(auths) > 0: + for auth_method in config.SUPPORTED_MFA_LIST: + registry = MultiFactorAuthRegistry.get(auth_method) + supported_mfa_auth_methods[registry.name] = registry + + for auth in auths: + if auth.mfa_auth in supported_mfa_auth_methods: + if return_true_on_found is True: + return True + res[auth.mfa_auth] = \ + supported_mfa_auth_methods[auth.mfa_auth] + + return res + + +def mfa_user_registered(is_registered, is_not_registered): + + return is_registered() if user_supported_mfa_methods(True) is True else \ + is_not_registered() + + +def mfa_session_authenticated(authenticated, unauthenticated): + + return authenticated() if session['mfa_authenticated'] is True \ + else unauthenticated() + + +def is_mfa_session_authenticated(): + return session['mfa_authenticated'] is True + + +def mfa_required(func): + + def get_next_url(): + next_url = request.url + registration_url = url_for('mfa.register') + + if next_url.startswith(registration_url): + return url('browser.index') + + return next_url + + def redirect_to_mfa_validate_url(): + return redirect(login_url("mfa.validate", next_url=get_next_url())) + + def redirect_to_mfa_registration(): + return redirect(login_url("mfa.register", next_url=get_next_url())) + + @wraps(func) + def inner(*args, **kwargs): + + def execute_func(): + return func(*args, **kwargs) + + def if_else_func(_func, first, second): + def if_else_func_inner(): + return _func(first, second) + return if_else_func_inner + + return mfa_enabled( + if_else_func( + mfa_session_authenticated, + execute_func, + if_else_func( + mfa_user_registered, + redirect_to_mfa_validate_url, + if_else_func( + mfa_user_force_registration_required, + redirect_to_mfa_registration, + execute_func + ) + ) + ), + execute_func + ) + + return inner + + +def is_mfa_enabled(): + return mfa_enabled(lambda: True, lambda: False) + + +def mfa_delete(auth_name): + auth = UserMFA.query.filter_by( + user_id=current_user.id, mfa_auth=auth_name + ) + + if auth.count() != 0: + auth.delete() + db.session.commit() + + return True + + return False + + +def mfa_add(auth_name, options): + auth = UserMFA.query.filter_by( + user_id=current_user.id, mfa_auth=auth_name + ).first() + + if auth is None: + auth = UserMFA( + user_id=current_user.id, + mfa_auth=auth_name, + options=options + ) + db.session.add(auth) + + # We will override the existing options + auth.options = options + + db.session.commit() + + +def fetch_auth_option(mfa_auth): + auth = UserMFA.query.filter_by( + user_id=current_user.id, mfa_auth=mfa_auth + ).first() + + if auth is None: + return none, False + + return auth.options, True diff --git a/web/pgadmin/authenticate/mfa/views.py b/web/pgadmin/authenticate/mfa/views.py new file mode 100644 index 000000000..b6a0d7383 --- /dev/null +++ b/web/pgadmin/authenticate/mfa/views.py @@ -0,0 +1,219 @@ +############################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2021, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +############################################################################## +"""Multi-factor Authentication (MFA) views""" + +import base64 +from flask import Response, render_template, request, flash, \ + current_app, url_for, redirect, session +from flask_babelex import gettext as _ +from flask_login import current_user, login_required +from flask_login.utils import login_url + +from pgadmin.utils.csrf import pgCSRFProtect +from pgadmin.utils.ajax import bad_request +from .utils import user_supported_mfa_methods, mfa_user_registered, \ + mfa_suppored_methods, ValidationException, mfa_delete, is_mfa_enabled, \ + is_mfa_session_authenticated + + +INDEX_URL = "browser.index" +NO_CACHE_HEADERS = dict({ + "Cache-Control": "no-cache, no-store, must-revalidate, public, max-age=0", + "Pragma": "no-cache", + "Expires": "0", +}) + + +def __handle_mfa_validation_request(mfa_method, user_mfa_auths, form_data): + + if mfa_method is None: + raise ValidationException(_("No authentication method provided.")) + + mfa_auth = user_mfa_auths.get(mfa_method, None) + + if mfa_auth is None: + raise ValidationException(_( + "No user supported authentication method provided" + )) + + mfa_auth.validate(**form_data) + + +@pgCSRFProtect.exempt +@login_required +def validate_view(): + + # Load at runtime to avoid circular dependency + from pgadmin.authenticate import get_logout_url + + next_url = request.args.get("next", None) + + if next_url is None or next_url == url_for('mfa.register') or \ + next_url == url_for('mfa.validate'): + next_url = url_for(INDEX_URL) + + if session.get('mfa_authenticated', False) is True: + return redirect(next_url) + + return_code = 200 + mfa_method = None + user_mfa_auths = user_supported_mfa_methods() + + if request.method == 'POST': + try: + form_data = {key: request.form[key] for key in request.form} + next_url = form_data.pop('next', url_for(INDEX_URL)) + mfa_method = form_data.pop('mfa_method', None) + + __handle_mfa_validation_request( + mfa_method, user_mfa_auths, form_data + ) + + session['mfa_authenticated'] = True + + return redirect(next_url) + + except ValidationException as ve: + current_app.logger.warning(( + "MFA validation failed for the user '{}' with an error: " + "{}" + ).format(current_user.username, str(ve))) + flash(str(ve), "danger") + return_code = 401 + except Exception as ex: + current_app.logger.exception(ex) + flash(str(ex), "danger") + return_code = 500 + + mfa_views = { + key: user_mfa_auths[key].validation_view_dict(mfa_method) + for key in user_mfa_auths + } + + if mfa_method is None and len(mfa_views) > 0: + list(mfa_views.items())[0][1]['selected'] = True + + return Response(render_template( + "mfa/validate.html", _=_, views=mfa_views, base64=base64, + logout_url=get_logout_url() + ), return_code, headers=NO_CACHE_HEADERS, mimetype="text/html") + + +def _mfa_registration_view(supported_mfa, form_data): + mfa = supported_mfa['mfa'] + + if form_data[mfa.name] == 'SETUP': + if supported_mfa['registered'] is True: + flash(_("'{}' is already registerd'").format(mfa.label), "success") + return None + + return mfa.registration_view(form_data) + + if mfa_delete(mfa.name) is True: + flash(_( + "'{}' unregistered from the authentication list." + ).format(mfa.label), "success") + + return None + + flash(_( + "'{}' is not found in the authentication list." + ).format(mfa.label), "warning") + + return None + + +def _generate_auth_view(_auth_list, _request): + + for key in _auth_list: + if key in _request.form: + return _mfa_registration_view( + _auth_list[key], _request.form + ) + + return False + + +def __handle_registration_view_for_post_method(_next_url, _mfa_auths): + + next_url = request.form.get("next", None) + + if next_url is None or next_url == url_for('mfa.validate'): + next_url = url_for(INDEX_URL) + + if request.form.get('cancel', None) is None: + view = _generate_auth_view(_mfa_auths, request) + + if view is False: + if next_url != 'internal': + return None, redirect(next_url), None + flash(_("Please close the dialog."), "info") + + if view is not None: + return None, Response( + render_template( + "mfa/register.html", _=_, + mfa_list=None, mfa_view=view, + next_url=next_url, + error_message=None + ), 200, + headers=NO_CACHE_HEADER + ), None + + # Regenerate the supported MFA list after + # registration/deregistration. + _mfa_auths = mfa_suppored_methods() + + return next_url, None, _mfa_auths + + +@pgCSRFProtect.exempt +@login_required +def registration_view(): + mfa_auths = mfa_suppored_methods() + mfa_list = list() + + next_url = request.args.get("next", None) + + if request.method == 'POST': + next_url, response, mfa_auths = \ + __handle_registration_view_for_post_method(next_url, mfa_auths) + + if response is not None: + return response + + if next_url is None: + next_url = url_for(INDEX_URL) + + error_message = None + found_one_mfa = False + + for key in mfa_auths: + mfa = mfa_auths[key]['mfa'] + mfa = mfa.to_dict() + mfa["registered"] = mfa_auths[key]["registered"] + mfa_list.append(mfa) + found_one_mfa = found_one_mfa or mfa["registered"] + + if request.method == 'GET': + if is_mfa_enabled() is False: + error_message = _( + "Can't access this page, when multi factor authentication is " + "disabled." + ) + elif is_mfa_session_authenticated() is False and \ + found_one_mfa is True: + flash(_("Complete the authentication process first"), "danger") + return redirect(login_url("mfa.validate", next_url=next_url)) + + return Response(render_template( + "mfa/register.html", _=_, + mfa_list=mfa_list, mfa_view=None, next_url=next_url, + error_message=error_message + ), 200 if error_message is None else 401, headers=NO_CACHE_HEADER) diff --git a/web/pgadmin/browser/__init__.py b/web/pgadmin/browser/__init__.py index 7fe8fed80..2ea19d48e 100644 --- a/web/pgadmin/browser/__init__.py +++ b/web/pgadmin/browser/__init__.py @@ -25,6 +25,7 @@ from flask import current_app, render_template, url_for, make_response, \ from flask_babelex import gettext from flask_gravatar import Gravatar from flask_login import current_user, login_required +from flask_login.utils import login_url from flask_security.changeable import change_user_password from flask_security.decorators import anonymous_user_required from flask_security.recoverable import reset_password_token_status, \ @@ -38,6 +39,8 @@ from werkzeug.datastructures import MultiDict import config from pgadmin import current_blueprint +from pgadmin.authenticate import get_logout_url +from pgadmin.authenticate.mfa.utils import mfa_required, is_mfa_enabled from pgadmin.settings import get_setting, store_setting from pgadmin.utils import PgAdminModule from pgadmin.utils.ajax import make_json_response @@ -695,6 +698,7 @@ def check_browser_upgrade(): @blueprint.route("/") @pgCSRFProtect.exempt @login_required +@mfa_required def index(): """Render and process the main browser window.""" # Register Gravatar module with the app only if required @@ -754,7 +758,11 @@ def index(): username=current_user.username, auth_source=auth_source, is_admin=current_user.has_role("Administrator"), - logout_url=_get_logout_url(), + logout_url=get_logout_url(), + requirejs=True, + basejs=True, + mfa_enabled=is_mfa_enabled(), + login_url=login_url, _=gettext, auth_only_internal=auth_only_internal )) @@ -848,7 +856,7 @@ def utils(): app_version_int=config.APP_VERSION_INT, pg_libpq_version=pg_libpq_version, support_ssh_tunnel=config.SUPPORT_SSH_TUNNEL, - logout_url=_get_logout_url(), + logout_url=get_logout_url(), platform=sys.platform, qt_default_placeholder=QT_DEFAULT_PLACEHOLDER, enable_psql=config.ENABLE_PSQL diff --git a/web/pgadmin/browser/static/js/browser.js b/web/pgadmin/browser/static/js/browser.js index e866d5c48..3457f122d 100644 --- a/web/pgadmin/browser/static/js/browser.js +++ b/web/pgadmin/browser/static/js/browser.js @@ -19,7 +19,7 @@ define('pgadmin.browser', [ 'pgadmin.browser.menu', 'pgadmin.browser.panel', 'pgadmin.browser.layout', 'pgadmin.browser.runtime', 'pgadmin.browser.error', 'pgadmin.browser.frame', 'pgadmin.browser.node', 'pgadmin.browser.collection', 'pgadmin.browser.activity', - 'sources/codemirror/addon/fold/pgadmin-sqlfoldcode', + 'sources/codemirror/addon/fold/pgadmin-sqlfoldcode', 'pgadmin.browser.dialog', 'pgadmin.browser.keyboard', 'sources/tree/pgadmin_tree_save_state','jquery.acisortable', 'jquery.acifragment', ], function( @@ -208,7 +208,7 @@ define('pgadmin.browser', [ let ih = window.innerHeight; if (ih > passed_height){ return passed_height; - }else{ + } else { if (ih > pgAdmin.Browser.stdH.lg) return pgAdmin.Browser.stdH.lg; else if (ih > pgAdmin.Browser.stdH.md) diff --git a/web/pgadmin/browser/static/js/dialog.js b/web/pgadmin/browser/static/js/dialog.js new file mode 100644 index 000000000..e6c225524 --- /dev/null +++ b/web/pgadmin/browser/static/js/dialog.js @@ -0,0 +1,108 @@ +///////////////////////////////////////////////////////////// +// +// pgAdmin 4 - PostgreSQL Tools +// +// Copyright (C) 2013 - 2021, The pgAdmin Development Team +// This software is released under the PostgreSQL Licence +// +////////////////////////////////////////////////////////////// + +import gettext from 'sources/gettext'; +import * as alertify from 'pgadmin.alertifyjs'; +import url_for from 'sources/url_for'; +import pgAdmin from 'sources/pgadmin'; + +let counter = 1; + +function url_dialog(_title, _url, _help_filename, _width, _height) { + + let pgBrowser = pgAdmin.Browser; + + const dlgName = 'UrlDialog' + counter++; + + alertify.dialog(dlgName, function factory() { + return { + main: function() {}, + build: function() { + alertify.pgDialogBuild.apply(this); + }, + settings: { + url: _url, + title: _title, + }, + setup: function() { + return { + buttons: [{ + text: '', + key: 112, + className: 'btn btn-primary-icon pull-left fa fa-question pg-alertify-icon-button', + attrs: { + name: 'dialog_help', + type: 'button', + label: _title, + url: url_for('help.static', { + 'filename': _help_filename, + }), + }, + }, { + text: gettext('Close'), + key: 27, + className: 'btn btn-secondary fa fa-lg fa-times pg-alertify-button', + attrs: { + name: 'close', + type: 'button', + }, + }], + // Set options for dialog + options: { + //disable both padding and overflow control. + padding: !1, + overflow: !1, + modal: false, + resizable: true, + maximizable: true, + pinnable: false, + closableByDimmer: false, + closable: false, + }, + }; + }, + hooks: { + // Triggered when the dialog is closed + onclose: function() { + // Clear the view + return setTimeout((function() { + return (alertify[dlgName]()).destroy(); + }), 1000); + }, + }, + prepare: function() { + // create the iframe element + var iframe = document.createElement('iframe'); + iframe.frameBorder = 'no'; + iframe.width = '100%'; + iframe.height = '100%'; + iframe.src = this.setting('url'); + // add it to the dialog + this.elements.content.appendChild(iframe); + }, + callback: function(e) { + if (e.button.element.name == 'dialog_help') { + e.cancel = true; + pgBrowser.showHelp( + e.button.element.name, e.button.element.getAttribute('url'), + null, null + ); + return; + } + }, + }; + }); + (alertify[dlgName]()).show().resizeTo(_width || pgBrowser.stdW.lg, _height || pgBrowser.stdH.md); +} + +pgAdmin.ui.dialogs.url_dialog = url_dialog; + +export { + url_dialog, +}; diff --git a/web/pgadmin/browser/templates/browser/index.html b/web/pgadmin/browser/templates/browser/index.html index c1387dad5..7a237353b 100644 --- a/web/pgadmin/browser/templates/browser/index.html +++ b/web/pgadmin/browser/templates/browser/index.html @@ -152,6 +152,17 @@ window.onload = function(e){ {% endif %} + {% if mfa_enabled is defined and mfa_enabled is true %} +
  • + {{ _('Two-Factor Authentication') }} +
  • + + {% endif %} {% if is_admin %}
  • {{ _('Users') }}
  • diff --git a/web/pgadmin/model/__init__.py b/web/pgadmin/model/__init__.py index aea32c1e1..f79cc31ef 100644 --- a/web/pgadmin/model/__init__.py +++ b/web/pgadmin/model/__init__.py @@ -473,3 +473,11 @@ class UserMacros(db.Model): ) name = db.Column(db.String(1024), nullable=False) sql = db.Column(db.Text(), nullable=False) + + +class UserMFA(db.Model): + """Stores the options for the MFA for a particular user.""" + __tablename__ = 'user_mfa' + user_id = db.Column(db.Integer, db.ForeignKey(USER_ID), primary_key=True) + mfa_auth = db.Column(db.String(64), primary_key=True) + options = db.Column(db.Text(), nullable=True) diff --git a/web/pgadmin/static/img/login.svg b/web/pgadmin/static/img/login.svg index f502c59e0..64fb3b4e5 100644 --- a/web/pgadmin/static/img/login.svg +++ b/web/pgadmin/static/img/login.svg @@ -1 +1 @@ -login_graphic \ No newline at end of file +login_graphic diff --git a/web/pgadmin/static/js/pgadmin.js b/web/pgadmin/static/js/pgadmin.js index 4377b0a27..a9abcfa90 100644 --- a/web/pgadmin/static/js/pgadmin.js +++ b/web/pgadmin/static/js/pgadmin.js @@ -178,5 +178,7 @@ define([], function() { }; } + pgAdmin.ui = {dialogs: {}}; + return pgAdmin; }); diff --git a/web/pgadmin/static/scss/_pgadmin.style.scss b/web/pgadmin/static/scss/_pgadmin.style.scss index 861c9b08f..88a64129c 100644 --- a/web/pgadmin/static/scss/_pgadmin.style.scss +++ b/web/pgadmin/static/scss/_pgadmin.style.scss @@ -932,19 +932,31 @@ table.table-empty-rows{ } .login_page { - background-color: $color-primary; + background-color: $login-page-background; height: 100%; position:relative; z-index:1; color: $security-text-color; + & a { + color: $security-text-color; + } + + & .panel-container { + background-color: rgba($security-btn-color, 0.25); + padding: 0px; + } + & .panel-header { padding-bottom: 1.0rem; } & .panel-body { padding-bottom: 0.8rem; } - & .btn-login { + & .btn-login { + background-color: $security-btn-color; + } + & .btn-validate { background-color: $security-btn-color; } & .btn-oauth { @@ -978,8 +990,11 @@ table.table-empty-rows{ z-index: 100; } +.auth_page { + background-color: $color-gray-dark; +} + .change_pass { - background-color: $color-gray-light; height: 100%; position:relative; z-index:1; diff --git a/web/pgadmin/static/scss/resources/_default.variables.scss b/web/pgadmin/static/scss/resources/_default.variables.scss index d9fb6653a..201b51767 100644 --- a/web/pgadmin/static/scss/resources/_default.variables.scss +++ b/web/pgadmin/static/scss/resources/_default.variables.scss @@ -364,6 +364,7 @@ $erd-canvas-grid: $color-gray !default; $erd-link-color: $color-fg !default; $erd-link-selected-color: $color-fg !default; +$login-page-background: $color-primary !default; @function url-friendly-colour($colour) { @return '%23' + str-slice('#{$colour}', 2, -1) diff --git a/web/pgadmin/static/scss/resources/dark/_theme.variables.scss b/web/pgadmin/static/scss/resources/dark/_theme.variables.scss index 2fa907b87..3be703fa6 100644 --- a/web/pgadmin/static/scss/resources/dark/_theme.variables.scss +++ b/web/pgadmin/static/scss/resources/dark/_theme.variables.scss @@ -79,6 +79,8 @@ $color-editor-operator: #d6aaaa; $color-editor-foldmarker: #0000FF !default; $color-editor-activeline: #323e43 !default; +$login-page-background: $color-bg; + $explain-sev-2-bg: #ded17e; $explain-sev-3-bg: #824d18; $explain-sev-4-bg: #880000; diff --git a/web/pgadmin/static/scss/resources/high_contrast/_theme.variables.scss b/web/pgadmin/static/scss/resources/high_contrast/_theme.variables.scss index b4783bce0..86a6ba319 100644 --- a/web/pgadmin/static/scss/resources/high_contrast/_theme.variables.scss +++ b/web/pgadmin/static/scss/resources/high_contrast/_theme.variables.scss @@ -37,6 +37,8 @@ $color-gray: #1F2932; $color-gray-light: #2D3A48; $color-gray-lighter: #8B9CAD; +$login-page-background: $color-bg; + $sql-gutters-bg: $color-gray-light; $sql-title-bg: #1F2932; $sql-title-fg: $color-fg; diff --git a/web/pgadmin/templates/base.html b/web/pgadmin/templates/base.html index ce375ec44..74f42d51b 100644 --- a/web/pgadmin/templates/base.html +++ b/web/pgadmin/templates/base.html @@ -31,6 +31,7 @@ window.resourceBasePath = "{{ url_for('static', filename='js') }}/generated/"; +{% if requirejs is defined and requirejs is true %} - +{% endif %} +{% if basejs is defined and basejs is true %} +{% endif %} @@ -72,7 +75,6 @@ {% block body %}{% endblock %} diff --git a/web/pgadmin/templates/security/change_password.html b/web/pgadmin/templates/security/change_password.html index 0420e9059..3436bce01 100644 --- a/web/pgadmin/templates/security/change_password.html +++ b/web/pgadmin/templates/security/change_password.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% from "security/fields.html" import render_field_with_errors %} {% block body %} -
    +
    {% include "security/messages.html" %}
    diff --git a/web/pgadmin/templates/security/panel.html b/web/pgadmin/templates/security/panel.html index 46aa0524c..82944f991 100644 --- a/web/pgadmin/templates/security/panel.html +++ b/web/pgadmin/templates/security/panel.html @@ -2,7 +2,7 @@ {% from "security/fields.html" import render_field_with_errors, render_username_with_errors %} {% block title %}{{ config.APP_NAME }}{% endblock %} {% block body %} -