Skip to content

Commit d105c10

Browse files
authored
Merge pull request #51 from D34DPlayer/master
Improve token refresh and add token login method
2 parents b8e422f + 4e9761d commit d105c10

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

tb_rest_client/rest_client_base.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ def __init__(self, base_url):
6767
self.base_url = base_url
6868
else:
6969
self.base_url = "http://" + base_url
70-
self.token_info = {"token": "", "refreshToken": 0}
70+
self.token_info = {"token": "", "refreshToken": "", "exp": 0}
7171
self.api_client = None
72-
self.username = None
73-
self.password = None
7472
self.logged_in = False
7573
self.stopped = True
7674
self.configuration = Configuration()
@@ -81,9 +79,9 @@ def run(self):
8179
while not self.stopped:
8280
try:
8381
check_time = time()
84-
if check_time >= self.token_info["refreshToken"] and self.logged_in:
85-
if self.username and self.password:
86-
self.login(self.username, self.password)
82+
if check_time >= self.token_info["exp"] and self.logged_in:
83+
if self.token_info["refreshToken"]:
84+
self.refresh()
8785
else:
8886
logger.error("No username or password provided!")
8987
sleep(1)
@@ -105,10 +103,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
105103

106104
def login(self, username, password):
107105
"""Authorization on the host and saving the toke information"""
108-
if self.username is None and self.password is None:
109-
self.username = username
110-
self.password = password
111-
self.logged_in = True
106+
self.logged_in = True
112107

113108
token_json = post(self.base_url + "/api/auth/login", json={"username": username, "password": password},
114109
verify=self.configuration.verify_ssl).json()
@@ -123,13 +118,35 @@ def public_login(self, public_id):
123118
self.__save_token(token_json)
124119
self.__load_configuration()
125120

121+
def token_login(self, token, refresh_token=None):
122+
token_json = {
123+
"token": token,
124+
"refresh_token": refresh_token,
125+
}
126+
127+
self.__save_token(token_json)
128+
self.__load_configuration()
129+
130+
def refresh(self):
131+
if not self.token_info["refreshToken"]:
132+
return
133+
134+
token_json = post(self.base_url + "/api/auth/token", json={"refreshToken": self.token_info["refreshToken"]},
135+
verify=self.configuration.verify_ssl).json()
136+
137+
self.__save_token(token_json)
138+
self.__load_configuration()
139+
126140
def __save_token(self, token_json):
127141
token = None
142+
refresh_token = None
128143
if isinstance(token_json, dict) and token_json.get("token") is not None:
129144
token = token_json["token"]
145+
refresh_token = token_json["refreshToken"]
130146
self.configuration.api_key_prefix["X-Authorization"] = "Bearer"
131147
self.configuration.api_key["X-Authorization"] = token
132148
self.token_info['token'] = token
149+
self.token_info['refreshToken'] = refreshToken
133150

134151
def __load_configuration(self):
135152
self.api_client = ApiClient(self.configuration)

tb_rest_client/rest_client_pe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def public_login(self, public_id):
3333
super(RestClientPE, self).public_login(public_id=public_id)
3434
self.__load_controllers()
3535

36+
def token_login(self, token, refresh_token=None):
37+
super(RestClientPE, self).token_login(token=token, refresh_token=refresh_token)
38+
self.__load_controllers()
39+
3640
# Self Registration Controller
3741
def get_privacy_policy(self, ):
3842
return self.self_registration_controller.get_privacy_policy_using_get()

0 commit comments

Comments
 (0)