Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Make sure access token is refreshed before it's used
Browse files Browse the repository at this point in the history
Access token expires afer 5 mins, refresh token after 30 mins.
  • Loading branch information
haasad committed Aug 25, 2023
1 parent bbe2961 commit 8ee5dd1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions eidl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def login(self):

self.login_success(response.ok)

def refresh_tokens(self):
if self.refresh_token is None:
return

sso_url='https://sso.ecoinvent.org/realms/ecoinvent/protocol/openid-connect/token'
post_data = {'client_id': 'apollo-ui',
'grant_type': 'refresh_token',
'refresh_token': self.refresh_token}
response = requests.post(sso_url, post_data, timeout=20)

if response.ok:
tokens = json.loads(response.text)
self.access_token = tokens['access_token']
self.refresh_token = tokens['refresh_token']
else:
self.login()

def login_success(self, success):
if not success:
print('Login failed')
Expand All @@ -97,6 +114,7 @@ def handle_connection_timeout(self):

def get_available_files(self):
files_url = 'https://api.ecoquery.ecoinvent.org/files'
self.refresh_tokens()
auth_header = {'Authorization': f'Bearer {self.access_token}'}
try:
files_res = requests.get(files_url, headers=auth_header, timeout=20)
Expand Down Expand Up @@ -151,6 +169,7 @@ def choose_db(self):
def download(self):
db_key = (self.version, self.system_model)
url = f'https://api.ecoquery.ecoinvent.org/files/r/{self.db_dict[db_key]}'
self.refresh_tokens()
auth_header = {'Authorization': f'Bearer {self.access_token}'}
try:
s3_link = json.loads(requests.get(url, headers=auth_header, timeout=20).text)
Expand Down

0 comments on commit 8ee5dd1

Please sign in to comment.