Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions releases/unreleased/handle-of-oauth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Handle of OAuth expired token
category: fixed
author: Santiago Dueñas <sduenas@bitergia.com>
issue: null
notes: >
There was a bug making that the expiration time
for a token was set to null. The token was not
correctly initialized preventing the refresh of
that token.
25 changes: 13 additions & 12 deletions sortinghat/core/importer/backends/eclipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
from django.conf import settings
from django.db.models import (Q, Subquery)

from requests_oauth2client import (
BearerToken,
OAuth2Client
)
from requests_oauth2client import OAuth2Client
from requests_oauth2client.tokens import ExpiredAccessToken

from grimoirelab_toolkit.datetime import (
str_to_datetime,
Expand Down Expand Up @@ -165,6 +163,8 @@ def get_individuals(self):
enr = Enrollment(org)
individual.enrollments.append(enr)

logger.info(f"Eclipse account processed; account={account['name']}; changed={account['changed']}")

yield individual

def post_process_individual(self, individual, uuid):
Expand Down Expand Up @@ -314,18 +314,19 @@ def _fetch_retry(self, url, params=None):
max_retries = self.MAX_RETRIES

while retries < max_retries:
response = requests.get(url, params=params, auth=self.token)
try:
response = requests.get(url, params=params, auth=self.token)
except ExpiredAccessToken:
# Refresh token and try again
self.login(self.user_id, self.password)
retries += 1
continue

if response.status_code == 200:
return response.json()
elif response.status_code == 403:
# Refresh token if needed and try again
if self.token.expires_at <= datetime_utcnow():
self.token = self._authenticate(
self.user_id,
self.password,
self.ECLIPSE_SCOPE,
)
self.login(self.user_id, self.password)
retries += 1
elif 500 <= response.status_code < 600:
# Errors could have been related to server overloading
Expand All @@ -351,4 +352,4 @@ def _authenticate(self, client_id, client_secret, scope):
)
token = oauth2client.client_credentials(scope=scope)

return BearerToken(token)
return token