Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ebay): Starting work on eBay OAuth Provider #3483

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
39 changes: 39 additions & 0 deletions allauth/socialaccount/providers/ebay/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class EbayAccount(ProviderAccount):
def to_str(self):
default = super(EbayAccount, self).to_str()
return self.account.extra_data.get("name", default)


class EbayProvider(OAuth2Provider):
id = "ebay"
name = "EBay"
account_class = EbayAccount

def get_default_scope(self):
return ["https://api.ebay.com/oauth/api_scope"]

def extract_uid(self, data):
return str(data.get("userid"))

def extract_common_fields(self, data):
common_fields = {
"userId": data.get("userId"),
"username": data.get("username"),
"accountType": data.get("accountType"),
}

account_type = data.get("accountType")
if account_type == "BUSINESS":
common_fields["email"] = data.get("businessAccount", {}).get("email")
elif account_type == "INDIVIDUAL":
common_fields["email"] = data.get("individualAccount", {}).get("email")

return common_fields


provider_classes = [EbayProvider]
68 changes: 68 additions & 0 deletions allauth/socialaccount/providers/ebay/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.test import override_settings
from allauth.socialaccount.providers.oauth2.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import EbayProvider


class EBayTests(OAuth2TestsMixin, TestCase):
provider_id = EbayProvider.id

def setUp(self):
self.app = SocialApp.objects.create(
provider=EbayProvider.id,
name="eBay",
client_id="test-client-id",
secret="test-secret",
)

def get_mocked_response(self, account_type="business"):
if account_type == "business":
response_content = """
{
"userId": "businessUser",
"username": "Business User",
"accountType": "BUSINESS",
"businessAccount": {
"email": "businessuser@example.com"
}
}
"""
else:
response_content = """
{
"userId": "individualUser",
"username": "Indivudal User",
"accountType": "INDIVIDUAL",
"individualAccount": {
"email": "studentuser@example.com"
}
}
"""
return MockedResponse(200, response_content)

@override_settings(EBAY_ENVIRONMENT="sandbox")
def test_login_sandbox(self):
response = self.client.get(reverse("ebay_login"))
self.assertEqual(
response.status_code, 302
) # Assuming a redirect to the eBay auth page

@override_settings(EBAY_ENVIRONMENT="production")
def test_login_production(self):
response = self.client.get(reverse("ebay_login"))
self.assertEqual(
response.status_code, 302
) # Assuming a redirect to the eBay auth page

def test_account_data_extraction(self):
for account_type in ["business", "student"]:
mocked_response = self.get_mocked_response(account_type)
self.assertEqual(
self.provider.extract_common_fields(mocked_response.json()),
{
"userId": f"{account_type}User",
"username": f"{account_type.capitalize()} User",
"accountType": account_type.upper(),
"email": f"{account_type}user@example.com",
},
)
6 changes: 6 additions & 0 deletions allauth/socialaccount/providers/ebay/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from allauth.socialaccount.providers.ebay.provider import EBayProvider
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns


urlpatterns = default_urlpatterns(EBayProvider)
57 changes: 57 additions & 0 deletions allauth/socialaccount/providers/ebay/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
import requests

from django.conf import settings

# from allauth.socialaccount import app_settings
from allauth.socialaccount.providers.ebay.provider import EBayProvider
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView,
)
from .provider import eBayProvider


class eBayOAuth2Adapter(OAuth2Adapter):
provider_id = eBayProvider.id

def __init__(self, request):
self.access_token_url = self.get_base_url() + "identity/v1/oauth2/token"
self.authorize_url = self.get_base_url() + "identity/v1/oauth2/authorize"
self.profile_url = self.get_base_url() + "commerce/identity/v1/user/"
# super().__init__(request)

def get_environment(self):
app = self.get_provider().app
return app.settings.get("environment", "production")

def get_base_url(self):
environment = self.get_environment()
if environment == "production":
return "https://api.ebay.com/"
else:
return "https://api.sandbox.ebay.com/"

def complete_login(self, request, app, token, **kwargs):
headers = {
"Authorization": f"Bearer {token.token}",
"Content-Type": "application/json",
"Accept": "application/json",
}

resp = requests.get(self.profile_url, headers=headers)
resp.raise_for_status()
data = resp.json()
# resp = requests.get(
# "https://api.ebay.com/oauth/api_scope/identity", headers=headers
# )

# resp.raise_for_status()

# data = resp.json()
return self.get_provider().sociallogin_from_response(request, data)


oauth2_login = OAuth2LoginView.adapter_view(eBayOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(eBayOAuth2Adapter)
3 changes: 3 additions & 0 deletions docs/socialaccount/providers/ebay.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
EBay

------------
1 change: 1 addition & 0 deletions docs/socialaccount/providers/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Provider Specifics
drip
dropbox
dwolla
ebay
edmodo
edx
eventbrite
Expand Down
2 changes: 1 addition & 1 deletion test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",

"allauth.account.middleware.AccountMiddleware",
)

Expand Down Expand Up @@ -93,6 +92,7 @@
"allauth.socialaccount.providers.drip",
"allauth.socialaccount.providers.dropbox",
"allauth.socialaccount.providers.dwolla",
"allauth.socialaccount.providers.ebay",
"allauth.socialaccount.providers.edmodo",
"allauth.socialaccount.providers.edx",
"allauth.socialaccount.providers.eventbrite",
Expand Down