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,34 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class PaypalAccount(ProviderAccount):
def get_avatar_url(self):
return self.account.extra_data.get("picture")
def to_str(self):
return self.account.extra_data.get("name", super(PaypalAccount, self).to_str())
class PaypalProvider(OAuth2Provider):
id = "paypal"
name = "Paypal"
account_class = PaypalAccount
def get_default_scope(self):
# See: https://developer.paypal.com/docs/integration/direct/identity/attributes/ # noqa
return ["openid", "email"]
def extract_uid(self, data):
return str(data["user_id"])
def extract_common_fields(self, data):
# See: https://developer.paypal.com/docs/api/#get-user-information
return dict(
first_name=data.get("given_name", ""),
last_name=data.get("family_name", ""),
email=data.get("email"),
)
provider_classes = [PaypalProvider]

View File

@@ -0,0 +1,23 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import PaypalProvider
class PaypalTests(OAuth2TestsMixin, TestCase):
provider_id = PaypalProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{
"user_id":
"https://www.paypal.com/webapps/auth/server/64ghr894040044",
"name": "Jane Doe",
"given_name": "Jane",
"family_name": "Doe",
"email": "janedoe@example.com"
}
""",
)

View File

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

View File

@@ -0,0 +1,48 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import PaypalProvider
class PaypalOAuth2Adapter(OAuth2Adapter):
provider_id = PaypalProvider.id
supports_state = False
@property
def authorize_url(self):
path = "webapps/auth/protocol/openidconnect/v1/authorize"
return "https://www.{0}/{1}".format(self._get_endpoint(), path)
@property
def access_token_url(self):
path = "v1/identity/openidconnect/tokenservice"
return "https://api.{0}/{1}".format(self._get_endpoint(), path)
@property
def profile_url(self):
path = "v1/identity/openidconnect/userinfo"
return "https://api.{0}/{1}".format(self._get_endpoint(), path)
def _get_endpoint(self):
settings = self.get_provider().get_settings()
if settings.get("MODE") == "live":
return "paypal.com"
else:
return "sandbox.paypal.com"
def complete_login(self, request, app, token, **kwargs):
response = requests.post(
self.profile_url,
params={"schema": "openid", "access_token": token},
)
extra_data = response.json()
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(PaypalOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(PaypalOAuth2Adapter)