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 MediaWiki as a provider
- Loading branch information
Showing
10 changed files
with
163 additions
and
0 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("mediawiki", {}) | ||
|
||
|
||
class MediaWikiAccount(ProviderAccount): | ||
def get_profile_url(self): | ||
userpage = settings.get( | ||
"USERPAGE_TEMPLATE", "https://meta.wikimedia.org/wiki/User:{username}" | ||
) | ||
username = self.account.extra_data.get("username") | ||
if not username: | ||
return None | ||
return userpage.format(username=username.replace(" ", "_")) | ||
|
||
def to_str(self): | ||
dflt = super(MediaWikiAccount, self).to_str() | ||
return self.account.extra_data.get("username", dflt) | ||
|
||
|
||
class MediaWikiProvider(OAuth2Provider): | ||
id = "mediawiki" | ||
name = "MediaWiki" | ||
account_class = MediaWikiAccount | ||
|
||
@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 = [MediaWikiProvider] |
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 MediaWikiProvider | ||
|
||
|
||
class MediaWikiTests(OAuth2TestsMixin, TestCase): | ||
provider_id = MediaWikiProvider.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 MediaWikiProvider | ||
|
||
|
||
urlpatterns = default_urlpatterns(MediaWikiProvider) |
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,35 @@ | ||
import requests | ||
|
||
from django.conf import settings | ||
|
||
from allauth.socialaccount.providers.oauth2.views import ( | ||
OAuth2Adapter, | ||
OAuth2CallbackView, | ||
OAuth2LoginView, | ||
) | ||
|
||
from .provider import MediaWikiProvider | ||
|
||
|
||
settings = getattr(settings, "SOCIALACCOUNT_PROVIDERS", {}).get("mediawiki", {}) | ||
|
||
|
||
class MediaWikiOAuth2Adapter(OAuth2Adapter): | ||
provider_id = MediaWikiProvider.id | ||
REST_API = settings.get("REST_API", "https://meta.wikimedia.org/w/rest.php") | ||
access_token_url = REST_API + "/oauth2/access_token" | ||
authorize_url = REST_API + "/oauth2/authorize" | ||
profile_url = REST_API + "/oauth2/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(MediaWikiOAuth2Adapter) | ||
oauth2_callback = OAuth2CallbackView.adapter_view(MediaWikiOAuth2Adapter) |
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
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
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