Skip to content

Commit

Permalink
fix: bad CrossrefProvider imports + reintroduce auth0 retry (#3819)
Browse files Browse the repository at this point in the history
* fix: bad CrossrefProvider imports

* fix tests

* lint

* last minute fix
  • Loading branch information
ebezzi authored Dec 21, 2022
1 parent 029853c commit 53e2c7a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
22 changes: 15 additions & 7 deletions backend/common/authorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
from functools import lru_cache

import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry
from backend.common.corpora_config import CorporaAuthConfig
from backend.common.utils.http_exceptions import UnauthorizedError
from backend.common.utils.jwt import jwt_decode, get_unverified_header

auth0_session_with_retry = requests.Session()
# TODO: these read the configuration on initialization and cause problems with tests
# retry_config = Retry(total=3, backoff_factor=1, status_forcelist=CorporaAuthConfig().retry_status_forcelist)
# auth0_session_with_retry.mount("https://", HTTPAdapter(max_retries=retry_config))
_auth0_session_with_retry = None


def get_auth0_session_with_retry():
global _auth0_session_with_retry
if _auth0_session_with_retry is None:
_auth0_session_with_retry = requests.Session()
retry_config = Retry(total=3, backoff_factor=1, status_forcelist=CorporaAuthConfig().retry_status_forcelist)
_auth0_session_with_retry.mount("https://", HTTPAdapter(max_retries=retry_config))
return _auth0_session_with_retry


def assert_authorized_token(token: str, audience: str = None) -> dict:
Expand Down Expand Up @@ -46,7 +54,7 @@ def assert_authorized_token(token: str, audience: str = None) -> dict:

def get_userinfo_from_auth0(token: str) -> dict:
auth_config = CorporaAuthConfig()
res = auth0_session_with_retry.get(auth_config.api_userinfo_url, headers={"Authorization": f"Bearer {token}"})
res = get_auth0_session_with_retry().get(auth_config.api_userinfo_url, headers={"Authorization": f"Bearer {token}"})
res.raise_for_status()
return res.json()

Expand All @@ -57,7 +65,7 @@ def get_openid_config(openid_provider: str):
:param openid_provider: the openid provider's domain.
:return: the openid configuration
"""
res = auth0_session_with_retry.get("{op}/.well-known/openid-configuration".format(op=openid_provider))
res = get_auth0_session_with_retry().get("{op}/.well-known/openid-configuration".format(op=openid_provider))
res.raise_for_status()
return res.json()

Expand All @@ -69,5 +77,5 @@ def get_public_keys(openid_provider: str):
:param openid_provider: the openid provider's domain.
:return: Public Keys
"""
keys = auth0_session_with_retry.get(get_openid_config(openid_provider)["jwks_uri"]).json()["keys"]
keys = get_auth0_session_with_retry().get(get_openid_config(openid_provider)["jwks_uri"]).json()["keys"]
return {key["kid"]: key for key in keys}
7 changes: 5 additions & 2 deletions backend/layers/business/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
from typing import Iterable, Optional, Tuple

from backend.common.providers.crossref_provider import CrossrefDOINotFoundException, CrossrefException
from backend.layers.business.business_interface import BusinessLogicInterface
from backend.layers.business.entities import (
CollectionMetadataUpdate,
Expand Down Expand Up @@ -50,7 +49,11 @@
Link,
)
from backend.layers.persistence.persistence_interface import DatabaseProviderInterface
from backend.layers.thirdparty.crossref_provider import CrossrefProviderInterface
from backend.layers.thirdparty.crossref_provider import (
CrossrefDOINotFoundException,
CrossrefException,
CrossrefProviderInterface,
)
from backend.layers.thirdparty.s3_provider import S3ProviderInterface
from backend.layers.thirdparty.step_function_provider import StepFunctionProviderInterface
from backend.layers.thirdparty.uri_provider import UriProviderInterface
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/backend/layers/api/test_portal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from furl import furl

from backend.common.providers.crossref_provider import CrossrefDOINotFoundException, CrossrefFetchException
from backend.layers.thirdparty.crossref_provider import CrossrefDOINotFoundException, CrossrefFetchException
from backend.portal.api.collections_common import verify_collection_body
from tests.unit.backend.layers.common.base_test import (
DatasetArtifactUpdate,
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/backend/layers/business/test_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from unittest.mock import Mock
from uuid import uuid4

from backend.common.providers.crossref_provider import CrossrefDOINotFoundException, CrossrefException
from backend.layers.thirdparty.crossref_provider import (
CrossrefDOINotFoundException,
CrossrefException,
CrossrefProviderInterface,
)
from backend.layers.business.business import (
BusinessLogic,
CollectionMetadataUpdate,
Expand Down Expand Up @@ -36,7 +40,6 @@
)
from backend.layers.persistence.persistence import DatabaseProvider
from backend.layers.persistence.persistence_mock import DatabaseProviderMock
from backend.layers.thirdparty.crossref_provider import CrossrefProviderInterface
from backend.layers.thirdparty.s3_provider import S3ProviderInterface
from backend.layers.thirdparty.step_function_provider import StepFunctionProviderInterface
from backend.layers.thirdparty.uri_provider import FileInfo, UriProviderInterface
Expand Down

0 comments on commit 53e2c7a

Please sign in to comment.