Skip to content

Commit

Permalink
Implement endpoint alternatives (GH-49)
Browse files Browse the repository at this point in the history
Co-authored-by: Tatayoyoh <[email protected]>
  • Loading branch information
ArtyomVancyan and Tatayoyoh authored Sep 8, 2024
2 parents e388257 + 23e8a66 commit 350b526
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
22 changes: 22 additions & 0 deletions docs/integration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
10 changes: 7 additions & 3 deletions src/fastapi_oauth2/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import random
import re
import string
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

from fastapi_oauth2.client import OAuth2Client
Expand All @@ -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(
Expand Down

0 comments on commit 350b526

Please sign in to comment.