2021-01-18 05:02:10 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2023-01-02 00:23:55 -06:00
|
|
|
# Copyright (C) 2013 - 2023, The pgAdmin Development Team
|
2021-01-18 05:02:10 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""A blueprint module implementing the Spnego/Kerberos authentication."""
|
|
|
|
|
|
|
|
import base64
|
2021-07-06 02:52:58 -05:00
|
|
|
from os import environ, path, remove
|
2021-01-18 05:02:10 -06:00
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
from werkzeug.datastructures import Headers, MultiDict
|
2021-11-24 05:52:57 -06:00
|
|
|
from flask_babel import gettext
|
2021-07-06 02:52:58 -05:00
|
|
|
from flask import request, Response, session,\
|
|
|
|
current_app, render_template, flash, url_for
|
|
|
|
from flask_security.views import _security
|
|
|
|
from flask_security.utils import logout_user
|
|
|
|
from flask_security import login_required
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
import config
|
|
|
|
from pgadmin.model import User
|
|
|
|
from pgadmin.tools.user_management import create_user
|
2023-06-30 05:38:33 -05:00
|
|
|
from pgadmin.utils.constants import KERBEROS, MessageType
|
2021-07-06 02:52:58 -05:00
|
|
|
from pgadmin.utils import PgAdminModule
|
|
|
|
from pgadmin.utils.ajax import make_json_response, internal_server_error
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
from pgadmin.authenticate.internal import BaseAuthentication
|
|
|
|
from pgadmin.authenticate import get_auth_sources
|
|
|
|
from pgadmin.utils.csrf import pgCSRFProtect
|
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
import gssapi
|
|
|
|
KERBEROS_AUTH_AVAILABLE = True
|
|
|
|
except ImportError:
|
|
|
|
# Do not fail at this time, as this could be a desktop mode.
|
|
|
|
# Instead throw the runtime error, when the server attempts
|
|
|
|
# to use this authentication method.
|
|
|
|
KERBEROS_AUTH_AVAILABLE = False
|
2021-01-20 06:39:07 -06:00
|
|
|
except OSError:
|
|
|
|
# On Windows, it fails with OSError, when KFW libraries not found.
|
|
|
|
# Instead throw the runtime error, when the server attempts
|
|
|
|
# to use this authentication method.
|
|
|
|
KERBEROS_AUTH_AVAILABLE = False
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
# Set the Kerberos config file
|
|
|
|
if config.KRB_KTNAME and config.KRB_KTNAME != '<KRB5_KEYTAB_FILE>':
|
|
|
|
environ['KRB5_KTNAME'] = config.KRB_KTNAME
|
|
|
|
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
class KerberosModule(PgAdminModule):
|
2021-11-24 05:52:57 -06:00
|
|
|
def register(self, app, options):
|
2021-07-06 02:52:58 -05:00
|
|
|
# Do not look for the sub_modules,
|
|
|
|
# instead call blueprint.register(...) directly
|
2022-11-18 22:43:41 -06:00
|
|
|
super().register(app, options)
|
2021-07-06 02:52:58 -05:00
|
|
|
|
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
return ['kerberos.login',
|
|
|
|
'kerberos.logout',
|
|
|
|
'kerberos.update_ticket',
|
|
|
|
'kerberos.validate_ticket']
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app):
|
|
|
|
MODULE_NAME = 'kerberos'
|
|
|
|
|
|
|
|
blueprint = KerberosModule(MODULE_NAME, __name__, static_url_path='')
|
|
|
|
|
|
|
|
@blueprint.route("/login",
|
|
|
|
endpoint="login", methods=["GET"])
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
def kerberos_login():
|
|
|
|
logout_user()
|
|
|
|
return Response(render_template("browser/kerberos_login.html",
|
|
|
|
login_url=url_for('security.login'),
|
|
|
|
))
|
|
|
|
|
|
|
|
@blueprint.route("/logout",
|
|
|
|
endpoint="logout", methods=["GET"])
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
def kerberos_logout():
|
|
|
|
logout_user()
|
|
|
|
if 'KRB5CCNAME' in session:
|
|
|
|
# Remove the credential cache
|
|
|
|
cache_file_path = session['KRB5CCNAME'].split(":")[1]
|
|
|
|
if path.exists(cache_file_path):
|
|
|
|
remove(cache_file_path)
|
|
|
|
|
|
|
|
return Response(render_template("browser/kerberos_logout.html",
|
|
|
|
login_url=url_for('security.login'),
|
|
|
|
))
|
|
|
|
|
|
|
|
@blueprint.route("/update_ticket",
|
|
|
|
endpoint="update_ticket", methods=["GET"])
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
@login_required
|
|
|
|
def kerberos_update_ticket():
|
|
|
|
"""
|
|
|
|
Update the kerberos ticket.
|
|
|
|
"""
|
|
|
|
from werkzeug.datastructures import Headers
|
|
|
|
headers = Headers()
|
|
|
|
|
|
|
|
authorization = request.headers.get("Authorization", None)
|
|
|
|
|
|
|
|
if authorization is None:
|
|
|
|
# Send the Negotiate header to the client
|
|
|
|
# if Kerberos ticket is not found.
|
|
|
|
headers.add('WWW-Authenticate', 'Negotiate')
|
|
|
|
return Response("Unauthorised", 401, headers)
|
|
|
|
else:
|
|
|
|
source = get_auth_sources(KERBEROS)
|
|
|
|
auth_header = authorization.split()
|
|
|
|
in_token = auth_header[1]
|
|
|
|
|
|
|
|
# Validate the Kerberos ticket
|
|
|
|
status, context = source.negotiate_start(in_token)
|
|
|
|
if status:
|
|
|
|
return Response("Ticket updated successfully.")
|
|
|
|
|
|
|
|
return Response(context, 500)
|
|
|
|
|
|
|
|
@blueprint.route("/validate_ticket",
|
|
|
|
endpoint="validate_ticket", methods=["GET"])
|
|
|
|
@pgCSRFProtect.exempt
|
|
|
|
@login_required
|
|
|
|
def kerberos_validate_ticket():
|
|
|
|
"""
|
|
|
|
Return the kerberos ticket lifetime left after getting the
|
|
|
|
ticket from the credential cache
|
|
|
|
"""
|
|
|
|
import gssapi
|
|
|
|
|
|
|
|
try:
|
|
|
|
del_creds = gssapi.Credentials(store={
|
|
|
|
'ccache': session['KRB5CCNAME']})
|
|
|
|
creds = del_creds.acquire(store={'ccache': session['KRB5CCNAME']})
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return internal_server_error(errormsg=str(e))
|
|
|
|
|
|
|
|
return make_json_response(
|
|
|
|
data={'ticket_lifetime': creds.lifetime},
|
|
|
|
status=200
|
|
|
|
)
|
|
|
|
|
|
|
|
app.register_blueprint(blueprint)
|
|
|
|
|
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
class KerberosAuthentication(BaseAuthentication):
|
|
|
|
|
2021-07-06 02:52:58 -05:00
|
|
|
LOGIN_VIEW = 'kerberos.login'
|
|
|
|
LOGOUT_VIEW = 'kerberos.logout'
|
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
def get_source_name(self):
|
|
|
|
return KERBEROS
|
|
|
|
|
|
|
|
def get_friendly_name(self):
|
|
|
|
return gettext("kerberos")
|
|
|
|
|
|
|
|
def validate(self, form):
|
2022-02-16 02:04:24 -06:00
|
|
|
return True, None
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
def authenticate(self, frm):
|
|
|
|
|
|
|
|
if KERBEROS_AUTH_AVAILABLE is not True:
|
|
|
|
raise RuntimeError(gettext(
|
|
|
|
"Kerberos authentication can't be used as"
|
|
|
|
" GSSAPI module couldn't be loaded."
|
|
|
|
))
|
|
|
|
|
|
|
|
retval = [True, None]
|
|
|
|
negotiate = False
|
|
|
|
headers = Headers()
|
|
|
|
authorization = request.headers.get("Authorization", None)
|
Update SQLAlchemy, Flask, Flask-SQLAlchemy, and other packages to current versions. #5901
- Update Flask, Flask-SQLAlchemy, Flask-Babel, Flask-Security-Too, Flask-SocketIO, pytz, psutil, SQLAlchemy, bcrypt, cryptography, eventlet, Authlib, requests python packages
- Remove pinned dnspython, Werkzeug packages from requirements.txt
2023-03-15 01:27:16 -05:00
|
|
|
form_class = _security.forms.get('login_form').cls
|
2022-08-17 01:51:05 -05:00
|
|
|
req_json = request.get_json(silent=True)
|
2021-01-18 05:02:10 -06:00
|
|
|
|
2022-08-17 01:24:16 -05:00
|
|
|
if req_json:
|
|
|
|
form = form_class(MultiDict(req_json))
|
2021-01-18 05:02:10 -06:00
|
|
|
else:
|
|
|
|
form = form_class()
|
|
|
|
|
|
|
|
try:
|
|
|
|
if authorization is not None:
|
|
|
|
auth_header = authorization.split()
|
|
|
|
if auth_header[0] == 'Negotiate':
|
|
|
|
status, negotiate = self.negotiate_start(auth_header[1])
|
|
|
|
|
|
|
|
if status:
|
|
|
|
# Saving the first 15 characters of the kerberos key
|
|
|
|
# to encrypt/decrypt database password
|
2021-07-06 02:52:58 -05:00
|
|
|
session['pass_enc_key'] = auth_header[1][0:15]
|
2021-01-18 05:02:10 -06:00
|
|
|
# Create user
|
|
|
|
retval = self.__auto_create_user(
|
|
|
|
str(negotiate.initiator_name))
|
|
|
|
elif isinstance(negotiate, Exception):
|
2023-06-30 05:38:33 -05:00
|
|
|
flash(gettext(negotiate), MessageType.ERROR)
|
2021-01-18 05:02:10 -06:00
|
|
|
retval = [status,
|
|
|
|
Response(render_template(
|
|
|
|
"security/login_user.html",
|
|
|
|
login_user_form=form))]
|
|
|
|
else:
|
|
|
|
headers.add('WWW-Authenticate', 'Negotiate ' +
|
|
|
|
str(base64.b64encode(negotiate), 'utf-8'))
|
|
|
|
return False, Response("Success", 200, headers)
|
|
|
|
else:
|
2023-06-30 05:38:33 -05:00
|
|
|
flash(gettext("Kerberos authentication failed. Couldn't find "
|
|
|
|
"kerberos ticket."), MessageType.ERROR)
|
2021-01-18 05:02:10 -06:00
|
|
|
headers.add('WWW-Authenticate', 'Negotiate')
|
|
|
|
retval = [False,
|
|
|
|
Response(render_template(
|
|
|
|
"security/login_user.html",
|
|
|
|
login_user_form=form), 401, headers)]
|
|
|
|
finally:
|
|
|
|
if negotiate is not False:
|
|
|
|
self.negotiate_end(negotiate)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def negotiate_start(self, in_token):
|
|
|
|
svc_princ = gssapi.Name('HTTP@%s' % config.KRB_APP_HOST_NAME,
|
|
|
|
name_type=gssapi.NameType.hostbased_service)
|
|
|
|
cname = svc_princ.canonicalize(gssapi.MechType.kerberos)
|
|
|
|
|
|
|
|
try:
|
|
|
|
server_creds = gssapi.Credentials(usage='accept', name=cname)
|
|
|
|
context = gssapi.SecurityContext(creds=server_creds)
|
|
|
|
out_token = context.step(base64.b64decode(in_token))
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return False, e
|
|
|
|
|
|
|
|
if out_token and not context.complete:
|
|
|
|
return False, out_token
|
|
|
|
if context.complete:
|
2021-05-03 05:40:45 -05:00
|
|
|
deleg_creds = context.delegated_creds
|
|
|
|
if not hasattr(deleg_creds, 'name'):
|
|
|
|
error_msg = gettext('Delegated credentials not supplied.')
|
|
|
|
current_app.logger.error(error_msg)
|
|
|
|
return False, Exception(error_msg)
|
|
|
|
try:
|
|
|
|
cache_file_path = path.join(
|
|
|
|
config.KERBEROS_CCACHE_DIR, 'pgadmin_cache_{0}'.format(
|
|
|
|
deleg_creds.name)
|
|
|
|
)
|
|
|
|
CCACHE = 'FILE:{0}'.format(cache_file_path)
|
|
|
|
store = {'ccache': CCACHE}
|
|
|
|
deleg_creds.store(store, overwrite=True, set_default=True)
|
|
|
|
session['KRB5CCNAME'] = CCACHE
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return False, e
|
|
|
|
|
2021-01-18 05:02:10 -06:00
|
|
|
return True, context
|
|
|
|
else:
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
def negotiate_end(self, context):
|
2021-05-03 05:40:45 -05:00
|
|
|
# Free Delegated Credentials
|
2021-01-18 05:02:10 -06:00
|
|
|
del_creds = getattr(context, 'delegated_creds', None)
|
|
|
|
if del_creds:
|
|
|
|
deleg_creds = context.delegated_creds
|
2022-08-01 11:36:34 -05:00
|
|
|
del deleg_creds
|
2021-01-18 05:02:10 -06:00
|
|
|
|
|
|
|
def __auto_create_user(self, username):
|
2021-05-03 05:40:45 -05:00
|
|
|
"""Add the kerberos user to the internal SQLite database."""
|
2021-01-18 05:02:10 -06:00
|
|
|
username = str(username)
|
|
|
|
if config.KRB_AUTO_CREATE_USER:
|
|
|
|
user = User.query.filter_by(
|
2021-07-06 02:52:58 -05:00
|
|
|
username=username, auth_source=KERBEROS).first()
|
2021-01-18 05:02:10 -06:00
|
|
|
if user is None:
|
2023-02-12 23:41:05 -06:00
|
|
|
create_msg = ("Creating user {0} with email {1} "
|
|
|
|
"from auth source KERBEROS.")
|
|
|
|
current_app.logger.info(create_msg.format(username,
|
|
|
|
username))
|
2021-01-18 05:02:10 -06:00
|
|
|
return create_user({
|
|
|
|
'username': username,
|
|
|
|
'email': username,
|
|
|
|
'role': 2,
|
|
|
|
'active': True,
|
|
|
|
'auth_source': KERBEROS
|
|
|
|
})
|
|
|
|
|
|
|
|
return True, {'username': username}
|