This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class RedditAccount(ProviderAccount):
def to_str(self):
dflt = super(RedditAccount, self).to_str()
name = self.account.extra_data.get("name", dflt)
return name
class RedditProvider(OAuth2Provider):
id = "reddit"
name = "Reddit"
account_class = RedditAccount
def extract_uid(self, data):
return data["name"]
def extract_common_fields(self, data):
return dict(username=data.get("name"))
def get_default_scope(self):
scope = ["identity"]
return scope
provider_classes = [RedditProvider]

View File

@@ -0,0 +1,17 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import RedditProvider
class RedditTests(OAuth2TestsMixin, TestCase):
provider_id = RedditProvider.id
def get_mocked_response(self):
return [
MockedResponse(
200,
"""{
"name": "wayward710"}""",
)
]

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import RedditProvider
urlpatterns = default_urlpatterns(RedditProvider)

View File

@@ -0,0 +1,37 @@
import requests
from allauth.socialaccount import app_settings
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import RedditProvider
class RedditAdapter(OAuth2Adapter):
provider_id = RedditProvider.id
access_token_url = "https://www.reddit.com/api/v1/access_token"
authorize_url = "https://www.reddit.com/api/v1/authorize"
profile_url = "https://oauth.reddit.com/api/v1/me"
basic_auth = True
settings = app_settings.PROVIDERS.get(provider_id, {})
# Allow custom User Agent to comply with reddit API limits
headers = {"User-Agent": settings.get("USER_AGENT", "django-allauth-header")}
def complete_login(self, request, app, token, **kwargs):
headers = {"Authorization": "bearer " + token.token}
headers.update(self.headers)
extra_data = requests.get(self.profile_url, headers=headers)
# This only here because of weird response from the test suite
if isinstance(extra_data, list):
extra_data = extra_data[0]
return self.get_provider().sociallogin_from_response(request, extra_data.json())
oauth2_login = OAuth2LoginView.adapter_view(RedditAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(RedditAdapter)