def logout(): """ Log out the user currently logged in in the application """ auth = pagure_config.get('PAGURE_AUTH', None) if auth in ['fas', 'openid']: if hasattr(flask.g, 'fas_user') and flask.g.fas_user is not None: from pagure.ui.fas_login import FAS FAS.logout() elif auth == 'oidc': from pagure.ui.oidc_login import oidc_logout oidc_logout() elif auth == 'local': import pagure.ui.login as login login.logout()
def auth_login(): # pragma: no cover """ Method to log into the application using FAS OpenID. """ return_point = flask.url_for("ui_ns.index") if "next" in flask.request.args: if pagure.utils.is_safe_url(flask.request.args["next"]): return_point = flask.request.args["next"] authenticated = pagure.utils.authenticated() auth = pagure_config.get("PAGURE_AUTH", None) if not authenticated and auth == "oidc": from pagure.ui.oidc_login import oidc, fas_user_from_oidc, set_user # If oidc is used and user hits this endpoint, it will redirect # to IdP with destination=<pagure>/login?next=<location> # After confirming user identity, the IdP will redirect user here # again, but this time oidc.user_loggedin will be True and thus # execution will go through the else clause, making the Pagure # authentication machinery pick the user up if not oidc.user_loggedin: return oidc.redirect_to_auth_server(flask.request.url) else: flask.session["oidc_logintime"] = time.time() fas_user_from_oidc() authenticated = pagure.utils.authenticated() set_user() if authenticated: return flask.redirect(return_point) admins = pagure_config["ADMIN_GROUP"] if admins: if isinstance(admins, list): admins = set(admins) else: # pragma: no cover admins = set([admins]) else: admins = set() if auth in ["fas", "openid"]: from pagure.ui.fas_login import FAS groups = set() if not pagure_config.get("ENABLE_GROUP_MNGT", False): groups = [ group.group_name for group in pagure.lib.query.search_groups(flask.g.session, group_type="user") ] groups = set(groups).union(admins) ext_committer = set(pagure_config.get("EXTERNAL_COMMITTER", {})) groups = set(groups).union(ext_committer) flask.g.unsafe_javascript = True return FAS.login(return_url=return_point, groups=groups) elif auth == "local": form = pagure.login_forms.LoginForm() return flask.render_template("login/login.html", next_url=return_point, form=form)
def logout(): """ Log out the user currently logged in in the application """ auth = pagure_config.get("PAGURE_AUTH", None) if auth in ["fas", "openid"]: if hasattr(flask.g, "fas_user") and flask.g.fas_user is not None: from pagure.ui.fas_login import FAS FAS.logout() elif auth == "oidc": from pagure.ui.oidc_login import oidc_logout oidc_logout() elif auth == "local": import pagure.ui.login as login login.logout()
def create_app(config=None): """ Create the flask application. """ app = flask.Flask(__name__) app.config = pagure_config if config: app.config.update(config) if app.config.get("SESSION_TYPE", None) is not None: import flask_session flask_session.Session(app) pagure.utils.set_up_logging(app=app) @app.errorhandler(500) def fatal_error(error): # pragma: no cover """500 Fatal Error page""" logger.exception("Error while processing request") return flask.render_template("fatal_error.html", error=error), 500 app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True if perfrepo: # Do this as early as possible. # We want the perfrepo before_request to be the very first thing # to be run, so that we can properly setup the stats before the # request. app.before_request(perfrepo.reset_stats) auth = pagure_config.get("PAGURE_AUTH", None) if auth in ["fas", "openid"]: # Only import and set flask_fas_openid if it is needed from pagure.ui.fas_login import FAS FAS.init_app(app) elif auth == "oidc": # Only import and set flask_fas_openid if it is needed from pagure.ui.oidc_login import oidc, fas_user_from_oidc oidc.init_app(app) app.before_request(fas_user_from_oidc) if auth == "local": # Only import the login controller if the app is set up for local login import pagure.ui.login as login app.before_request(login._check_session_cookie) app.after_request(login._send_session_cookie) # Support proxy app.wsgi_app = pagure.proxy.ReverseProxied(app.wsgi_app) # Back port 'equalto' to older version of jinja2 app.jinja_env.tests.setdefault("equalto", lambda value, other: value == other) # Import the application from pagure.api import API # noqa: E402 app.register_blueprint(API) from pagure.ui import UI_NS # noqa: E402 app.register_blueprint(UI_NS) from pagure.internal import PV # noqa: E402 app.register_blueprint(PV) themename = pagure_config.get("THEME", "default") here = os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)))) themeblueprint = flask.Blueprint( "theme", __name__, static_url_path="/theme/static", static_folder=os.path.join(here, "themes", themename, "static"), ) # Jinja can be told to look for templates in different folders # That's what we do here template_folders = os.path.join( app.root_path, app.template_folder, os.path.join(here, "themes", themename, "templates"), ) import jinja2 # Jinja looks for the template in the order of the folders specified templ_loaders = [ jinja2.FileSystemLoader(template_folders), app.jinja_loader, ] app.jinja_loader = jinja2.ChoiceLoader(templ_loaders) app.register_blueprint(themeblueprint) app.before_request(set_request) app.teardown_request(end_request) if perfrepo: # Do this at the very end, so that this after_request comes last. app.after_request(perfrepo.print_stats) app.add_url_rule("/login/", view_func=auth_login, methods=["GET", "POST"]) app.add_url_rule("/logout/", view_func=auth_logout) return app
def create_app(config=None): """ Create the flask application. """ app = MultiStaticFlask(__name__) app.config = pagure_config if config: app.config.update(config) if app.config.get('SESSION_TYPE', None) is not None: import flask_session flask_session.Session(app) logging.basicConfig() logging.config.dictConfig(app.config.get('LOGGING') or {'version': 1}) app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True if perfrepo: # Do this as early as possible. # We want the perfrepo before_request to be the very first thing # to be run, so that we can properly setup the stats before the # request. app.before_request(perfrepo.reset_stats) if pagure_config.get('THEME_TEMPLATE_FOLDER', False): # Jinja can be told to look for templates in different folders # That's what we do here template_folder = pagure_config['THEME_TEMPLATE_FOLDER'] if template_folder[0] != '/': template_folder = os.path.join(app.root_path, app.template_folder, template_folder) import jinja2 # Jinja looks for the template in the order of the folders specified templ_loaders = [ jinja2.FileSystemLoader(template_folder), app.jinja_loader, ] app.jinja_loader = jinja2.ChoiceLoader(templ_loaders) if pagure_config.get('THEME_STATIC_FOLDER', False): static_folder = pagure_config['THEME_STATIC_FOLDER'] if static_folder[0] != '/': static_folder = os.path.join(app.root_path, 'static', static_folder) # Unlike templates, to serve static files from multiples folders we # need flask-multistatic app.static_folder = [ static_folder, os.path.join(app.root_path, 'static'), ] auth = pagure_config.get('PAGURE_AUTH', None) if auth in ['fas', 'openid']: # Only import and set flask_fas_openid if it is needed from pagure.ui.fas_login import FAS FAS.init_app(app) elif auth == 'oidc': # Only import and set flask_fas_openid if it is needed from pagure.ui.oidc_login import oidc, fas_user_from_oidc oidc.init_app(app) app.before_request(fas_user_from_oidc) # Report error by email if not app.debug and not pagure_config.get('DEBUG', False): app.logger.addHandler( pagure.mail_logging.get_mail_handler( smtp_server=pagure_config.get('SMTP_SERVER', '127.0.0.1'), mail_admin=pagure_config.get('MAIL_ADMIN', pagure_config['EMAIL_ERROR']), from_email=pagure_config.get('FROM_EMAIL', '*****@*****.**'))) # Support proxy app.wsgi_app = pagure.proxy.ReverseProxied(app.wsgi_app) # Back port 'equalto' to older version of jinja2 app.jinja_env.tests.setdefault('equalto', lambda value, other: value == other) # Import the application from pagure.api import API # noqa: E402 app.register_blueprint(API) from pagure.ui import UI_NS # noqa: E402 app.register_blueprint(UI_NS) from pagure.internal import PV # noqa: E402 app.register_blueprint(PV) app.before_request(set_request) app.teardown_request(end_request) # Only import the login controller if the app is set up for local login if pagure_config.get('PAGURE_AUTH', None) == 'local': import pagure.ui.login as login app.before_request(login._check_session_cookie) app.after_request(login._send_session_cookie) if perfrepo: # Do this at the very end, so that this after_request comes last. app.after_request(perfrepo.print_stats) app.add_url_rule('/login/', view_func=auth_login, methods=['GET', 'POST']) app.add_url_rule('/logout/', view_func=auth_logout) return app
def create_app(config=None): """ Create the flask application. """ app = flask.Flask(__name__) app.config = pagure_config if config: app.config.update(config) if app.config.get("SESSION_TYPE", None) is not None: import flask_session flask_session.Session(app) pagure.utils.set_up_logging(app=app) @app.errorhandler(500) def fatal_error(error): # pragma: no cover """500 Fatal Error page""" logger.exception("Error while processing request") return flask.render_template("fatal_error.html", error=error), 500 app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True if perfrepo: # Do this as early as possible. # We want the perfrepo before_request to be the very first thing # to be run, so that we can properly setup the stats before the # request. app.before_request(perfrepo.reset_stats) auth = pagure_config.get("PAGURE_AUTH", None) if auth in ["fas", "openid"]: # Only import and set flask_fas_openid if it is needed from pagure.ui.fas_login import FAS FAS.init_app(app) elif auth == "oidc": # Only import and set flask_fas_openid if it is needed from pagure.ui.oidc_login import oidc, fas_user_from_oidc oidc.init_app(app) app.before_request(fas_user_from_oidc) if auth == "local": # Only import the login controller if the app is set up for local login import pagure.ui.login as login app.before_request(login._check_session_cookie) app.after_request(login._send_session_cookie) # Support proxy app.wsgi_app = pagure.proxy.ReverseProxied(app.wsgi_app) # Back port 'equalto' to older version of jinja2 app.jinja_env.tests.setdefault("equalto", lambda value, other: value == other) # Import the application from pagure.api import API # noqa: E402 app.register_blueprint(API) from pagure.ui import UI_NS # noqa: E402 app.register_blueprint(UI_NS) from pagure.internal import PV # noqa: E402 app.register_blueprint(PV) # Import 3rd party blueprints plugin_config = flask.config.Config("") if "PAGURE_PLUGIN" in os.environ: # Warn the user about deprecated variable (defaults to stderr) warnings.warn( "The environment variable PAGURE_PLUGIN is deprecated and will be " "removed in future releases of Pagure. Please replace it with " "PAGURE_PLUGINS_CONFIG instead.", FutureWarning, ) # Log usage of deprecated variable logger.warning("Using deprecated variable PAGURE_PLUGIN. " "You should use PAGURE_PLUGINS_CONFIG instead.") plugin_config.from_envvar("PAGURE_PLUGIN") elif "PAGURE_PLUGINS_CONFIG" in os.environ: plugin_config.from_envvar("PAGURE_PLUGINS_CONFIG") elif "PAGURE_PLUGINS_CONFIG" in app.config: # If the os.environ["PAGURE_PLUGINS_CONFIG"] is not set, we try to load # it from the pagure config file. plugin_config.from_pyfile(app.config.get("PAGURE_PLUGINS_CONFIG")) for blueprint in plugin_config.get("PLUGINS") or []: logger.info("Loading blueprint: %s", blueprint.name) app.register_blueprint(blueprint) themename = pagure_config.get("THEME", "default") here = os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)))) themeblueprint = flask.Blueprint( "theme", __name__, static_url_path="/theme/static", static_folder=os.path.join(here, "themes", themename, "static"), ) # Jinja can be told to look for templates in different folders # That's what we do here template_folders = os.path.join( app.root_path, app.template_folder, os.path.join(here, "themes", themename, "templates"), ) import jinja2 # Jinja looks for the template in the order of the folders specified templ_loaders = [ jinja2.FileSystemLoader(template_folders), app.jinja_loader, ] app.jinja_loader = jinja2.ChoiceLoader(templ_loaders) app.register_blueprint(themeblueprint) # Setup WhiteNoise for serving static files app.wsgi_app = WhiteNoise(app.wsgi_app, root=os.path.join(here, "static"), prefix="/static") app.before_request(set_request) app.after_request(after_request) app.teardown_request(end_request) if perfrepo: # Do this at the very end, so that this after_request comes last. app.after_request(perfrepo.print_stats) app.add_url_rule("/login/", view_func=auth_login, methods=["GET", "POST"]) app.add_url_rule("/logout/", view_func=auth_logout) return app