Example #1
0
def _login_json_aspect(func, *args, **kwargs):
    """
    CherryPy-specific session login aspect.

    """
    _message = cherrypy.request.json

    if _message is not None and isinstance(_message, dict):
        # TODO: Add a hook for custom login

        if "credentials" not in _message:
            raise AuthenticationError("_login_json_aspect for \"" + func.__name__ + "\": No credentials in request")

        # Already logged in, log out old session
        if "session_id" in cherrypy.request.cookie and cherrypy.request.cookie["session_id"].value != "":
            print("_login_json_aspect: Logging out old session: " + cherrypy.request.cookie["session_id"].value)
            logout(cherrypy.request.cookie["session_id"].value)


        _session_id, _user = login(_message["credentials"])
        _session_cookie = cherrypy.response.cookie
        _session_cookie["session_id"] = _session_id
        _session_cookie["session_id"]["secure"] = True
        _session_cookie["session_id"]["path"] = "/"

        kwargs["_message"] = _message
        kwargs["_session_id"] = _session_id
        kwargs["_user"] = _user
        return func(*args, **kwargs)
    else:
        raise AuthenticationError("Authentication aspect for \"" + func.__name__ + "\": No message structure")
Example #2
0
def cherrypy_logout(_session_id):
    """
    CherryPy-specific logout

    :param _session_id: The session id of the session that is to be logged out
    :return:
    """
    logout(_session_id)

    _new_cookie = SimpleCookie()
    _new_cookie["session_id"] = ""
    _new_cookie["session_id"]['expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
    return _new_cookie