2023-02-26 21:24:07 +01:00
|
|
|
"""
|
|
|
|
flask_cas.__init__
|
|
|
|
"""
|
|
|
|
|
|
|
|
import flask
|
|
|
|
from flask import current_app
|
|
|
|
|
|
|
|
from . import routing
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
2023-02-28 21:40:50 +01:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
class CAS(object):
|
|
|
|
"""
|
|
|
|
Required Configs:
|
|
|
|
|
|
|
|
|Key |
|
|
|
|
|----------------|
|
|
|
|
|CAS_SERVER |
|
|
|
|
|CAS_AFTER_LOGIN |
|
|
|
|
|
|
|
|
Optional Configs:
|
|
|
|
|
|
|
|
|Key | Default |
|
|
|
|
|---------------------------|-----------------------|
|
|
|
|
|CAS_TOKEN_SESSION_KEY | _CAS_TOKEN |
|
|
|
|
|CAS_USERNAME_SESSION_KEY | CAS_USERNAME |
|
|
|
|
|CAS_ATTRIBUTES_SESSION_KEY | CAS_ATTRIBUTES |
|
|
|
|
|CAS_LOGIN_ROUTE | '/cas' |
|
|
|
|
|CAS_LOGOUT_ROUTE | '/cas/logout' |
|
|
|
|
|CAS_VALIDATE_ROUTE | '/cas/serviceValidate'|
|
|
|
|
|CAS_AFTER_LOGOUT | None |
|
|
|
|
"""
|
|
|
|
|
2023-02-28 21:40:50 +01:00
|
|
|
def __init__(self, app=None, url_prefix=None, configuration_function=None):
|
2023-02-26 21:24:07 +01:00
|
|
|
self._app = app
|
|
|
|
if app is not None:
|
2023-02-28 21:40:50 +01:00
|
|
|
self.init_app(app, url_prefix, configuration_function)
|
2023-02-26 21:24:07 +01:00
|
|
|
|
2023-02-28 21:40:50 +01:00
|
|
|
def init_app(self, app, url_prefix=None, configuration_function=None):
|
2023-02-26 21:24:07 +01:00
|
|
|
# Configuration defaults
|
2023-02-28 21:40:50 +01:00
|
|
|
app.config.setdefault("CAS_TOKEN_SESSION_KEY", "_CAS_TOKEN")
|
|
|
|
app.config.setdefault("CAS_USERNAME_SESSION_KEY", "CAS_USERNAME")
|
|
|
|
app.config.setdefault("CAS_ATTRIBUTES_SESSION_KEY", "CAS_ATTRIBUTES")
|
|
|
|
app.config.setdefault("CAS_LOGIN_ROUTE", "/cas")
|
|
|
|
app.config.setdefault("CAS_LOGOUT_ROUTE", "/cas/logout")
|
|
|
|
app.config.setdefault("CAS_VALIDATE_ROUTE", "/cas/serviceValidate")
|
|
|
|
app.config.setdefault("CAS_CONFIGURATION_FUNCTION", configuration_function)
|
2023-02-26 21:24:07 +01:00
|
|
|
# Requires CAS 2.0
|
2023-02-28 21:40:50 +01:00
|
|
|
app.config.setdefault("CAS_AFTER_LOGOUT", None)
|
2023-02-26 21:24:07 +01:00
|
|
|
# Register Blueprint
|
|
|
|
app.register_blueprint(routing.blueprint, url_prefix=url_prefix)
|
|
|
|
|
|
|
|
# Use the newstyle teardown_appcontext if it's available,
|
|
|
|
# otherwise fall back to the request context
|
2023-02-28 21:40:50 +01:00
|
|
|
if hasattr(app, "teardown_appcontext"):
|
2023-02-26 21:24:07 +01:00
|
|
|
app.teardown_appcontext(self.teardown)
|
|
|
|
else:
|
|
|
|
app.teardown_request(self.teardown)
|
|
|
|
|
|
|
|
def teardown(self, exception):
|
2023-04-03 17:40:45 +02:00
|
|
|
pass # ctx = stack.top
|
2023-02-28 21:40:50 +01:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
@property
|
|
|
|
def app(self):
|
|
|
|
return self._app or current_app
|
|
|
|
|
|
|
|
@property
|
|
|
|
def username(self):
|
2023-02-28 21:40:50 +01:00
|
|
|
return flask.session.get(self.app.config["CAS_USERNAME_SESSION_KEY"], None)
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def attributes(self):
|
2023-02-28 21:40:50 +01:00
|
|
|
return flask.session.get(self.app.config["CAS_ATTRIBUTES_SESSION_KEY"], None)
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def token(self):
|
2023-02-28 21:40:50 +01:00
|
|
|
return flask.session.get(self.app.config["CAS_TOKEN_SESSION_KEY"], None)
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
def login():
|
2023-02-28 21:40:50 +01:00
|
|
|
return flask.redirect(flask.url_for("cas.login", _external=True))
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
def logout():
|
2023-02-28 21:40:50 +01:00
|
|
|
return flask.redirect(flask.url_for("cas.logout", _external=True))
|
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
|
|
|
|
def login_required(function):
|
|
|
|
@wraps(function)
|
|
|
|
def wrap(*args, **kwargs):
|
2023-02-28 21:40:50 +01:00
|
|
|
if "CAS_USERNAME" not in flask.session:
|
|
|
|
flask.session["CAS_AFTER_LOGIN_SESSION_URL"] = (
|
|
|
|
flask.request.script_root + flask.request.full_path
|
2023-02-26 21:24:07 +01:00
|
|
|
)
|
|
|
|
return login()
|
|
|
|
else:
|
|
|
|
return function(*args, **kwargs)
|
2023-02-28 21:40:50 +01:00
|
|
|
|
2023-02-26 21:24:07 +01:00
|
|
|
return wrap
|