def log_out(self): """ For when the session needs to kill itself. """ self.logger.debug( "Ending session for '%s' via admin.remove_session()." % self.User.user["login"]) admin.remove_session(self.session["_id"], "admin")
def __init__(self, params={}): """ Initialize a new Session object.""" self.logger = get_logger() # these are our session attributes. Declare them all here self.params = params self.session = None self.Settlement = None self.User = None self.set_cookie = False # # special session types # # we're not processing params yet, but if we have a log out request, we # do it here, while we're initializing a new session object. if "remove_session" in self.params: user = mdb.users.find_one({ "current_session": ObjectId(self.params["remove_session"].value) }) if user is not None: self.User = assets.User(user_id=user["_id"], session_object={"_id": 0}) self.User.mark_usage("signed out") if 'login' in self.params: admin.remove_session(self.params["remove_session"].value, self.params["login"].value) else: admin.remove_session(self.params["remove_session"].value, "webapp_error") # ok, if this is a recovery request, let's try to do that if 'recovery_code' in self.params: self.logger.info("Password Recovery Code sign-in initiated!") user = mdb.users.find_one( {'recovery_code': self.params["recovery_code"].value}) if user is None: self.logger.info( "Password Recovery Code not found (possibly expired). Aborting attempt." ) else: self.logger.info( "Rendering Password Recovery controls for '%s'" % user["login"]) login.render("reset", user["login"], self.params['recovery_code'].value) # # normal session types # # # initialize! # # 1.) try to set the session ID from the cookie self.cookie = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE")) if "session" in self.cookie: session_id = ObjectId(self.cookie['session'].value) else: session_id = None # 2.) determine if creds are present creds_present = False if 'login' in self.params and 'password' in self.params: creds_present = True # # do stuff! # # default sign in method; def sign_in(): """ Private DRYness method for quickly logging in with params. """ if 'login' in self.params and 'password' in self.params: self.AuthObject = login.AuthObject(self.params) self.User, self.session = self.AuthObject.authenticate() self.set_cookie = True if session_id is not None: self.session = mdb.sessions.find_one({"_id": session_id}) if self.session is None: sign_in() else: user_object = mdb.users.find_one( {"current_session": session_id}) self.User = assets.User(user_object["_id"], session_object=self) elif self.cookie is not None and 'Session' not in self.cookie.keys( ) and creds_present: sign_in() elif self.cookie is None and creds_present: sign_in() else: sign_in() # self.logger.error("Error attempting to process cookie!") # self.logger.error(self.cookie) if self.session is not None: if not api.check_token(self): # self.logger.debug("JWT Token expired! Attempting to refresh...") r = api.refresh_jwt_token(self) if r.status_code == 401: self.log_out() self.session = None