Skip to content

Commit

Permalink
Merge pull request #11 from jsxc/add-isuser
Browse files Browse the repository at this point in the history
add isuser operation

fix #8
  • Loading branch information
sualko authored May 2, 2017
2 parents 5cac915 + c645477 commit a5bdb07
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions external_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "=======")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a5bdb07

Please sign in to comment.