From c6454776347f603fdab832acb54fa7ac22aa2079 Mon Sep 17 00:00:00 2001 From: sualko Date: Thu, 13 Apr 2017 09:55:25 +0200 Subject: [PATCH] add isuser operation (fix #8) --- external_cloud.py | 67 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/external_cloud.py b/external_cloud.py index 568b23e..e349ae0 100755 --- a/external_cloud.py +++ b/external_cloud.py @@ -19,6 +19,30 @@ usersafe_encoding = maketrans('-$%', 'OIl') +def send_request(data): + payload = urllib.urlencode(data) + signature = hmac.new(SECRET, msg=payload, digestmod=hashlib.sha1).hexdigest(); + headers = { + 'X-JSXC-SIGNATURE': 'sha1=' + signature, + 'content-type': 'application/x-www-form-urlencoded' + } + + try: + r = requests.post(URL, data = payload, headers = headers, allow_redirects = False) + except requests.exceptions.HTTPError as err: + logging.warn(err) + return False + except requests.exceptions.RequestException as err: + logging.warn('An error occured during the request') + return False + + if r.status_code != requests.codes.ok: + return False + + json = r.json(); + + return json; + def verify_token(username, server, password): try: token = b64decode(password.translate(usersafe_encoding) + "=======") @@ -48,32 +72,30 @@ def verify_token(username, server, password): return hmac.compare_digest(mac, response[:16]) def verify_cloud(username, server, password): - payload = urllib.urlencode({ + response = send_request({ 'operation':'auth', 'username':username, 'password':password - }) - signature = hmac.new(SECRET, msg=payload, digestmod=hashlib.sha1).hexdigest(); - headers = { - 'X-JSXC-SIGNATURE': 'sha1=' + signature, - 'content-type': 'application/x-www-form-urlencoded' - } + }); - try: - r = requests.post(URL, data = payload, headers = headers, allow_redirects = False) - except requests.exceptions.HTTPError as err: - logging.warn(err) - return False - except requests.exceptions.RequestException as err: - logging.warn('An error occured during the request') + if not response: return False - if r.status_code != requests.codes.ok: - return False + if response['result'] == 'success': + return True - json = r.json(); + return False + +def is_user_cloud(username, server): + response = send_request({ + 'operation':'isuser', + 'username':username + }); - if json['result'] == 'success': + if not response: + return False + + if response['result'] == 'success' and response['data']['isUser']: return True return False @@ -125,6 +147,13 @@ def auth(username, server, password): return False +def is_user(username, server): + if is_user_cloud(username, server): + logging.info('Cloud says this user exists') + return True + + return False + def getArgs(): # build command line argument parser desc = 'XMPP server authentication script' @@ -179,6 +208,8 @@ def getArgs(): success = False if data[0] == "auth" and len(data) == 4: success = auth(data[1], data[2], data[3]) + if data[0] == "isuser" and len(data) == 3: + success = is_user(data[1], data[2]) to_server(TYPE, success)