def store_session(metadata): """ Store new session and metadata :param metadata: metadata parsed from jwt token """ # also clear the secure cookie data cookie_session.clear() # get the hashed user id for eq id_generator = current_app.eq['id_generator'] user_id = id_generator.generate_id(metadata) user_ik = id_generator.generate_ik(metadata) eq_session_id = str(uuid4()) # store the user ik and es_session_id in the cookie cookie_session[USER_IK] = user_ik cookie_session[EQ_SESSION_ID] = eq_session_id session_data = _create_session_data_from_metadata(metadata) create_session_store(eq_session_id, user_id, user_ik, session_data) questionnaire_store = get_questionnaire_store(user_id, user_ik) questionnaire_store.metadata = metadata questionnaire_store.add_or_update() # Set the user in Flask such that anyone calling current_user for the duration of this request doesn't have to # load it from the db. login_user(User(user_id, user_ik)) logger.info('user authenticated')
def load_user(): """ Checks for the present of the JWT in the users sessions :return: A user object if a JWT token is available in the session """ session_store = get_session_store() if session_store and session_store.user_id and _is_session_valid( session_store): logger.debug('session exists') user_id = session_store.user_id user_ik = cookie_session.get(USER_IK) user = User(user_id, user_ik) if session_store.session_data.tx_id: logger.bind(tx_id=session_store.session_data.tx_id) _extend_session_expiry(session_store) return user logger.info('session does not exist') cookie_session.pop(USER_IK, None) return None
def load_user(extend_session: bool = True) -> Optional[User]: """ Checks for the present of the JWT in the users sessions :return: A user object if a JWT token is available in the session :param extend_session: bool, whether to extend the session """ session_store = get_session_store() if session_store and _is_session_valid(session_store): logger.debug("session exists") user_id = session_store.user_id user_ik = cookie_session.get(USER_IK) user = User(user_id, user_ik) if session_store.session_data and session_store.session_data.tx_id: logger.bind(tx_id=session_store.session_data.tx_id) if extend_session: _extend_session_expiry(session_store) return user logger.info("session does not exist") cookie_session.pop(USER_IK, None)
def check_session(): """ Checks for the present of the JWT in the users sessions :return: A user object if a JWT token is available in the session """ logger.debug("Checking for session") if session_storage.has_user_id(): user = User(session_storage.get_user_id(), session_storage.get_user_ik()) questionnaire_store = get_questionnaire_store(user.user_id, user.user_ik) metadata = questionnaire_store.metadata logger.debug("Session token exists for tx_id=%s", metadata["tx_id"]) return user else: logging.info("Session does not have an authenticated token") return None
def load_user(): """ Checks for the present of the JWT in the users sessions :return: A user object if a JWT token is available in the session """ user_id = current_app.eq['session_storage'].get_user_id() if user_id: user = User(user_id, current_app.eq['session_storage'].get_user_ik()) questionnaire_store = get_questionnaire_store(user.user_id, user.user_ik) metadata = questionnaire_store.metadata if metadata: logger.bind(tx_id=metadata["tx_id"]) logger.debug("session token exists") return user else: logger.info("session does not have an authenticated token") return None
def test_get_user_ik(self): with self.application.test_request_context(): user = User("1", "2") self.assertEqual("2", user.user_ik)
def _get_user(decrypted_token): id_generator = current_app.eq['id_generator'] user_id = id_generator.generate_id(decrypted_token) user_ik = id_generator.generate_ik(decrypted_token) return User(user_id, user_ik)
def _get_user(response_id): id_generator = current_app.eq["id_generator"] user_id = id_generator.generate_id(response_id) user_ik = id_generator.generate_ik(response_id) return User(user_id, user_ik)
def test_none_user(self): with self.application.test_request_context(): with self.assertRaises(ValueError): User(None, "2")
def test_negative_user(self): with self.application.test_request_context(): user = User("-1", "2") self.assertEqual("-1", user.user_id)
def test_no_user(): with pytest.raises(ValueError): User("", "")
def test_negative_user(self): with self.application.test_request_context(): user = User('-1', '2') self.assertEqual('-1', user.user_id)
def test_get_user_ik(self): with self.application.test_request_context(): user = User('1', '2') self.assertEqual('2', user.user_ik)
def test_get_user_id(): user = User("1", "2") assert "1" == user.user_id
def test_none_user(): with pytest.raises(ValueError): User(None, "2")
def _get_user(decrypted_token): user_id = UserIDGenerator.generate_id(decrypted_token) user_ik = UserIDGenerator.generate_ik(decrypted_token) return User(user_id, user_ik)
def test_negative_user(): user = User("-1", "2") assert "-1" == user.user_id