Skip to content

Commit 350b526

Browse files
Implement endpoint alternatives (GH-49)
Co-authored-by: Tatayoyoh <[email protected]>
2 parents e388257 + 23e8a66 commit 350b526

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

docs/integration/configuration.md

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ OAuth2Client(
5151
)
5252
```
5353

54+
## Backends
55+
56+
If endpoints of a backend need to use `authorization_url` and `access_token_url`, then please ensure
57+
`AUTHORIZATION_URL` and `ACCESS_TOKEN_URL` are set to `None` or at least empty strings so they can be called
58+
alternatively.
59+
60+
```python
61+
from social_core.backends import facebook
62+
63+
64+
class FacebookOAuth2(facebook.FacebookOAuth2):
65+
AUTHORIZATION_URL = None
66+
ACCESS_TOKEN_URL = None
67+
USER_DATA_URL = "https://graph.facebook.com/v18.0/me"
68+
69+
def authorization_url(self):
70+
return "https://www.facebook.com/v18.0/dialog/oauth"
71+
72+
def access_token_url(self):
73+
return "https://graph.facebook.com/v18.0/oauth/access_token"
74+
```
75+
5476
## Claims
5577

5678
The `Claims` class is used to define the claim mapping for a given OAuth2 client, and it has `display_name`, `identity`,

src/fastapi_oauth2/core.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import random
34
import re
45
import string
@@ -33,7 +34,10 @@ def absolute_uri(self, path=None) -> str:
3334
return path
3435

3536
def get_setting(self, name) -> Any:
36-
"""Mocked setting method."""
37+
value = os.getenv(name)
38+
if value is None:
39+
raise KeyError
40+
return value
3741

3842
@staticmethod
3943
def get_json(url, method='GET', *args, **kwargs) -> httpx.Response:
@@ -64,8 +68,8 @@ def __init__(self, client: OAuth2Client) -> None:
6468
self.provider = client.backend.name
6569
self.redirect_uri = client.redirect_uri
6670
self.backend = client.backend(OAuth2Strategy())
67-
self._authorization_endpoint = client.backend.AUTHORIZATION_URL
68-
self._token_endpoint = client.backend.ACCESS_TOKEN_URL
71+
self._authorization_endpoint = client.backend.AUTHORIZATION_URL or self.backend.authorization_url()
72+
self._token_endpoint = client.backend.ACCESS_TOKEN_URL or self.backend.access_token_url()
6973
self._oauth_client = WebApplicationClient(self.client_id)
7074

7175
@property

tests/test_backends.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pytest
24

35
from fastapi_oauth2.client import OAuth2Client
@@ -6,6 +8,9 @@
68

79
@pytest.mark.anyio
810
async def test_core_init_with_all_backends(backends):
11+
os.environ["OIDC_ENDPOINT"] = "https://oidctest.wsweet.org"
12+
os.environ["BASE_URL"] = "https://oidctest.wsweet.org"
13+
914
for backend in backends:
1015
try:
1116
OAuth2Core(OAuth2Client(

0 commit comments

Comments
 (0)