def test_vk_auth_after_normal_registartion(self): os.environ["VK_APP_SECRET_KEY"] = "12345" vk_id = 42 vk_concated_string = "blablabla=1&blabla=2" sig = md5(vk_concated_string.replace("&", "") + "12345") # Обычная регистрация, получаем токен: credentials = Credentials() credentials.phone = "+79263435016" credentials.password = "******" register_result = self.service.register(credentials) verification_code = register_result["verification"]["send_code"] self.service.verify_phone(credentials, verification_code) auth_result = self.service.authenticate(credentials) self.assertEqual(1, auth_result[0]) token = auth_result[1] # Используем токен и укажем vk_id: new_credentials = Credentials() new_credentials.token = token new_credentials.vk_id = vk_id vk_auth_result = self.service.authenticate_vk(new_credentials, vk_concated_string, sig) self.assertTrue(isinstance(vk_auth_result, tuple)) self.assertEqual(1, vk_auth_result[0]) self.assertEqual(token, vk_auth_result[1]) # Теперь можно и без токена: new_credentials = Credentials() new_credentials.vk_id = vk_id vk_auth_result = self.service.authenticate_vk(new_credentials, vk_concated_string, sig) self.assertTrue(isinstance(vk_auth_result, tuple)) self.assertEqual(1, vk_auth_result[0]) self.assertEqual(32, len(vk_auth_result[1])) self.assertNotEqual(token, vk_auth_result[1])
def test_vk_auth_fail(self): os.environ["VK_APP_SECRET_KEY"] = "12345" vk_id = 42 vk_concated_string = "blablabla=1&blabla=2" sig = md5(vk_concated_string.replace("&", "") + "12345") credentials = Credentials() credentials.vk_id = vk_id self.assertRaises( IncorrectOAuthSignature, self.service.authenticate_vk, credentials, vk_concated_string, sig+"1" )
def test_authenticate_vk(self): os.environ["VK_APP_SECRET_KEY"] = "12345" vk_id = 42 vk_concated_string = "blablabla=1&blabla=2" sig = md5(vk_concated_string.replace("&", "") + "12345") credentials = Credentials() credentials.vk_id = vk_id response = self.app.get( "/v1/authenticate_vk/", {"vk_id": "42", "vk_concated_string": vk_concated_string, "signature": sig} ) response = json.loads(response.body.decode()) self.assertTrue(isinstance(response, list)) self.assertTrue(len(response), 2) self.assertEqual(1, response[0]) self.assertEqual(32, len(response[1]))
def importes(tree): es = es_instance() chunks = [] for (word, node) in tree.nodes.items(): try: source = dict() source["word"] = word.encode("utf-8") source["result"] = node.children source["_index"] = "related_search" source["_type"] = "related_search" source["_id"] = md5(source["word"].encode("utf-8")) chunks.append( source) # es.index_op(source, id = md5(word.encode("utf-8"))) except Exception, e: print e
def login(): if request.method == "GET": return jsonify(msg="please login") try: data = json.loads(request.data.decode('utf8')) username = data.get("username") password = data.get("password") except Exception: response = jsonify(errMsg="wrong format") response.status_code = 401 return response if username == None or password == None: response = jsonify(errMsg="fields not complete") response.status_code = 401 return response db = DBSession() results = db.query(User).filter(User.username == username).filter( User.password == md5(password)) # if successfully logged in if results.count() == 1: user = results.first() login_info = dict() login_info['user_id'] = user.id login_info['username'] = user.username login_info['kind'] = user.kind authorization = base64.b64encode( json.dumps(login_info).encode('utf-8')) if user.kind == 0: kind = "customer" else: kind = "saler" response = jsonify(kind=kind) response.headers['authorization'] = authorization response.status_code = 200 DBSession.remove() return response # if login failed response = jsonify(errMsg="login failed") response.status_code = 401 DBSession.remove() return response
def test_init_registration_with_vk(self): os.environ["VK_APP_SECRET_KEY"] = "12345" vk_id = 42 vk_concated_string = "blablabla=1&blabla=2" sig = md5(vk_concated_string.replace("&", "") + "12345") credentials = Credentials() credentials.vk_id = vk_id result = self.service.authenticate_vk(credentials, vk_concated_string, sig) self.assertTrue(isinstance(result, tuple)) self.assertEqual(1, result[0]) self.assertEqual(32, len(result[1])) token = result[1] # Используя токен, полученный при регистрации через вк авторизуемся стандартным путем: credentials.token = token auth_result = self.service.authenticate(credentials) self.assertTrue(isinstance(auth_result, tuple)) self.assertEqual(1, auth_result[0]) self.assertEqual(token, auth_result[1])
def register(): if request.method == 'GET': return jsonify(msg="Please register") else: try: data = json.loads(request.data.decode('utf8')) username = data.get("username") password = data.get("password") kind = data.get("kind") except Exception: response = jsonify(errMsg="wrong format") response.status_code = 400 return response if username == None or password == None or kind == None: response = jsonify(errMsg="fields not complete") response.status_code = 400 return response if kind == "customer": kind = 0 elif kind == "saler": kind = 1 else: response = jsonify(errMsg="fields with illegal value") response.status_code = 400 return response db = DBSession() if db.query(User).filter(User.username == username).count() != 0: response = jsonify(errMsg="user already exists") response.status_code = 400 DBSession.remove() return response user = User(username=username, password=md5(password), kind=kind) db.add(user) db.commit() response = jsonify(msg="register successfully") response.status_code = 201 DBSession.remove() return response
def set_pwd(self, pwd): self.password = md5(pwd)
def check_pwd(self, pwd): return self.password == md5(pwd)
def resetpwd(self, oldpwd, newpwd): if md5(oldpwd) == self.password: self.password = md5(newpwd) self.save() return 0, {'msg': "修改成功"} return 1, {'msg': '密码输入错误'}