From a057c048aefc7dd1cec07a6c5b5fac5256a6b275 Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Tue, 3 Sep 2024 19:12:54 +0200 Subject: [PATCH 01/12] Fix authorization_url and access_token_url variables resolution (like facebook backend) --- src/fastapi_oauth2/core.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/fastapi_oauth2/core.py b/src/fastapi_oauth2/core.py index a283184..5d6b634 100644 --- a/src/fastapi_oauth2/core.py +++ b/src/fastapi_oauth2/core.py @@ -1,3 +1,4 @@ +import os import json import random import re @@ -33,7 +34,8 @@ def absolute_uri(self, path=None) -> str: return path def get_setting(self, name) -> Any: - """Mocked setting method.""" + """ settings from environment """ + return os.getenv(name, '') @staticmethod def get_json(url, method='GET', *args, **kwargs) -> httpx.Response: @@ -64,8 +66,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 = self.backend.authorization_url() + self._token_endpoint = self.backend.access_token_url() self._oauth_client = WebApplicationClient(self.client_id) @property From 307bef04bae45ef960086428f56ac550d0feb983 Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Tue, 3 Sep 2024 19:25:51 +0200 Subject: [PATCH 02/12] Add documentation for backend settings --- docs/integration/configuration.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/integration/configuration.md b/docs/integration/configuration.md index bab1d1e..6366659 100644 --- a/docs/integration/configuration.md +++ b/docs/integration/configuration.md @@ -51,6 +51,15 @@ OAuth2Client( ) ``` +Sometime, a `backend` needs settings. You can define them by enrironment variable (host OS, Docker, .env file, ...) + +Syntax is : `SOCIAL_AUTH__` + +```bash +# Example: Set API_VERSION to 20.0 for "facebook-app" backend +SOCIAL_AUTH_FACEBOOK_APP_API_VERSION=20.0 +``` + ## Claims The `Claims` class is used to define the claim mapping for a given OAuth2 client, and it has `display_name`, `identity`, From 693084672e1aa5ebecee63abeda8a9680e68abc2 Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Tue, 3 Sep 2024 20:02:25 +0200 Subject: [PATCH 03/12] Fix OAuth2Strategy, raise KeyError when get_setting is not providing a value --- src/fastapi_oauth2/core.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fastapi_oauth2/core.py b/src/fastapi_oauth2/core.py index 5d6b634..65c2ede 100644 --- a/src/fastapi_oauth2/core.py +++ b/src/fastapi_oauth2/core.py @@ -35,7 +35,10 @@ def absolute_uri(self, path=None) -> str: def get_setting(self, name) -> Any: """ settings from environment """ - return os.getenv(name, '') + value = os.getenv(name) + if value == None: + raise KeyError + return value @staticmethod def get_json(url, method='GET', *args, **kwargs) -> httpx.Response: From 26b13714bdef800b0703e87b08c62cb0f84470e1 Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Wed, 4 Sep 2024 14:53:06 +0200 Subject: [PATCH 04/12] Fix OAuth2Core, some backends do not have authorization_url() and access_token_url() methods --- src/fastapi_oauth2/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fastapi_oauth2/core.py b/src/fastapi_oauth2/core.py index 65c2ede..fc282dc 100644 --- a/src/fastapi_oauth2/core.py +++ b/src/fastapi_oauth2/core.py @@ -69,8 +69,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 = self.backend.authorization_url() - self._token_endpoint = self.backend.access_token_url() + self._authorization_endpoint = self.backend.authorization_url() if hasattr(self.backend, 'authorization_url') else self.backend.AUTHORIZATION_URL + self._token_endpoint = self.backend.access_token_url() if hasattr(self.backend, 'access_token_url') else self.backend.ACCESS_TOKEN_URL self._oauth_client = WebApplicationClient(self.client_id) @property From 9561f0467143dc0e8ca713d600de2808835f7c3a Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Wed, 4 Sep 2024 15:53:42 +0200 Subject: [PATCH 05/12] Fix test_backends for backends needing environment variables (skip Vend backend, which is malformed) --- tests/test_backends.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_backends.py b/tests/test_backends.py index 47a91d6..dbd00ae 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -1,3 +1,4 @@ +import os import pytest from fastapi_oauth2.client import OAuth2Client @@ -6,8 +7,21 @@ @pytest.mark.anyio async def test_core_init_with_all_backends(backends): + + # azuread-b2c-oauth2 + os.environ['SOCIAL_AUTH_AZUREAD_B2C_OAUTH2_TENANT_NAME'] = 'test' + os.environ['SOCIAL_AUTH_AZUREAD_B2C_OAUTH2_POLICY'] = 'b2c_test' + # UFFD backend + os.environ['SOCIAL_AUTH_UFFD_BASE_URL'] = 'test' + # OIDC backend (OpenID Connect) + os.environ['SOCIAL_AUTH_OIDC_ENDPOINT'] = 'https://oidctest.wsweet.org' + for backend in backends: try: + # Vend backend + if backend.__name__ == 'VendOAuth2': + continue # malformed backend + OAuth2Core(OAuth2Client( backend=backend, client_id="test_client_id", From fe96ed5344fe2cff9d3d8e37801fbe1690d2d6b7 Mon Sep 17 00:00:00 2001 From: Tatayoyoh Date: Wed, 4 Sep 2024 15:53:53 +0200 Subject: [PATCH 06/12] chmod +x to build.sh --- build.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 build.sh diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 From 70d4aba565aec2ca1a26e3cc45ec208b52c7b255 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 14:43:39 +0400 Subject: [PATCH 07/12] Revert file mod back to 644 --- build.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 build.sh diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 From 2d6ce18519208d13d3343f2ee99a0e13618f51ae Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 14:52:34 +0400 Subject: [PATCH 08/12] Make endpoint methods as an alternative choice --- src/fastapi_oauth2/core.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/fastapi_oauth2/core.py b/src/fastapi_oauth2/core.py index fc282dc..1dbfaa3 100644 --- a/src/fastapi_oauth2/core.py +++ b/src/fastapi_oauth2/core.py @@ -1,5 +1,5 @@ -import os import json +import os import random import re import string @@ -34,9 +34,8 @@ def absolute_uri(self, path=None) -> str: return path def get_setting(self, name) -> Any: - """ settings from environment """ value = os.getenv(name) - if value == None: + if value is None: raise KeyError return value @@ -69,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 = self.backend.authorization_url() if hasattr(self.backend, 'authorization_url') else self.backend.AUTHORIZATION_URL - self._token_endpoint = self.backend.access_token_url() if hasattr(self.backend, 'access_token_url') else self.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 From 6ebc605d99bbee18d6c96bf5d69128fe41173ffe Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 14:53:38 +0400 Subject: [PATCH 09/12] Wrong to skip a backend just for making tests pass --- tests/test_backends.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_backends.py b/tests/test_backends.py index dbd00ae..623e8ac 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -18,10 +18,6 @@ async def test_core_init_with_all_backends(backends): for backend in backends: try: - # Vend backend - if backend.__name__ == 'VendOAuth2': - continue # malformed backend - OAuth2Core(OAuth2Client( backend=backend, client_id="test_client_id", From 6e06db54b14cd3bff68e2fc76a4c113232c3809e Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 14:54:14 +0400 Subject: [PATCH 10/12] Remove unnecessary environment variables --- tests/test_backends.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/test_backends.py b/tests/test_backends.py index 623e8ac..afa0a8c 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -1,4 +1,5 @@ import os + import pytest from fastapi_oauth2.client import OAuth2Client @@ -7,14 +8,7 @@ @pytest.mark.anyio async def test_core_init_with_all_backends(backends): - - # azuread-b2c-oauth2 - os.environ['SOCIAL_AUTH_AZUREAD_B2C_OAUTH2_TENANT_NAME'] = 'test' - os.environ['SOCIAL_AUTH_AZUREAD_B2C_OAUTH2_POLICY'] = 'b2c_test' - # UFFD backend - os.environ['SOCIAL_AUTH_UFFD_BASE_URL'] = 'test' - # OIDC backend (OpenID Connect) - os.environ['SOCIAL_AUTH_OIDC_ENDPOINT'] = 'https://oidctest.wsweet.org' + os.environ["OIDC_ENDPOINT"] = "https://oidctest.wsweet.org" for backend in backends: try: From 7eae8622d80392e8a0fb976faac1ebd3ec5ecfd9 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 15:11:49 +0400 Subject: [PATCH 11/12] Update the docs - add "Backends" section --- docs/integration/configuration.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/integration/configuration.md b/docs/integration/configuration.md index 6366659..c7c18bf 100644 --- a/docs/integration/configuration.md +++ b/docs/integration/configuration.md @@ -51,13 +51,26 @@ OAuth2Client( ) ``` -Sometime, a `backend` needs settings. You can define them by enrironment variable (host OS, Docker, .env file, ...) +## Backends -Syntax is : `SOCIAL_AUTH__` +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. -```bash -# Example: Set API_VERSION to 20.0 for "facebook-app" backend -SOCIAL_AUTH_FACEBOOK_APP_API_VERSION=20.0 +```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 From 23e8a66947da3ac7ed9701d22f773e5468dffc97 Mon Sep 17 00:00:00 2001 From: Artyom Vancyan Date: Sun, 8 Sep 2024 15:19:46 +0400 Subject: [PATCH 12/12] Fix tests for latest versions of FastAPI --- tests/test_backends.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_backends.py b/tests/test_backends.py index afa0a8c..0a966f9 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -9,6 +9,7 @@ @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: