def _save_state(self, resp, context): """ Saves a state from context to cookie :type resp: satosa.response.Response :type context: satosa.context.Context :param resp: The response :param context: Session context """ if context.state.should_delete(): # Save empty state with a max age of 0 cookie = state_to_cookie(State(), self.config.COOKIE_STATE_NAME, "/", self.config.STATE_ENCRYPTION_KEY, 0) else: cookie = state_to_cookie(context.state, self.config.COOKIE_STATE_NAME, "/", self.config.STATE_ENCRYPTION_KEY) if isinstance(resp, Response): resp.add_cookie(cookie) else: try: resp.headers.append(tuple(cookie.output().split(": ", 1))) except: satosa_logging(LOGGER, logging.WARN, "can't add cookie to response '%s'" % resp.__class__, context.state) pass
def _save_state(self, resp, context): """ Saves a state from context to cookie :type resp: satosa.response.Response :type context: satosa.context.Context :param resp: The response :param context: Session context """ if context.state.should_delete(): # Save empty state with a max age of 0 cookie = state_to_cookie(State(), self.config.COOKIE_STATE_NAME, "/", self.config.STATE_ENCRYPTION_KEY, 0) else: cookie = state_to_cookie(context.state, self.config.COOKIE_STATE_NAME, "/", self.config.STATE_ENCRYPTION_KEY) if isinstance(resp, Response): resp.add_cookie(cookie) else: try: resp.headers.append(tuple(cookie.output().split(": ", 1))) except: satosa_logging( LOGGER, logging.WARN, "can't add cookie to response '%s'" % resp.__class__, context.state) pass
def test_state_to_cookie_produces_cookie_without_max_age_for_state_that_should_be_deleted(self): state_key = "27614gjkrn" saved_data = "data" state = State() state[state_key] = saved_data state.delete = True cookie_name = "state_cookie" path = "/" encrypt_key = "2781y4hef90" cookie = state_to_cookie(state, cookie_name, path, encrypt_key) cookie_str = cookie[cookie_name].OutputString() parsed_cookie = SimpleCookie(cookie_str) assert not parsed_cookie[cookie_name].value assert parsed_cookie[cookie_name]["max-age"] == '0'
def test_state_cookie(): """ Test that the state can be converted between cookie and state """ state_key = "27614gjkrn" saved_data = "data" state = State() state.add(state_key, saved_data) cookie_name = "state_cookie" path = "/" encrypt_key = "2781y4hef90" cookie = state_to_cookie(state, cookie_name, path, encrypt_key) cookie_str = cookie.output() loaded_state = cookie_to_state(cookie_str, cookie_name, encrypt_key) assert loaded_state.get(state_key) == saved_data
def test_encode_decode_of_state(self): """ Test that the state can be converted between cookie and state """ state_key = "27614gjkrn" saved_data = "data" state = State() state[state_key] = saved_data cookie_name = "state_cookie" path = "/" encrypt_key = "2781y4hef90" cookie = state_to_cookie(state, cookie_name, path, encrypt_key) cookie_str = cookie[cookie_name].OutputString() loaded_state = cookie_to_state(cookie_str, cookie_name, encrypt_key) assert loaded_state[state_key] == saved_data
def test_state_to_cookie_produces_cookie_without_max_age_for_state_that_should_be_deleted( self): state_key = "27614gjkrn" saved_data = "data" state = State() state[state_key] = saved_data state.delete = True cookie_name = "state_cookie" path = "/" encrypt_key = "2781y4hef90" cookie = state_to_cookie(state, cookie_name, path, encrypt_key) cookie_str = cookie[cookie_name].OutputString() parsed_cookie = SimpleCookie(cookie_str) assert not parsed_cookie[cookie_name].value assert parsed_cookie[cookie_name]["max-age"] == '0'