def POST(self, accesstoken): graph_url = "https://graph.facebook.com/debug_token?input_token={0}&access_token={1}".format(accesstoken, CF_FACEBOOK_APPTOKEN) try: debug_data = urllib2.urlopen(graph_url).read() json_data = json.loads(debug_data) token_data = json_data['data'] #print token_data is_valid = token_data['is_valid'] if is_valid: app_id = str(token_data['app_id']) fb_id = str(token_data['user_id']) if fb_id and (app_id == CF_FACEBOOK_APPID): username = CF_FACEBOOK_PREFIX + fb_id username_full = username if not self._userinfo_db.exist_user(username_full): #create facebook account here self._userinfo_db.create_user(username_full) session_id = self._session_db.create_token(username) if session_id: return to_json({'result':0, 'authenticated_token': session_id, 'userid': username, 'description': 'Token is created successfully.'}) except: return to_json({'result':1, 'description': 'Error.'}) return to_json({'result':1, 'description': 'Error.'})
def POST( self, username, password, email, phone=None, recaptcha_challenge_field=None, recaptcha_response_field=None ): username = username.lower() full_username = "******".format(username, XMPP_DOMAIN_NAME) """check captcha""" if "X-Real-IP" in cherrypy.request.headers: ip_remote = cherrypy.request.headers["X-Real-IP"] else: ip_remote = cherrypy.request.remote.ip if self.check_captcha: check = self.check_captcha.verify_captcha(recaptcha_challenge_field, recaptcha_response_field, ip_remote) if not check: return to_json({"result": 2, "description": "Wrong captcha."}) """register""" if self.check_register_value(username, password, email, phone): avatar_random = random.randint(0, 24) created_auth = self._auth_db.create_user(username, password, email, phone) # create a userinfo record created_info = self._userinfo_db.create_user(full_username, avatar_id=avatar_random) verification_code = random_string_generator(12) verified = self.send_verification_email(username, email, verification_code) if created_auth and created_info: return to_json({"result": 0, "description": "Registration is successful."}) else: return to_json({"result": 4, "description": "Duplicate username or email."}) else: return to_json({"result": 3, "description": "Invalid input value."}) return to_json({"result": 1, "description": "Registration error."})
def GET(self, turingtestid=None): captcha_con = self._captcha_database #captcha_con = Captcha_Session_Connector() if 'X-Real-IP' in cherrypy.request.headers: ip_remote = cherrypy.request.headers['X-Real-IP'] else: ip_remote = cherrypy.request.remote.ip if not turingtestid: self._captcha_manager.update_captcha_count(ip_remote) if self._captcha_manager.get_captcha_id(ip_remote) != CAPTCHA_DEFAULT: return to_json({'result': 3,'description':'Please use recaptcha.'}) captcha_card_image = CaptchaCardImage(length=3) captcha_image = captcha_card_image.get_image() captcha_value = captcha_card_image.get_encode_string_values() captcha_insert = captcha_con.create_captcha(captcha_image, captcha_value) if captcha_insert: result = {'result': 0,'turingtestid': captcha_insert['turingtestid'], 'description':'Successful.'} return to_json(result) else: return to_json({'result': 9,'description':'Unknown error.'}) else: result = captcha_con.get_captcha(turingtestid) #buffering = '' if result: data = result['image'] cherrypy.response.headers['Content-Type'] = "image/jpeg" #return file_generator(data) return str(data) else: return 'Error'
def POST(self,username, oldpassword, newpassword): username = username.lower() if not REGEX_PASSWORD.match(newpassword): return to_json({'result': 2, 'description': 'Password can contain from 6 to 32 characters.'}) res = self._database.change_password(username, oldpassword, newpassword) if res: return to_json({'result': 0, 'description': 'Password changed.'}) else: return to_json({'result': 1, 'description': 'Changing password failed.'})
def POST(self, username, password): username = username.lower() if self._auth_db.authenticate_user(username, password): session_id = self._session_db.create_token(username) if session_id: return to_json({'result':0, 'authenticated_token': session_id, 'userid': username, 'description': 'Token is created successfully.'}) return to_json({'result':1, 'description': 'Error.'})
def POST(self, email, password): email = email.lower() authenticated = self._auth_db.authenticate_email(email, password) if authenticated: username = authenticated['username'] session_id = self._session_db.create_token(username) if session_id: return to_json({'result':0, 'authenticated_token': session_id, 'userid': username, 'description': 'Token is created successfully.'}) return to_json({'result':1, 'description': 'Error.'})
def POST(self, session_token, recaptcha_challenge_field=None, recaptcha_response_field=None): authenticated = self._session_db.get_token(session_token) if not authenticated: return to_json({'result': 3, 'description': 'Invalid session.'}) if authenticated: username = authenticated['username'] verification_code = random_string_generator(12) email = self._auth_db.exist_user(username)['email'] res = self.send_verification_email(username, email, verification_code) if res: return to_json({'result': 0, 'description': 'Request is successful.'}) return to_json({'result': 1, 'description': 'Request failed.'})
def POST(self, email, resetcode, newpassword, recaptcha_challenge_field=None, recaptcha_response_field=None): if 'X-Real-IP' in cherrypy.request.headers: ip_remote = cherrypy.request.headers['X-Real-IP'] else: ip_remote = cherrypy.request.remote.ip if self.check_captcha: check = self.check_captcha.verify_captcha(recaptcha_challenge_field, recaptcha_response_field,ip_remote) if not check: return to_json({'result': 2, 'description': 'Wrong captcha.'}) #print username email = email.lower() res = self._database.reset_password(email, resetcode, newpassword) if res: return to_json({'result': 0, 'description': 'Password changed.'}) return to_json({'result': 1, 'description': 'Reset password failed.'})
def captcha_validate(captcha_manager, recaptcha_challenge_field, recaptcha_response_field, ip_remote): captcha_valid = None if captcha_manager: if (not recaptcha_challenge_field) or (not recaptcha_response_field): """need captcha""" return to_json({'result': 2, 'description': 'Captcha is required.',}) captcha_challenge = recaptcha_challenge_field captcha_response = recaptcha_response_field #captcha = captcha_manager.get_captcha(ip_remote) captcha_valid = captcha_manager.verify_captcha(captcha_challenge, captcha_response, ip_remote) if not captcha_valid: return to_json({'result': 3, 'description': 'Wrong captcha.'}) return captcha_valid
def POST(self, email, recaptcha_challenge_field=None, recaptcha_response_field=None): if 'X-Real-IP' in cherrypy.request.headers: ip_remote = cherrypy.request.headers['X-Real-IP'] else: ip_remote = cherrypy.request.remote.ip if self.check_captcha: check = self.check_captcha.verify_captcha(recaptcha_challenge_field, recaptcha_response_field,ip_remote) if not check: return to_json({'result': 2, 'description': 'Wrong captcha.'}) reset_code = random_string_generator(12) email = email.lower() res = self.send_reset_password_email(email, reset_code) if res: return to_json({'result': 0, 'description': 'Reset password email sent.'}) return to_json({'result': 1, 'description': 'Request resetting password failed.'})
def GET(self, verification_code=None, username=None): if verification_code and username: res = self._database.verify_email(verification_code, username) if res: return to_json({'result': 0, 'description': 'Verification is successful.'}) else: return to_json({'result': 1, 'description': 'Verification failed.'}) else: form_html = '<html><head><title>Verify Email</title></head><body>'+\ '<form name="input" method="get">'+\ 'Username: <input type="text" name="username" />'+\ 'Code: <input type="text" name="verification_code" />'+\ '<input type="submit" value="Submit" />'+\ '</form>' +\ '</body></html>' return form_html
def POST(self, email, password, guestid=None): email = email.lower() """register""" guest_user = None if guestid: guest_user = self._guest_auth_db.exist_user(guestid) if not guest_user: return to_json({'result': 5, 'description': 'Invalid guest ID.'}) else: guest_user = self._guest_userinfo_db.exist_user(guestid) if self.check_register_value(password, email): avatar_random = random.randint(0,24) created_auth = self._auth_db.create_user_by_email(email, password) #create a userinfo record created_info = None if created_auth: init_money = 0 free_money = CF_FREE_MONEY nickname = 'ThanBai' if guest_user: init_money = guest_user['money'] free_money = init_money + (CF_FREE_MONEY - CF_GUEST_FREE_MONEY) nickname = guest_user['nickname'] #full_name = "{}@{}".format(created_auth["username"],XMPP_DOMAIN_NAME) created_info = self._userinfo_db.create_user(created_auth["username"], free_money = free_money, avatar_id = avatar_random, nickname = nickname) #print created_auth #verification_code = random_string_generator(12) #verified = self.send_verification_email(username, email, verification_code) if created_auth and created_info: if guest_user: self._guest_auth_db.remove_user(guestid) self._guest_userinfo_db.remove_user(guestid) return to_json({'result': 0, 'description': 'Registration is successful.'}) else: return to_json({'result': 4, 'description': 'Duplicate email.'}) else: return to_json({'result': 3, 'description': 'Invalid input value.'}) return to_json({'result': 1, 'description': 'Registration error.'})
def GET(self): return to_json({'message':'Welcome to Cacafefe\'s services.'})