Skip to content

Commit 7219318

Browse files
authored
Merges #6 Closes #6
2 parents 4926e20 + 5ff3ebf commit 7219318

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Handle of OAuth expired token
3+
category: fixed
4+
author: Santiago Dueñas <sduenas@bitergia.com>
5+
issue: null
6+
notes: >
7+
There was a bug making that the expiration time
8+
for a token was set to null. The token was not
9+
correctly initialized preventing the refresh of
10+
that token.

sortinghat/core/importer/backends/eclipse.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
from django.conf import settings
2424
from django.db.models import (Q, Subquery)
2525

26-
from requests_oauth2client import (
27-
BearerToken,
28-
OAuth2Client
29-
)
26+
from requests_oauth2client import OAuth2Client
27+
from requests_oauth2client.tokens import ExpiredAccessToken
3028

3129
from grimoirelab_toolkit.datetime import (
3230
str_to_datetime,
@@ -165,6 +163,8 @@ def get_individuals(self):
165163
enr = Enrollment(org)
166164
individual.enrollments.append(enr)
167165

166+
logger.info(f"Eclipse account processed; account={account['name']}; changed={account['changed']}")
167+
168168
yield individual
169169

170170
def post_process_individual(self, individual, uuid):
@@ -314,18 +314,19 @@ def _fetch_retry(self, url, params=None):
314314
max_retries = self.MAX_RETRIES
315315

316316
while retries < max_retries:
317-
response = requests.get(url, params=params, auth=self.token)
317+
try:
318+
response = requests.get(url, params=params, auth=self.token)
319+
except ExpiredAccessToken:
320+
# Refresh token and try again
321+
self.login(self.user_id, self.password)
322+
retries += 1
323+
continue
318324

319325
if response.status_code == 200:
320326
return response.json()
321327
elif response.status_code == 403:
322-
# Refresh token if needed and try again
323328
if self.token.expires_at <= datetime_utcnow():
324-
self.token = self._authenticate(
325-
self.user_id,
326-
self.password,
327-
self.ECLIPSE_SCOPE,
328-
)
329+
self.login(self.user_id, self.password)
329330
retries += 1
330331
elif 500 <= response.status_code < 600:
331332
# Errors could have been related to server overloading
@@ -351,4 +352,4 @@ def _authenticate(self, client_id, client_secret, scope):
351352
)
352353
token = oauth2client.client_credentials(scope=scope)
353354

354-
return BearerToken(token)
355+
return token

0 commit comments

Comments
 (0)