2020-05-14 19:07:05 +00:00
|
|
|
import json
|
|
|
|
|
2020-02-20 20:35:07 +00:00
|
|
|
import connexion
|
2020-05-21 16:11:35 +00:00
|
|
|
from flask import redirect, g, request
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-05-22 11:55:58 +00:00
|
|
|
from crc import app, db
|
2020-02-18 21:38:56 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc.models.user import UserModel, UserModelSchema
|
2020-05-22 13:50:18 +00:00
|
|
|
from crc.services.ldap_service import LdapService, LdapUserInfo
|
2020-03-24 18:15:21 +00:00
|
|
|
|
2020-02-20 20:35:07 +00:00
|
|
|
"""
|
|
|
|
.. module:: crc.api.user
|
|
|
|
:synopsis: Single Sign On (SSO) user login and session handlers
|
|
|
|
"""
|
2020-02-18 21:38:56 +00:00
|
|
|
def verify_token(token):
|
2020-03-24 18:15:21 +00:00
|
|
|
failure_error = ApiError("invalid_token", "Unable to decode the token you provided. Please re-authenticate", status_code=403)
|
|
|
|
if (not 'PRODUCTION' in app.config or not app.config['PRODUCTION']) and token == app.config["SWAGGER_AUTH_KEY"]:
|
|
|
|
g.user = UserModel.query.first()
|
|
|
|
token = g.user.encode_auth_token()
|
|
|
|
|
2020-02-18 21:38:56 +00:00
|
|
|
try:
|
2020-03-24 18:15:21 +00:00
|
|
|
token_info = UserModel.decode_auth_token(token)
|
|
|
|
g.user = UserModel.query.filter_by(uid=token_info['sub']).first()
|
2020-02-18 21:38:56 +00:00
|
|
|
except:
|
2020-03-24 18:15:21 +00:00
|
|
|
raise failure_error
|
2020-02-18 21:38:56 +00:00
|
|
|
if g.user is not None:
|
2020-03-24 18:15:21 +00:00
|
|
|
return token_info
|
2020-02-18 21:38:56 +00:00
|
|
|
else:
|
2020-03-24 18:15:21 +00:00
|
|
|
raise failure_error
|
2020-02-18 21:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_current_user():
|
|
|
|
return UserModelSchema().dump(g.user)
|
|
|
|
|
2020-05-24 04:05:13 +00:00
|
|
|
@app.route('/v1.0/login')
|
2020-05-22 11:55:58 +00:00
|
|
|
def sso_login():
|
|
|
|
# This what I see coming back:
|
|
|
|
# X-Remote-Cn: Daniel Harold Funk (dhf8r)
|
|
|
|
# X-Remote-Sn: Funk
|
|
|
|
# X-Remote-Givenname: Daniel
|
|
|
|
# X-Remote-Uid: dhf8r
|
|
|
|
# Eppn: dhf8r@virginia.edu
|
|
|
|
# Cn: Daniel Harold Funk (dhf8r)
|
|
|
|
# Sn: Funk
|
|
|
|
# Givenname: Daniel
|
|
|
|
# Uid: dhf8r
|
|
|
|
# X-Remote-User: dhf8r@virginia.edu
|
|
|
|
# X-Forwarded-For: 128.143.0.10
|
|
|
|
# X-Forwarded-Host: dev.crconnect.uvadcos.io
|
|
|
|
# X-Forwarded-Server: dev.crconnect.uvadcos.io
|
|
|
|
# Connection: Keep-Alive
|
|
|
|
uid = request.headers.get("Uid")
|
|
|
|
if not uid:
|
|
|
|
uid = request.headers.get("X-Remote-Uid")
|
|
|
|
|
|
|
|
if not uid:
|
|
|
|
raise ApiError("invalid_sso_credentials", "'Uid' nor 'X-Remote-Uid' were present in the headers: %s"
|
|
|
|
% str(request.headers))
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-05-21 16:11:35 +00:00
|
|
|
redirect = request.args.get('redirect')
|
|
|
|
app.logger.info("SSO_LOGIN: Full URL: " + request.url)
|
2020-05-22 11:55:58 +00:00
|
|
|
app.logger.info("SSO_LOGIN: User Id: " + uid)
|
2020-05-22 13:50:18 +00:00
|
|
|
app.logger.info("SSO_LOGIN: Will try to redirect to : " + str(redirect))
|
2020-05-22 11:55:58 +00:00
|
|
|
|
|
|
|
ldap_service = LdapService()
|
|
|
|
info = ldap_service.user_info(uid)
|
|
|
|
|
2020-05-22 13:50:18 +00:00
|
|
|
return _handle_login(info, redirect)
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-05-24 04:05:13 +00:00
|
|
|
@app.route('/sso')
|
2020-05-22 11:55:58 +00:00
|
|
|
def sso():
|
|
|
|
response = ""
|
|
|
|
response += "<h1>Headers</h1>"
|
2020-05-22 13:50:18 +00:00
|
|
|
response += "<ul>"
|
|
|
|
for k,v in request.headers:
|
|
|
|
response += "<li><b>%s</b> %s</li>\n" % (k, v)
|
2020-05-22 11:55:58 +00:00
|
|
|
response += "<h1>Environment</h1>"
|
2020-05-22 13:50:18 +00:00
|
|
|
for k,v in request.environ:
|
|
|
|
response += "<li><b>%s</b> %s</li>\n" % (k, v)
|
2020-05-22 11:55:58 +00:00
|
|
|
return response
|
2020-05-21 20:02:45 +00:00
|
|
|
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-05-22 13:50:18 +00:00
|
|
|
def _handle_login(user_info: LdapUserInfo, redirect_url=app.config['FRONTEND_AUTH_CALLBACK']):
|
2020-02-20 20:35:07 +00:00
|
|
|
"""On successful login, adds user to database if the user is not already in the system,
|
|
|
|
then returns the frontend auth callback URL, with auth token appended.
|
|
|
|
|
|
|
|
Args:
|
2020-05-22 13:50:18 +00:00
|
|
|
user_info - an ldap user_info object.
|
|
|
|
redirect_url: Optional[str]
|
2020-02-20 20:35:07 +00:00
|
|
|
|
|
|
|
Returns:
|
2020-02-21 16:24:39 +00:00
|
|
|
Response. 302 - Redirects to the frontend auth callback URL, with auth token appended.
|
2020-02-20 20:35:07 +00:00
|
|
|
"""
|
2020-05-22 13:50:18 +00:00
|
|
|
user = db.session.query(UserModel).filter(UserModel.uid == user_info.uid).first()
|
2020-02-20 20:35:07 +00:00
|
|
|
|
2020-02-21 16:24:39 +00:00
|
|
|
if user is None:
|
|
|
|
# Add new user
|
2020-05-22 13:50:18 +00:00
|
|
|
user = UserModel()
|
2020-02-20 20:35:07 +00:00
|
|
|
|
2020-05-22 13:50:18 +00:00
|
|
|
user.uid = user_info.uid
|
|
|
|
user.display_name = user_info.display_name
|
|
|
|
user.email_address = user_info.email_address
|
|
|
|
user.affiliation = user_info.affiliation
|
|
|
|
user.title = user_info.title
|
2020-02-20 20:35:07 +00:00
|
|
|
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Return the frontend auth callback URL, with auth token appended.
|
2020-02-18 21:38:56 +00:00
|
|
|
auth_token = user.encode_auth_token().decode()
|
2020-03-24 18:15:21 +00:00
|
|
|
if redirect_url is not None:
|
2020-05-21 16:11:35 +00:00
|
|
|
app.logger.info("SSO_LOGIN: REDIRECTING TO: " + redirect_url)
|
2020-03-24 18:15:21 +00:00
|
|
|
return redirect('%s/%s' % (redirect_url, auth_token))
|
|
|
|
else:
|
2020-05-21 16:11:35 +00:00
|
|
|
app.logger.info("SSO_LOGIN: NO REDIRECT, JUST RETURNING AUTH TOKEN.")
|
2020-03-24 18:15:21 +00:00
|
|
|
return auth_token
|
2020-02-18 21:38:56 +00:00
|
|
|
|
2020-05-22 13:50:18 +00:00
|
|
|
|
|
|
|
|
2020-02-21 16:24:39 +00:00
|
|
|
def backdoor(
|
|
|
|
uid=None,
|
|
|
|
affiliation=None,
|
|
|
|
display_name=None,
|
|
|
|
email_address=None,
|
|
|
|
eppn=None,
|
|
|
|
first_name=None,
|
|
|
|
last_name=None,
|
2020-02-24 21:59:16 +00:00
|
|
|
title=None,
|
2020-05-26 19:09:57 +00:00
|
|
|
redirect=None,
|
2020-02-21 16:24:39 +00:00
|
|
|
):
|
2020-02-20 20:35:07 +00:00
|
|
|
"""A backdoor for end-to-end system testing that allows the system to simulate logging in as a specific user.
|
|
|
|
Only works if the application is running in a non-production environment.
|
|
|
|
|
|
|
|
Args:
|
2020-02-21 16:24:39 +00:00
|
|
|
uid: str
|
|
|
|
affiliation: Optional[str]
|
|
|
|
display_name: Optional[str]
|
|
|
|
email_address: Optional[str]
|
|
|
|
eppn: Optional[str]
|
|
|
|
first_name: Optional[str]
|
|
|
|
last_name: Optional[str]
|
|
|
|
title: Optional[str]
|
2020-02-24 21:59:16 +00:00
|
|
|
redirect_url: Optional[str]
|
2020-02-20 20:35:07 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
str. If not on production, returns the frontend auth callback URL, with auth token appended.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ApiError. If on production, returns a 404 error.
|
|
|
|
"""
|
|
|
|
if not 'PRODUCTION' in app.config or not app.config['PRODUCTION']:
|
2020-05-25 16:29:05 +00:00
|
|
|
|
|
|
|
ldap_info = LdapService().user_info(uid)
|
2020-05-26 19:09:57 +00:00
|
|
|
return _handle_login(ldap_info, redirect)
|
2020-02-18 21:38:56 +00:00
|
|
|
else:
|
2020-02-20 20:35:07 +00:00
|
|
|
raise ApiError('404', 'unknown')
|