2013-12-27 22:19:14 +00:00
|
|
|
import logging
|
2014-09-04 18:24:20 +00:00
|
|
|
import datetime
|
2015-02-06 22:52:09 +00:00
|
|
|
import os
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2017-07-20 17:46:20 +00:00
|
|
|
from flask import make_response, render_template, request, session
|
2016-09-29 00:17:14 +00:00
|
|
|
from flask_login import login_user
|
|
|
|
from flask_principal import identity_changed
|
|
|
|
|
2017-02-01 23:17:25 +00:00
|
|
|
import endpoints.decorated # Register the various exceptions via decorators.
|
2016-09-29 00:17:14 +00:00
|
|
|
import features
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2018-08-20 19:12:18 +00:00
|
|
|
from app import app, oauth_apps, oauth_login, LoginWrappedDBUser, user_analytics, IS_KUBERNETES
|
2014-03-19 22:21:58 +00:00
|
|
|
from auth import scopes
|
2016-09-29 00:17:14 +00:00
|
|
|
from auth.permissions import QuayDeferredPermissionUser
|
2015-08-03 20:56:32 +00:00
|
|
|
from config import frontend_visible_config
|
2014-05-09 22:49:33 +00:00
|
|
|
from external_libraries import get_external_javascript, get_external_css
|
2017-07-20 19:57:42 +00:00
|
|
|
from endpoints.common_models_pre_oci import pre_oci_model as model
|
2016-09-29 00:17:14 +00:00
|
|
|
from util.secscan import PRIORITY_LEVELS
|
2017-04-05 15:25:29 +00:00
|
|
|
from util.saas.useranalytics import build_error_callback
|
2016-10-10 17:00:59 +00:00
|
|
|
from util.timedeltastring import convert_to_timedelta
|
2017-07-20 15:31:22 +00:00
|
|
|
from _init import __version__
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2014-04-05 03:26:10 +00:00
|
|
|
|
2013-12-27 22:19:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-07-21 19:09:31 +00:00
|
|
|
|
2017-08-02 15:26:36 +00:00
|
|
|
JS_BUNDLE_NAME = 'bundle'
|
|
|
|
|
|
|
|
|
2017-07-20 19:40:14 +00:00
|
|
|
def common_login(user_uuid, permanent_session=True):
|
2017-07-20 17:46:20 +00:00
|
|
|
""" Performs login of the given user, with optional non-permanence on the session. """
|
2017-07-20 19:57:42 +00:00
|
|
|
user = model.get_user(user_uuid)
|
|
|
|
if user is None:
|
|
|
|
return False
|
|
|
|
|
2017-07-20 19:40:14 +00:00
|
|
|
if login_user(LoginWrappedDBUser(user_uuid)):
|
2017-07-20 19:57:42 +00:00
|
|
|
logger.debug('Successfully signed in as user %s with uuid %s', user.username, user_uuid)
|
2017-07-20 19:40:14 +00:00
|
|
|
new_identity = QuayDeferredPermissionUser.for_id(user_uuid)
|
2013-12-27 22:19:14 +00:00
|
|
|
identity_changed.send(app, identity=new_identity)
|
2014-09-04 18:24:20 +00:00
|
|
|
session['login_time'] = datetime.datetime.now()
|
2016-10-13 17:48:35 +00:00
|
|
|
|
2016-10-10 17:00:59 +00:00
|
|
|
if permanent_session and features.PERMANENT_SESSIONS:
|
|
|
|
session_timeout_str = app.config.get('SESSION_TIMEOUT', '31d')
|
|
|
|
session.permanent = True
|
|
|
|
session.permanent_session_lifetime = convert_to_timedelta(session_timeout_str)
|
|
|
|
|
2016-10-13 17:48:35 +00:00
|
|
|
# Inform our user analytics that we have a new "lead"
|
2017-07-20 19:57:42 +00:00
|
|
|
create_lead_future = user_analytics.create_lead(
|
|
|
|
user.email,
|
|
|
|
user.username,
|
|
|
|
user.given_name,
|
|
|
|
user.family_name,
|
|
|
|
user.company,
|
2018-02-06 19:24:00 +00:00
|
|
|
user.location,
|
2017-07-20 19:57:42 +00:00
|
|
|
)
|
|
|
|
create_lead_future.add_done_callback(build_error_callback('Create lead failed'))
|
2013-12-27 22:19:14 +00:00
|
|
|
return True
|
2017-07-20 19:40:14 +00:00
|
|
|
|
|
|
|
logger.debug('User could not be logged in, inactive?')
|
|
|
|
return False
|
2013-12-28 19:07:44 +00:00
|
|
|
|
2014-03-26 22:36:59 +00:00
|
|
|
|
2017-08-01 17:28:24 +00:00
|
|
|
def _list_files(path, extension, contains=""):
|
2017-07-20 15:31:22 +00:00
|
|
|
""" Returns a list of all the files with the given extension found under the given path. """
|
2014-04-15 20:35:15 +00:00
|
|
|
def matches(f):
|
2017-08-01 17:28:24 +00:00
|
|
|
return os.path.splitext(f)[1] == '.' + extension and contains in os.path.splitext(f)[0]
|
2014-04-15 20:35:15 +00:00
|
|
|
|
|
|
|
def join_path(dp, f):
|
|
|
|
# Remove the static/ prefix. It is added in the template.
|
|
|
|
return os.path.join(dp, f)[len('static/'):]
|
|
|
|
|
2017-02-01 23:17:25 +00:00
|
|
|
filepath = os.path.join('static/', path)
|
2017-07-20 17:46:20 +00:00
|
|
|
return [join_path(dp, f) for dp, _, files in os.walk(filepath) for f in files if matches(f)]
|
2014-04-15 19:51:32 +00:00
|
|
|
|
2015-12-28 18:27:32 +00:00
|
|
|
|
2016-01-08 18:53:27 +00:00
|
|
|
def render_page_template(name, route_data=None, **kwargs):
|
2017-07-20 17:46:20 +00:00
|
|
|
""" Renders the page template with the given name as the response and returns its contents. """
|
2017-08-02 15:26:36 +00:00
|
|
|
main_scripts = _list_files('build', 'js', JS_BUNDLE_NAME)
|
2014-04-15 19:51:32 +00:00
|
|
|
|
2014-11-25 20:32:10 +00:00
|
|
|
use_cdn = app.config.get('USE_CDN', True)
|
|
|
|
if request.args.get('use_cdn') is not None:
|
|
|
|
use_cdn = request.args.get('use_cdn') == 'true'
|
|
|
|
|
|
|
|
external_styles = get_external_css(local=not use_cdn)
|
|
|
|
external_scripts = get_external_javascript(local=not use_cdn)
|
2014-05-09 22:49:33 +00:00
|
|
|
|
2016-04-01 18:10:11 +00:00
|
|
|
# Add Stripe checkout if billing is enabled.
|
|
|
|
if features.BILLING:
|
|
|
|
external_scripts.append('//checkout.stripe.com/checkout.js')
|
|
|
|
|
2017-01-20 20:21:08 +00:00
|
|
|
def get_external_login_config():
|
|
|
|
login_config = []
|
|
|
|
for login_service in oauth_login.services:
|
|
|
|
login_config.append({
|
|
|
|
'id': login_service.service_id(),
|
|
|
|
'title': login_service.service_name(),
|
|
|
|
'config': login_service.get_public_config(),
|
|
|
|
'icon': login_service.get_icon(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return login_config
|
|
|
|
|
2014-11-05 21:43:37 +00:00
|
|
|
def get_oauth_config():
|
|
|
|
oauth_config = {}
|
|
|
|
for oauth_app in oauth_apps:
|
2014-11-07 01:35:52 +00:00
|
|
|
oauth_config[oauth_app.key_name] = oauth_app.get_public_config()
|
2014-11-05 21:43:37 +00:00
|
|
|
|
|
|
|
return oauth_config
|
|
|
|
|
2014-10-22 18:49:33 +00:00
|
|
|
contact_href = None
|
|
|
|
if len(app.config.get('CONTACT_INFO', [])) == 1:
|
|
|
|
contact_href = app.config['CONTACT_INFO'][0]
|
|
|
|
|
2015-12-28 18:27:32 +00:00
|
|
|
version_number = ''
|
|
|
|
if not features.BILLING:
|
2017-02-01 23:17:25 +00:00
|
|
|
version_number = 'Quay %s' % __version__
|
2015-12-28 18:27:32 +00:00
|
|
|
|
2018-03-16 17:03:42 +00:00
|
|
|
scopes_set = {scope.scope: scope._asdict() for scope in scopes.app_scopes(app.config).values()}
|
|
|
|
|
2017-07-20 17:46:20 +00:00
|
|
|
contents = render_template(name,
|
|
|
|
route_data=route_data,
|
|
|
|
external_styles=external_styles,
|
|
|
|
external_scripts=external_scripts,
|
|
|
|
main_scripts=main_scripts,
|
|
|
|
feature_set=features.get_features(),
|
|
|
|
config_set=frontend_visible_config(app.config),
|
|
|
|
oauth_set=get_oauth_config(),
|
|
|
|
external_login_set=get_external_login_config(),
|
2018-03-16 17:03:42 +00:00
|
|
|
scope_set=scopes_set,
|
2017-07-20 17:46:20 +00:00
|
|
|
vuln_priority_set=PRIORITY_LEVELS,
|
|
|
|
enterprise_logo=app.config.get('ENTERPRISE_LOGO_URL', ''),
|
|
|
|
mixpanel_key=app.config.get('MIXPANEL_KEY', ''),
|
|
|
|
munchkin_key=app.config.get('MARKETO_MUNCHKIN_ID', ''),
|
|
|
|
recaptcha_key=app.config.get('RECAPTCHA_SITE_KEY', ''),
|
|
|
|
google_tagmanager_key=app.config.get('GOOGLE_TAGMANAGER_KEY', ''),
|
|
|
|
google_anaytics_key=app.config.get('GOOGLE_ANALYTICS_KEY', ''),
|
|
|
|
sentry_public_dsn=app.config.get('SENTRY_PUBLIC_DSN', ''),
|
|
|
|
is_debug=str(app.config.get('DEBUGGING', False)).lower(),
|
|
|
|
aci_conversion=features.ACI_CONVERSION,
|
|
|
|
has_billing=features.BILLING,
|
|
|
|
contact_href=contact_href,
|
|
|
|
hostname=app.config['SERVER_HOSTNAME'],
|
|
|
|
preferred_scheme=app.config['PREFERRED_URL_SCHEME'],
|
|
|
|
version_number=version_number,
|
|
|
|
current_year=datetime.datetime.now().year,
|
2018-08-20 19:12:18 +00:00
|
|
|
is_kubernetes=IS_KUBERNETES,
|
2017-07-20 17:46:20 +00:00
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
resp = make_response(contents)
|
2014-02-18 20:50:15 +00:00
|
|
|
resp.headers['X-FRAME-OPTIONS'] = 'DENY'
|
2014-02-21 19:52:40 +00:00
|
|
|
return resp
|