2020-04-06 05:27:05 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2021-01-04 04:04:45 -06:00
|
|
|
# Copyright (C) 2013 - 2021, The pgAdmin Development Team
|
2020-04-06 05:27:05 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""A blueprint module implementing the Authentication."""
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
import config
|
|
|
|
import copy
|
|
|
|
|
2021-07-22 01:54:43 -05:00
|
|
|
from flask import current_app, flash, Response, request, url_for, \
|
2021-07-06 02:52:58 -05:00
|
|
|
session, redirect
|
2021-05-03 05:40:45 -05:00
|
|
|
from flask_babelex import gettext
|
2021-07-06 02:52:58 -05:00
|
|
|
from flask_security.views import _security
|
|
|
|
from flask_security.utils import get_post_logout_redirect, \
|
2021-07-22 01:54:43 -05:00
|
|
|
get_post_login_redirect, logout_user
|
2021-01-18 05:02:10 -06:00
|
|
|
|
2021-07-22 01:54:43 -05:00
|
|
|
from pgadmin import db, User
|
2020-04-06 05:27:05 -05:00
|
|
|
from pgadmin.utils import PgAdminModule
|
2021-07-06 02:52:58 -05:00
|
|
|
from pgadmin.utils.constants import KERBEROS, INTERNAL, OAUTH2, LDAP
|
|
|
|
from pgadmin.authenticate.registry import AuthSourceRegistry
|
2021-01-18 05:02:10 -06:00
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
MODULE_NAME = 'authenticate'
|
2021-07-06 02:52:58 -05:00
|
|
|
auth_obj = None
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
class AuthenticateModule(PgAdminModule):
|
|
|
|
def get_exposed_url_endpoints(self):
|
2021-07-06 02:52:58 -05:00
|
|
|
return ['authenticate.login']
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
blueprint = AuthenticateModule(MODULE_NAME, __name__, static_url_path='')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/login', endpoint='login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
"""
|
|
|
|
Entry point for all the authentication sources.
|
|
|
|
The user input will be validated and authenticated.
|
|
|
|
"""
|
|
|
|
form = _security.login_form()
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
auth_obj = AuthSourceManager(form, copy.deepcopy(
|
|
|
|
config.AUTHENTICATION_SOURCES))
|
2021-07-22 01:54:43 -05:00
|
|
|
if OAUTH2 in config.AUTHENTICATION_SOURCES \
|
2021-07-06 02:52:58 -05:00
|
|
|
and 'oauth2_button' in request.form:
|
|
|
|
session['auth_obj'] = auth_obj
|
|
|
|
|
|
|
|
session['auth_source_manager'] = None
|
2021-07-22 01:54:43 -05:00
|
|
|
|
|
|
|
username = form.data['email']
|
2021-08-09 03:54:26 -05:00
|
|
|
user = User.query.filter_by(username=username,
|
|
|
|
auth_source=INTERNAL).first()
|
2021-07-22 01:54:43 -05:00
|
|
|
|
|
|
|
if user:
|
|
|
|
if user.login_attempts >= config.MAX_LOGIN_ATTEMPTS > 0:
|
|
|
|
user.locked = True
|
|
|
|
else:
|
|
|
|
user.locked = False
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if user.login_attempts >= config.MAX_LOGIN_ATTEMPTS > 0:
|
|
|
|
flash(gettext('Your account is locked. Please contact the '
|
|
|
|
'Administrator.'),
|
|
|
|
'warning')
|
|
|
|
logout_user()
|
|
|
|
return redirect(get_post_logout_redirect())
|
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
# Validate the user
|
|
|
|
if not auth_obj.validate():
|
|
|
|
for field in form.errors:
|
2021-08-03 06:36:06 -05:00
|
|
|
flash_login_attempt_error = None
|
2021-08-09 10:25:06 -05:00
|
|
|
if user and field in config.LOGIN_ATTEMPT_FIELDS:
|
2021-07-22 01:54:43 -05:00
|
|
|
if config.MAX_LOGIN_ATTEMPTS > 0:
|
|
|
|
user.login_attempts += 1
|
2021-08-03 06:36:06 -05:00
|
|
|
left_attempts = \
|
|
|
|
config.MAX_LOGIN_ATTEMPTS - user.login_attempts
|
2021-08-18 09:03:01 -05:00
|
|
|
if left_attempts > 1:
|
|
|
|
flash_login_attempt_error = \
|
|
|
|
gettext('{0} more attempts remaining.'.
|
|
|
|
format(left_attempts))
|
|
|
|
else:
|
|
|
|
flash_login_attempt_error = \
|
|
|
|
gettext('{0} more attempt remaining.'.
|
|
|
|
format(left_attempts))
|
2021-07-22 01:54:43 -05:00
|
|
|
db.session.commit()
|
2020-04-06 05:27:05 -05:00
|
|
|
for error in form.errors[field]:
|
2021-08-18 09:03:01 -05:00
|
|
|
if flash_login_attempt_error:
|
|
|
|
error = error + flash_login_attempt_error
|
|
|
|
flash_login_attempt_error = None
|
2020-04-06 05:27:05 -05:00
|
|
|
flash(error, 'warning')
|
2021-08-03 06:36:06 -05:00
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
return redirect(get_post_logout_redirect())
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
# Authenticate the user
|
|
|
|
status, msg = auth_obj.authenticate()
|
|
|
|
if status:
|
|
|
|
# Login the user
|
|
|
|
status, msg = auth_obj.login()
|
2021-01-18 05:02:10 -06:00
|
|
|
current_auth_obj = auth_obj.as_dict()
|
2021-07-06 02:52:58 -05:00
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
if not status:
|
2021-07-22 01:54:43 -05:00
|
|
|
if current_auth_obj['current_source'] == \
|
2021-01-18 05:02:10 -06:00
|
|
|
KERBEROS:
|
2021-07-06 02:52:58 -05:00
|
|
|
return redirect('{0}?next={1}'.format(url_for(
|
2021-01-18 05:02:10 -06:00
|
|
|
'authenticate.kerberos_login'), url_for('browser.index')))
|
|
|
|
|
2021-04-08 07:45:34 -05:00
|
|
|
flash(msg, 'danger')
|
2021-07-06 02:52:58 -05:00
|
|
|
return redirect(get_post_logout_redirect())
|
2021-07-22 01:54:43 -05:00
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
session['auth_source_manager'] = current_auth_obj
|
2021-07-22 01:54:43 -05:00
|
|
|
|
2021-08-09 03:54:26 -05:00
|
|
|
if user:
|
|
|
|
user.login_attempts = 0
|
2021-07-22 01:54:43 -05:00
|
|
|
db.session.commit()
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
if 'auth_obj' in session:
|
2021-07-09 11:20:50 -05:00
|
|
|
session.pop('auth_obj')
|
2021-07-06 02:52:58 -05:00
|
|
|
return redirect(get_post_login_redirect())
|
2020-04-06 05:27:05 -05:00
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
elif isinstance(msg, Response):
|
|
|
|
return msg
|
2021-07-06 02:52:58 -05:00
|
|
|
elif 'oauth2_button' in request.form and not isinstance(msg, str):
|
|
|
|
return msg
|
2021-07-09 11:20:50 -05:00
|
|
|
if 'auth_obj' in session:
|
|
|
|
session.pop('auth_obj')
|
2021-04-08 07:45:34 -05:00
|
|
|
flash(msg, 'danger')
|
2021-07-06 02:52:58 -05:00
|
|
|
response = redirect(get_post_logout_redirect())
|
2021-01-18 05:02:10 -06:00
|
|
|
return response
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
class AuthSourceManager:
|
2020-04-06 05:27:05 -05:00
|
|
|
"""This class will manage all the authentication sources.
|
|
|
|
"""
|
2021-07-06 02:52:58 -05:00
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
def __init__(self, form, sources):
|
|
|
|
self.form = form
|
|
|
|
self.auth_sources = sources
|
|
|
|
self.source = None
|
2021-06-10 12:25:31 -05:00
|
|
|
self.source_friendly_name = INTERNAL
|
2021-07-06 02:52:58 -05:00
|
|
|
self.current_source = INTERNAL
|
|
|
|
self.update_auth_sources()
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
"""
|
|
|
|
Returns the dictionary object representing this object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
res = dict()
|
|
|
|
res['source_friendly_name'] = self.source_friendly_name
|
|
|
|
res['auth_sources'] = self.auth_sources
|
2021-01-18 05:02:10 -06:00
|
|
|
res['current_source'] = self.current_source
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
def update_auth_sources(self):
|
|
|
|
for auth_src in [KERBEROS, OAUTH2]:
|
|
|
|
if auth_src in self.auth_sources:
|
|
|
|
if 'internal_button' in request.form:
|
|
|
|
self.auth_sources.remove(auth_src)
|
2021-07-09 11:20:50 -05:00
|
|
|
else:
|
|
|
|
if INTERNAL in self.auth_sources:
|
|
|
|
self.auth_sources.remove(INTERNAL)
|
|
|
|
if LDAP in self.auth_sources:
|
|
|
|
self.auth_sources.remove(LDAP)
|
2021-07-06 02:52:58 -05:00
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
def set_current_source(self, source):
|
|
|
|
self.current_source = source
|
|
|
|
|
|
|
|
@property
|
2021-02-15 06:01:20 -06:00
|
|
|
def get_current_source(self):
|
2021-01-18 05:02:10 -06:00
|
|
|
return self.current_source
|
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
def set_source(self, source):
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
@property
|
|
|
|
def get_source(self):
|
|
|
|
return self.source
|
|
|
|
|
|
|
|
def set_source_friendly_name(self, name):
|
|
|
|
self.source_friendly_name = name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def get_source_friendly_name(self):
|
|
|
|
return self.source_friendly_name
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
"""Validate through all the sources."""
|
|
|
|
for src in self.auth_sources:
|
|
|
|
source = get_auth_sources(src)
|
|
|
|
if source.validate(self.form):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def authenticate(self):
|
|
|
|
"""Authenticate through all the sources."""
|
|
|
|
status = False
|
|
|
|
msg = None
|
|
|
|
for src in self.auth_sources:
|
|
|
|
source = get_auth_sources(src)
|
2021-07-06 02:52:58 -05:00
|
|
|
self.set_source(source)
|
2021-01-18 05:02:10 -06:00
|
|
|
current_app.logger.debug(
|
|
|
|
"Authentication initiated via source: %s" %
|
|
|
|
source.get_source_name())
|
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
status, msg = source.authenticate(self.form)
|
2021-01-18 05:02:10 -06:00
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
if status:
|
2021-01-18 05:02:10 -06:00
|
|
|
self.set_current_source(source.get_source_name())
|
|
|
|
if msg is not None and 'username' in msg:
|
|
|
|
self.form._fields['email'].data = msg['username']
|
2020-04-06 05:27:05 -05:00
|
|
|
return status, msg
|
2021-07-06 02:52:58 -05:00
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
return status, msg
|
|
|
|
|
|
|
|
def login(self):
|
|
|
|
status, msg = self.source.login(self.form)
|
|
|
|
if status:
|
|
|
|
self.set_source_friendly_name(self.source.get_friendly_name())
|
2021-01-18 05:02:10 -06:00
|
|
|
current_app.logger.debug(
|
|
|
|
"Authentication and Login successfully done via source : %s" %
|
|
|
|
self.source.get_source_name())
|
2021-07-06 02:52:58 -05:00
|
|
|
|
|
|
|
# Set the login, logout view as per source if available
|
|
|
|
current_app.login_manager.login_view = getattr(
|
|
|
|
self.source, 'LOGIN_VIEW', 'security.login')
|
|
|
|
current_app.login_manager.logout_view = getattr(
|
|
|
|
self.source, 'LOGOUT_VIEW', 'security.logout')
|
|
|
|
|
2020-04-06 05:27:05 -05:00
|
|
|
return status, msg
|
|
|
|
|
|
|
|
|
|
|
|
def get_auth_sources(type):
|
|
|
|
"""Get the authenticated source object from the registry"""
|
|
|
|
|
|
|
|
auth_sources = getattr(current_app, '_pgadmin_auth_sources', None)
|
|
|
|
|
|
|
|
if auth_sources is None or not isinstance(auth_sources, dict):
|
|
|
|
auth_sources = dict()
|
|
|
|
|
|
|
|
if type in auth_sources:
|
|
|
|
return auth_sources[type]
|
|
|
|
|
2021-06-24 01:00:11 -05:00
|
|
|
auth_source = AuthSourceRegistry.get(type)
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
if auth_source is not None:
|
|
|
|
auth_sources[type] = auth_source
|
|
|
|
setattr(current_app, '_pgadmin_auth_sources', auth_sources)
|
|
|
|
|
|
|
|
return auth_source
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app):
|
|
|
|
auth_sources = dict()
|
|
|
|
|
|
|
|
setattr(app, '_pgadmin_auth_sources', auth_sources)
|
2021-06-24 01:00:11 -05:00
|
|
|
AuthSourceRegistry.load_modules(app)
|
2020-04-06 05:27:05 -05:00
|
|
|
|
|
|
|
return auth_sources
|