forked from pennersr/django-allauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(socialaccount): Add Wikimedia as a provider
- Loading branch information
Showing
9 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Optional | ||
|
||
from django.conf import settings | ||
|
||
from allauth.account.models import EmailAddress | ||
from allauth.socialaccount.providers.base import ProviderAccount | ||
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider | ||
|
||
|
||
settings = getattr(settings, "SOCIALACCOUNT_PROVIDERS", {}).get("wikimedia", {}) | ||
|
||
|
||
class WikimediaAccount(ProviderAccount): | ||
def get_profile_url(self): | ||
domain = settings.get("DOMAIN", "meta.wikimedia.org") | ||
username = self.account.extra_data.get("username") | ||
if not username: | ||
return None | ||
return "https://{domain}/wiki/User:{username}".format( | ||
domain=domain, username=username.replace(" ", "_") | ||
) | ||
|
||
def to_str(self): | ||
dflt = super(WikimediaAccount, self).to_str() | ||
return self.account.extra_data.get("username", dflt) | ||
|
||
|
||
class WikimediaProvider(OAuth2Provider): | ||
id = "wikimedia" | ||
name = "Wikimedia" | ||
account_class = WikimediaAccount | ||
|
||
@staticmethod | ||
def _get_email(data: dict) -> Optional[str]: | ||
if data.get("confirmed_email"): | ||
return data.get("email") | ||
return None | ||
|
||
def extract_uid(self, data): | ||
return str(data["sub"]) | ||
|
||
def extract_extra_data(self, data): | ||
return dict( | ||
username=data.get("username"), | ||
) | ||
|
||
def extract_common_fields(self, data): | ||
return dict( | ||
email=self._get_email(data), | ||
username=data.get("username"), | ||
name=data.get("realname"), | ||
) | ||
|
||
def extract_email_addresses(self, data): | ||
return [EmailAddress(email=self._get_email(data), verified=True, primary=True)] | ||
|
||
|
||
provider_classes = [WikimediaProvider] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from allauth.socialaccount.tests import OAuth2TestsMixin | ||
from allauth.tests import MockedResponse, TestCase | ||
|
||
from .provider import WikimediaProvider | ||
|
||
|
||
class WikimediaTests(OAuth2TestsMixin, TestCase): | ||
provider_id = WikimediaProvider.id | ||
|
||
def get_mocked_response(self): | ||
return MockedResponse( | ||
200, | ||
""" | ||
{ | ||
"iss": "https://meta.wikimedia.org", | ||
"sub": 12345, | ||
"aud": "1234567890abcdef", | ||
"exp": 946681300, | ||
"iat": 946681200, | ||
"username": "John Doe", | ||
"editcount": 123, | ||
"confirmed_email": true, | ||
"blocked": false, | ||
"registered": "20000101000000", | ||
"groups": ["*", "user", "autoconfirmed"], | ||
"rights": ["read", "edit"], | ||
"grants": ["basic"], | ||
"email": "johndoe@example.com" | ||
} | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns | ||
|
||
from .provider import WikimediaProvider | ||
|
||
|
||
urlpatterns = default_urlpatterns(WikimediaProvider) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import requests | ||
|
||
from django.conf import settings | ||
|
||
from allauth.socialaccount.providers.oauth2.views import ( | ||
OAuth2Adapter, | ||
OAuth2CallbackView, | ||
OAuth2LoginView, | ||
) | ||
|
||
from .provider import WikimediaProvider | ||
|
||
|
||
settings = getattr(settings, "SOCIALACCOUNT_PROVIDERS", {}).get("wikimedia", {}) | ||
|
||
|
||
class WikimediaOAuth2Adapter(OAuth2Adapter): | ||
provider_id = WikimediaProvider.id | ||
domain = settings.get("DOMAIN", "meta.wikimedia.org") | ||
OAUTH_API = "https://{domain}/w/rest.php/oauth2".format(domain=domain) | ||
access_token_url = OAUTH_API + "/access_token" | ||
authorize_url = OAUTH_API + "/authorize" | ||
profile_url = OAUTH_API + "/resource/profile" | ||
|
||
def complete_login(self, request, app, token, **kwargs): | ||
resp = requests.get( | ||
self.profile_url, | ||
headers={"Authorization": "Bearer {token}".format(token=token.token)}, | ||
) | ||
resp.raise_for_status() | ||
extra_data = resp.json() | ||
return self.get_provider().sociallogin_from_response(request, extra_data) | ||
|
||
|
||
oauth2_login = OAuth2LoginView.adapter_view(WikimediaOAuth2Adapter) | ||
oauth2_callback = OAuth2CallbackView.adapter_view(WikimediaOAuth2Adapter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,6 +219,8 @@ Supported Providers | |
|
||
- Weixin (OAuth2) | ||
|
||
- Wikimedia (OAuth2) | ||
|
||
- Windows Live (OAuth2) | ||
|
||
- Xing (OAuth) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters