def prepare(self): """get our user from the request and set to self.current_user""" self.current_user = None self.username = None self.channel = None self.channel_id = None # self._url_args only has a list # we need a dictonary with the named parameters # so, we reparse the url try: print self._url_args self.username = self.get_cookie("username", None, self.application.cookie_secret) if self.username != None: logging.info("username from cookie: %s " % (self.username)) if hasattr(self, "_url_args") and "channel_id" in self._url_args: self.channel_id = unquote(self._url_args["channel_id"]).decode("utf-8") logging.info("channel_id from url_args: %s " % (self.channel_id)) if self.channel_id == None: self.channel_id = self.get_argument("channel_id", "public") logging.info("channel_id from arguments: %s " % (self.channel_id)) self.channel = get_chat_channel(redis_server, self.channel_id) if self.username == None: self.username = self.get_argument("username", None) if self.username != None: logging.info("username from arguments: %s " % (self.username)) self.username = cookie_decode(self.username, self.application.cookie_secret) if self.username == None: if hasattr(self, "_url_args") and "username" in self._url_args: self.username = unquote(self._url_args["username"]).decode("utf-8") logging.info("username from url_args: %s " % (self.username)) if self.username != None and self.channel != None: self.username = unescape(self.username) # user = self.channel.find_user_by_username(self.username) user = find_user_by_username(self.username) if user != None: self.current_user = self.channel.update_user_timestamp(user) logging.info("set current user %s" % self.current_user.username) except Exception: raise self.headers["Access-Control-Allow-Origin"] = "sp://spotichat" self.headers["Access-Control-Allow-Credentials"] = "true" self.headers[ "Access-Control-Allow-Headers" ] = "Origin, Content-Type, User-Agent, Accept, Cache-Control, Pragma, Set-Cookie" self.headers["Access-Control-Allow-Methods"] = "POST, GET, DELETE, OPTIONS"
def test_cookie_handling(self): # set our cookie key and values cookie_key = 'my_key' cookie_value = 'my_secret' # encode our cookie encoded_cookie = cookie_encode(cookie_value, cookie_key) # Make sure we do not contain our value (i.e. we are really encrypting) self.assertEqual(encoded_cookie.find(cookie_value) == -1, True) # Make sure we are an encoded cookie using the function self.assertEqual(cookie_is_encoded(encoded_cookie), True) # Make sure after decoding our cookie we are the same as the unencoded cookie decoded_cookie_value = cookie_decode(encoded_cookie, cookie_key) self.assertEqual(decoded_cookie_value, cookie_value)
def prepare(self): """get our user from the request and set to self.current_user""" self.current_user = None self.username = None self.channel = None self.channel_id = None # self._url_args only has a list # we need a dictonary with the named parameters # so, we reparse the url try: print self._url_args self.username = self.get_cookie('username', None, self.application.cookie_secret) if self.username != None: logging.info("username from cookie: %s " % (self.username)) if hasattr(self, '_url_args') and 'channel_id' in self._url_args: self.channel_id = unquote( self._url_args['channel_id']).decode('utf-8') logging.info("channel_id from url_args: %s " % (self.channel_id)) if self.channel_id == None: self.channel_id = self.get_argument('channel_id', 'public') logging.info("channel_id from arguments: %s " % (self.channel_id)) self.channel = get_chat_channel(redis_server, self.channel_id) if self.username == None: self.username = self.get_argument('username', None) if self.username != None: logging.info("username from arguments: %s " % (self.username)) self.username = cookie_decode( self.username, self.application.cookie_secret) if self.username == None: if hasattr(self, '_url_args') and 'username' in self._url_args: self.username = unquote( self._url_args['username']).decode('utf-8') logging.info("username from url_args: %s " % (self.username)) if self.username != None and self.channel != None: self.username = unescape(self.username) # user = self.channel.find_user_by_username(self.username) user = find_user_by_username(self.username) if user != None: self.current_user = self.channel.update_user_timestamp( user) logging.info("set current user %s" % self.current_user.username) except Exception: raise self.headers['Access-Control-Allow-Origin'] = 'sp://spotichat' self.headers['Access-Control-Allow-Credentials'] = 'true' self.headers[ 'Access-Control-Allow-Headers'] = 'Origin, Content-Type, User-Agent, Accept, Cache-Control, Pragma, Set-Cookie' self.headers[ 'Access-Control-Allow-Methods'] = 'POST, GET, DELETE, OPTIONS'