From ff8c4c5f2ee2cd0605712ad9e63ec890d219a0b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E9=95=BF=E6=B1=9F?= Date: Fri, 25 Oct 2019 19:55:28 +0800 Subject: [PATCH 1/2] add loginByPhoneCode and getVerificationCode --- authing/authing.py | 150 +++++++++++++++++++++++++++------------------ authing/test.py | 22 +++++-- 2 files changed, 107 insertions(+), 65 deletions(-) diff --git a/authing/authing.py b/authing/authing.py index 3186856..14a52fc 100644 --- a/authing/authing.py +++ b/authing/authing.py @@ -1,8 +1,8 @@ from sgqlc.endpoint.http import HTTPEndpoint -import json import urllib import rsa import base64 +import json LOGIN_METHOD_USING_EMAIL = "EMAIL" LOGIN_METHOD_USING_PHONE = "PHONE" @@ -143,7 +143,7 @@ def __init__(self, clientId, secret, options=None): self.userToken = options["userToken"] - self.servies = { + self.services = { "oauth": options["oauth"], "users": options["users"] } @@ -156,7 +156,7 @@ def __init__(self, clientId, secret, options=None): def auth(self): if 'authService' not in self.__dict__: - self.authService = self._initService(self.servies['users']) + self.authService = self._initService(self.services['users']) authQuery = ''' query getAccessTokenByAppSecret($secret: String!, $clientId: String!){ @@ -184,7 +184,7 @@ def auth(self): "Authorization": 'Bearer {}'.format(self.userToken or self.accessToken) }) - self.authService = self._initService(self.servies['users'], headers={ + self.authService = self._initService(self.services['users'], headers={ "Authorization": 'Bearer {}'.format(self.userToken or self.accessToken) }) @@ -197,89 +197,103 @@ def _initService(self, url, headers={}): return AuthingEndPoint(url, headers) def _initOAuth(self, headers={}): - self.oauth = self._initService(self.servies['oauth'], headers=headers) + self.oauth = self._initService(self.services['oauth'], headers=headers) return self.oauth def _initUsers(self, headers={}): - self.users = self._initService(self.servies['users'], headers=headers) + self.users = self._initService(self.services['users'], headers=headers) return self.users - def login(self, email: str = None, password: str = None, - verifyCode: str = None, username: str = None, - phone: str = None, phoneCode: int = None - ): + def login(self, email=None, username: str = None, password=None, verifyCode=None): - def detect_login_method(): - if email and password: - return LOGIN_METHOD_USING_EMAIL - elif phone and phoneCode: - return LOGIN_METHOD_USING_PHONE - elif username and password: - return LOGIN_METHOD_USING_USERNAME + if not email and not username: + raise Exception('请提供邮箱 email 或用户名 username') - login_method = detect_login_method() - if login_method is None: - raise Exception("登陆信息不全,查看文档 https://learn.authing.cn/authing/sdk/sdk-for-python 了解 Authing 支持的不同登陆方式。") + if not password: + raise Exception('请提供密码:password') loginQuery = """ - mutation login( - $email: String - $password: String - $lastIP: String - $registerInClient: String! - $verifyCode: String - $phone: String - $username: String - $browser: String - $phoneCode: Int -) { - login( - email: $email - password: $password - lastIP: $lastIP - registerInClient: $registerInClient - verifyCode: $verifyCode - phone: $phone - phoneCode: $phoneCode - username: $username - browser: $browser - ) { + mutation login($unionid: String, $email: String, $password: String, $lastIP: String, $registerInClient: String!, $verifyCode: String) { + login(unionid: $unionid, email: $email, password: $password, lastIP: $lastIP, registerInClient: $registerInClient, verifyCode: $verifyCode) { + _id + email + emailVerified + username + nickname + company + photo + browser + token + tokenExpiredAt + loginsCount + lastLogin + lastIP + signedUp + blocked + isDeleted + } + } + """ + + _password = self.encrypt(password) + + variables = { + "password": _password, + "registerInClient": self.clientId, + "verifyCode": verifyCode + } + if email: + variables['email'] = email + elif username: + variables['username'] = username + + loginResult = self.users(loginQuery, variables) + + if not loginResult.get('errors'): + self.users = self._initUsers({ + "Authorization": 'Bearer {}'.format(loginResult['data']['login']['token']) + }) + return loginResult['data']['login'] + else: + return loginResult + + def loginByPhoneCode(self, phone: str, phoneCode: int): + + if not isinstance(phoneCode, int): + raise Exception("phoneCode 必须为 int 类型") + + loginQuery = """ +mutation login($phone: String, $phoneCode: Int, $registerInClient: String!, $browser: String) { + login(phone: $phone, phoneCode: $phoneCode, registerInClient: $registerInClient, browser: $browser) { _id email + unionid + openid emailVerified username nickname + phone company photo browser - password token + tokenExpiredAt loginsCount - group { - name - } + lastLogin + lastIP + signedUp blocked + isDeleted } } """ - - if password: - password = self.encrypt(password) variables = { - "registerInClient": self.clientId + "registerInClient": self.clientId, + 'phone': phone, + 'phoneCode': phoneCode } - if login_method == LOGIN_METHOD_USING_EMAIL: - variables['email'] = email - variables['password'] = password - variables['verifyCode'] = verifyCode - elif login_method == LOGIN_METHOD_USING_PHONE: - variables['phone'] = phone - variables['phoneCode'] = phoneCode - elif login_method == LOGIN_METHOD_USING_USERNAME: - variables['username'] = username - variables['password'] = password - loginResult = self.users(loginQuery, variables) + loginResult = self.users(loginQuery, variables) if not loginResult.get('errors'): self.users = self._initUsers({ "Authorization": 'Bearer {}'.format(loginResult['data']['login']['token']) @@ -288,6 +302,20 @@ def detect_login_method(): else: return loginResult + def getVerificationCode(self, phone): + send_sms_spi = "{}/send_smscode/{}/{}".format( + self.services['users'].replace("/graphql", ''), + phone, + self.clientId + ) + req = urllib.request.Request(send_sms_spi) + resp = urllib.request.urlopen(req) + data = json.loads(resp.read()) + code, msg = data['code'], data['message'] + if code != 200: + raise Exception(msg) + return data + def register(self, email=None, password=None): if not email: diff --git a/authing/test.py b/authing/test.py index 1812867..e05e416 100644 --- a/authing/test.py +++ b/authing/test.py @@ -6,9 +6,10 @@ test_name = '' - username = "" - password = "" - phone = "" + email = "febwfb@gmail.com" + username = "dboehfoikjd" + password = "123456" + phone = "17670416754" phoneCode = 1234 @@ -35,6 +36,14 @@ def log_test_result(result): }) log_test_result(authing.accessToken) + # ------- login test ---------- # + log_tester_name("使用邮箱密码登陆") + loginResult = authing.login( + email=email, + password=password + ) + log_test_result(loginResult) + # ------- login test ---------- # log_tester_name("使用用户名密码登陆") loginResult = authing.login( @@ -43,9 +52,14 @@ def log_test_result(result): ) log_test_result(loginResult) + # ------- sms test ---------- # + log_tester_name("发送验证码") + verificationResult = authing.getVerificationCode(phone) + log_test_result(verificationResult) + # ------- login test ---------- # log_tester_name("使用手机号登陆") - loginResult = authing.login( + loginResult = authing.loginByPhoneCode( phone=phone, phoneCode=phoneCode ) From 915c0a76973c2f46cff9c9e74052a9700fadd69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E9=95=BF=E6=B1=9F?= Date: Fri, 25 Oct 2019 19:57:39 +0800 Subject: [PATCH 2/2] update setup.py version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e696bf8..1312f8b 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='authing', # 名称 - version='0.14.0', # 版本 + version='0.15.0', # 版本 description="Authing SDK for Python", # 描述 long_description=long_description, long_description_content_type="text/markdown",