Skip to content

Commit 8967126

Browse files
committed
fixed authentication code
1 parent f3ab4bd commit 8967126

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

sliderule/sliderule.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def __build_auth_header():
329329
rsps = requests.post(host, data=json.dumps(rqst), headers=hdrs, timeout=request_timeout).json()
330330
ps_refresh_token = rsps["refresh"]
331331
ps_access_token = rsps["access"]
332-
ps_token_exp = time.time() + (rsps["access_lifetime"] / 2)
332+
ps_token_exp = time.time() + (float(rsps["access_lifetime"]) / 2)
333333
# Build Authentication Header
334334
headers = {'Authorization': 'Bearer ' + ps_access_token}
335335
return headers
@@ -411,14 +411,14 @@ def source (api, parm={}, stream=False, callbacks={}, path="/source"):
411411
for c in __callbacks:
412412
if c not in callbacks:
413413
callbacks[c] = __callbacks[c]
414-
# Construct Request URL and Authorization
415-
if service_org:
416-
url = 'https://%s.%s%s/%s' % (service_org, service_url, path, api)
417-
headers = __build_auth_header()
418-
else:
419-
url = 'http://%s%s/%s' % (service_url, path, api)
420414
# Attempt Request
421415
try:
416+
# Construct Request URL and Authorization
417+
if service_org:
418+
url = 'https://%s.%s%s/%s' % (service_org, service_url, path, api)
419+
headers = __build_auth_header()
420+
else:
421+
url = 'http://%s%s/%s' % (service_url, path, api)
422422
# Perform Request
423423
if not stream:
424424
data = requests.get(url, data=rqst, headers=headers, timeout=request_timeout)
@@ -566,9 +566,9 @@ def update_available_servers (desired_nodes=None):
566566

567567
# Update number of nodes
568568
if type(desired_nodes) == int:
569-
host = "https://ps." + service_url + "/api/desired_org_num_nodes/" + service_org + "/" + str(desired_nodes)
569+
host = "https://ps." + service_url + "/api/desired_org_num_nodes/" + service_org + "/" + str(desired_nodes) + "/"
570570
headers = __build_auth_header()
571-
rsps = requests.get(host, headers=headers, timeout=request_timeout).json()
571+
rsps = requests.put(host, headers=headers, timeout=request_timeout)
572572
rsps.raise_for_status()
573573

574574
# Get number of nodes currently registered
@@ -640,10 +640,13 @@ def authenticate (ps_organization, ps_username=None, ps_password=None):
640640
headers = {'Content-Type': 'application/json'}
641641
try:
642642
api = "https://" + ps_url + "/api/org_token/"
643-
rsps = requests.post(api, data=json.dumps(rqst), headers=headers, timeout=request_timeout).json()
643+
print("cred", api, ps_username, ps_password, ps_organization)
644+
rsps = requests.post(api, data=json.dumps(rqst), headers=headers, timeout=request_timeout)
645+
rsps.raise_for_status()
646+
rsps = rsps.json()
644647
ps_refresh_token = rsps["refresh"]
645648
ps_access_token = rsps["access"]
646-
ps_token_exp = time.time() + (rsps["access_lifetime"] / 2)
649+
ps_token_exp = time.time() + (float(rsps["access_lifetime"]) / 2)
647650
login_status = True
648651
except:
649652
logger.error("Unable to authenticate user %s to %s" % (ps_username, api))

0 commit comments

Comments
 (0)