def post(self, username): # right now a nickname and username are the same, however that will change if username != None and len(username) != 0: user = find_user_by_username(username) nickname = username if user == None: logging.info("adding user %s." % (username)) user = add_user(User(username=username, nickname=nickname)) else: nickname = user.nickname logging.info("logging in user %s." % (username)) ## respond to the client our success self.set_status(200) self.set_cookie( "username", username.encode("utf-8"), self.application.cookie_secret, domain="spotichat.com" ) self.set_cookie("nickname", nickname, domain="spotichat.com") self.add_to_payload("message", nickname + " has entered the chat room") self.add_to_payload("username", cookie_encode(username.encode("utf-8"), self.application.cookie_secret)) self.add_to_payload("nickname", nickname) else: ## let the client know we failed because they didn't ask nice self.set_status(403, "missing username argument") return self.render()
def post(self, username): # right now a nickname and username are the same, however that will change if username != None and len(username) != 0: user = find_user_by_username(username) nickname = username if user == None: logging.info("adding user %s." % (username)) user = add_user(User(username=username, nickname=nickname)) else: nickname = user.nickname logging.info("logging in user %s." % (username)) ## respond to the client our success self.set_status(200) self.set_cookie('username', username.encode('utf-8'), self.application.cookie_secret, domain="spotichat.com") self.set_cookie('nickname', nickname, domain="spotichat.com") self.add_to_payload('message', nickname + ' has entered the chat room') self.add_to_payload( 'username', cookie_encode(username.encode('utf-8'), self.application.cookie_secret)) self.add_to_payload('nickname', nickname) else: ## let the client know we failed because they didn't ask nice self.set_status(403, 'missing username argument') return self.render()
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)