def __create_session(roles, expire_in_secs, tenant_name): # current local time current_datetime = datetime.now() # How long session lasts expiration_datetime = datetime.now() + timedelta(seconds=expire_in_secs) # create session SAML Response session = Session() # make a UUID based on the host ID and current time __session_id = str(uuid.uuid4()) session.set_session_id(__session_id) session.set_expiration(expiration_datetime) session.set_last_access(current_datetime) # set session rolerelations relations = [] for role in roles: # This creates State Level permission relations.append( RoleRelation(role, tenant_name, get_state_code_mapping([tenant_name])[0], None, None)) session.set_user_context(relations) # set user __uid = str(uuid.uuid4()) session.set_uid(__uid) # save current session get_session_backend().create_new_session(session, overwrite_timeout=True) return session
def update_session_access(session): ''' update_session user_session.last_access ''' current_time = datetime.now() session.set_last_access(current_time) get_session_backend().update_session(session)
def create_new_user_session(user_info_response, name_id, session_index, identity_parser_class, session_expire_after_in_secs=30): ''' Create new user session from SAMLResponse ''' # current local time current_datetime = datetime.now() # How long session lasts expiration_datetime = current_datetime + timedelta(seconds=session_expire_after_in_secs) # create session session = identity_parser_class.create_session(name_id, session_index, user_info_response, current_datetime, expiration_datetime) session.set_expiration(expiration_datetime) session.set_last_access(current_datetime) get_session_backend().create_new_session(session) return session
def test_flush_session(self): session = Session() session.set_session_id('1a2b3c') backend = get_session_backend() backend.create_new_session(session) session = backend.get_session('1a2b3c') self.assertIsNotNone(session) cache_flush_session() self.assertIsNone(backend.get_session('1a2b3c'))
def create_test_session(roles=[], uid='dummy', tenant=None, full_name='Dummy User', expiration=None, last_access=None, idpSessionIndex='123', first_name='Dummy', last_name='User', name_id='abc', save_to_backend=False, email='foo@foocom'): # Prepare session session_id = str(uuid.uuid1()) current_datetime = datetime.now() if not expiration: expiration = current_datetime + timedelta(seconds=30) if not last_access: last_access = current_datetime session = Session() session.set_session_id(session_id) session.set_last_access(current_datetime) session.set_expiration(expiration) relations = [] if tenant is None: tenant = get_unittest_tenant_name() for role in roles: relations.append(RoleRelation(role, tenant, None, None, None)) session.set_user_context(relations) session.set_uid(uid) session.set_email(email) session.set_fullName(full_name) session.set_firstName(first_name) session.set_lastName(last_name) session.set_name_id(name_id) session.set_idp_session_index(idpSessionIndex) if save_to_backend: get_session_backend().create_new_session(session) return session
def expire_session(session_id): ''' expire session by session_id ''' session = get_user_session(session_id) current_time = datetime.now() if session is not None: # Expire the entry session.set_expiration(current_time) __backend = get_session_backend() __backend.update_session(session) # Delete the session __backend.delete_session(session_id)
def create_new_user_session(user_info_response, name_id, session_index, identity_parser_class, session_expire_after_in_secs=30): ''' Create new user session from SAMLResponse ''' # current local time current_datetime = datetime.now() # How long session lasts expiration_datetime = current_datetime + timedelta( seconds=session_expire_after_in_secs) # create session session = identity_parser_class.create_session(name_id, session_index, user_info_response, current_datetime, expiration_datetime) session.set_expiration(expiration_datetime) session.set_last_access(current_datetime) get_session_backend().create_new_session(session) return session
def __create_session(roles, expire_in_secs, tenant_name): # current local time current_datetime = datetime.now() # How long session lasts expiration_datetime = datetime.now() + timedelta(seconds=expire_in_secs) # create session SAML Response session = Session() # make a UUID based on the host ID and current time __session_id = str(uuid.uuid4()) session.set_session_id(__session_id) session.set_expiration(expiration_datetime) session.set_last_access(current_datetime) # set session rolerelations relations = [] for role in roles: # This creates State Level permission relations.append(RoleRelation(role, tenant_name, get_state_code_mapping([tenant_name])[0], None, None)) session.set_user_context(relations) # set user __uid = str(uuid.uuid4()) session.set_uid(__uid) # save current session get_session_backend().create_new_session(session, overwrite_timeout=True) return session
def get_user_session(session_id): ''' get user session from DB if user session does not exist, then return None ''' return get_session_backend().get_session(session_id)
def test_pdf_batch_user_login(self): (cookie_name, cookie_value) = batch_user_session.create_batch_user_session(TestPdfSession.settings, TestPdfSession.roles, 'test') session_id = self.get_session_id(cookie_name, cookie_value) session = session_backend.get_session_backend().get_session(session_id) self.assertIsNotNone(session)