Skip to content

Commit e8d4d16

Browse files
proof of concept for refresh Token
1 parent a25cfdc commit e8d4d16

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

SoftLayer/API.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def authenticate_with_password(self, username, password, security_token=None):
665665

666666

667667
self.settings['softlayer']['access_token'] = auth_result['hash']
668-
self.settings['softlayer']['userId'] = auth_result['userId']
668+
self.settings['softlayer']['userId'] = str(auth_result['userId'])
669669
# self.settings['softlayer']['refresh_token'] = tokens['refresh_token']
670670

671671
config.write_config(self.settings, self.config_file)
@@ -708,7 +708,7 @@ def call(self, service, method, *args, **kwargs):
708708
raise ex
709709

710710
def __repr__(self):
711-
return "IAMClient(transport=%r, auth=%r)" % (self.transport, self.auth)
711+
return "EmployeeClient(transport=%r, auth=%r)" % (self.transport, self.auth)
712712

713713
class Service(object):
714714
"""A SoftLayer Service.

SoftLayer/CLI/login.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,36 @@ def cli(env):
2828
username: Set this in either the softlayer config, or SL_USER ENV variable
2929
password: Set this in SL_PASSWORD env variable. You will be prompted for them otherwise.
3030
"""
31-
settings = config.get_config(config_file=env.config_file)
32-
username = settings.get('username') or os.environ.get('SLCLI_USER')
31+
config_settings = config.get_config(config_file=env.config_file)
32+
settings = config_settings['softlayer']
33+
username = settings.get('username') or os.environ.get('SLCLI_USER', None)
3334
password = os.environ.get('SLCLI_PASSWORD', '')
34-
yubi = 123456
35+
yubi = None
36+
# client = EmployeeClient(config_file=env.config_file)
37+
client = env.client
38+
39+
# Might already be logged in, try and refresh token
40+
if settings.get('access_token') and settings.get('userid'):
41+
client.authenticate_with_hash(settings.get('userid'), settings.get('access_token'))
42+
try:
43+
employee = client.call('SoftLayer_User_Employee', 'getObject', id=settings.get('userid'), mask="mask[id,username]")
44+
print(employee)
45+
refresh = client.call('SoftLayer_User_Employee', 'refreshEncryptedToken', settings.get('access_token'), id=settings.get('userid'))
46+
print("REFRESH:\n{}\n".format(refresh))
47+
# we expect 2 results, a hash and a timeout
48+
if len(refresh) > 1:
49+
for returned_data in refresh:
50+
# Access tokens should be 188 characters, but just incase.
51+
if len(returned_data) > 180:
52+
settings['access_token'] = returned_data
53+
else:
54+
raise Exception("Got unexpected data. Expected 2 properties: {}".format(refresh))
55+
config_settings['softlayer'] = settings
56+
config.write_config(config_settings, env.config_file)
57+
return
58+
except Exception as ex:
59+
print("Error with Hash, try with password: {}".format(ex))
60+
3561

3662
url = settings.get('endpoint_url') or consts.API_EMPLOYEE_ENDPOINT
3763
click.echo("URL: {}".format(url))
@@ -41,11 +67,12 @@ def cli(env):
4167
if password is None:
4268
password = env.getpass("Password: ")
4369
click.echo("Password: {}".format(censor_password(password)))
44-
# yubi = input("Yubi: ")
70+
yubi = input("Yubi: ")
4571

46-
client = EmployeeClient(config_file=env.config_file)
72+
4773
try:
48-
client.authenticate_with_password(username, password, yubi)
74+
result = client.authenticate_with_password(username, password, str(yubi))
75+
print(result)
4976
except Exception as e:
5077
click.echo("EXCEPTION: {}".format(e))
5178

islcli

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!python
2+
import re
3+
import sys
4+
5+
from SoftLayer.CLI.core import main
6+
7+
if __name__ == '__main__':
8+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
9+
# print("arvs[0] = %s" % sys.argv[0])
10+
sys.exit(main())

0 commit comments

Comments
 (0)