diff --git a/docs/integration/configuration.md b/docs/integration/configuration.md index bab1d1e..c7c18bf 100644 --- a/docs/integration/configuration.md +++ b/docs/integration/configuration.md @@ -51,6 +51,28 @@ OAuth2Client( ) ``` +## Backends + +If endpoints of a backend need to use `authorization_url` and `access_token_url`, then please ensure +`AUTHORIZATION_URL` and `ACCESS_TOKEN_URL` are set to `None` or at least empty strings so they can be called +alternatively. + +```python +from social_core.backends import facebook + + +class FacebookOAuth2(facebook.FacebookOAuth2): + AUTHORIZATION_URL = None + ACCESS_TOKEN_URL = None + USER_DATA_URL = "https://graph.facebook.com/v18.0/me" + + def authorization_url(self): + return "https://www.facebook.com/v18.0/dialog/oauth" + + def access_token_url(self): + return "https://graph.facebook.com/v18.0/oauth/access_token" +``` + ## Claims The `Claims` class is used to define the claim mapping for a given OAuth2 client, and it has `display_name`, `identity`, diff --git a/src/fastapi_oauth2/core.py b/src/fastapi_oauth2/core.py index a283184..1dbfaa3 100644 --- a/src/fastapi_oauth2/core.py +++ b/src/fastapi_oauth2/core.py @@ -1,4 +1,5 @@ import json +import os import random import re import string @@ -33,7 +34,10 @@ def absolute_uri(self, path=None) -> str: return path def get_setting(self, name) -> Any: - """Mocked setting method.""" + value = os.getenv(name) + if value is None: + raise KeyError + return value @staticmethod def get_json(url, method='GET', *args, **kwargs) -> httpx.Response: @@ -64,8 +68,8 @@ def __init__(self, client: OAuth2Client) -> None: self.provider = client.backend.name self.redirect_uri = client.redirect_uri self.backend = client.backend(OAuth2Strategy()) - self._authorization_endpoint = client.backend.AUTHORIZATION_URL - self._token_endpoint = client.backend.ACCESS_TOKEN_URL + self._authorization_endpoint = client.backend.AUTHORIZATION_URL or self.backend.authorization_url() + self._token_endpoint = client.backend.ACCESS_TOKEN_URL or self.backend.access_token_url() self._oauth_client = WebApplicationClient(self.client_id) @property diff --git a/tests/test_backends.py b/tests/test_backends.py index 47a91d6..0a966f9 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -1,3 +1,5 @@ +import os + import pytest from fastapi_oauth2.client import OAuth2Client @@ -6,6 +8,9 @@ @pytest.mark.anyio async def test_core_init_with_all_backends(backends): + os.environ["OIDC_ENDPOINT"] = "https://oidctest.wsweet.org" + os.environ["BASE_URL"] = "https://oidctest.wsweet.org" + for backend in backends: try: OAuth2Core(OAuth2Client(