Skip to content

Commit

Permalink
feat(socialaccount): Add MediaWiki as a provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tgr committed May 23, 2021
1 parent 9f52b43 commit c4faf77
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 0 deletions.
Empty file.
58 changes: 58 additions & 0 deletions allauth/socialaccount/providers/mediawiki/provider.py
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]
31 changes: 31 additions & 0 deletions allauth/socialaccount/providers/mediawiki/tests.py
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"
}
""",
)
6 changes: 6 additions & 0 deletions allauth/socialaccount/providers/mediawiki/urls.py
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)
35 changes: 35 additions & 0 deletions allauth/socialaccount/providers/mediawiki/views.py
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)
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ settings.py (Important - Please note 'django.contrib.sites' is required as INSTA
'allauth.socialaccount.providers.linkedin_oauth2',
'allauth.socialaccount.providers.mailchimp',
'allauth.socialaccount.providers.mailru',
'allauth.socialaccount.providers.mediawiki',
'allauth.socialaccount.providers.meetup',
'allauth.socialaccount.providers.microsoft',
'allauth.socialaccount.providers.naver',
Expand Down
2 changes: 2 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Supported Providers

- MailChimp (OAuth2)

- MediaWiki (OAuth2)

- Meetup (OAuth2)

- Microsoft (Graph) (OAuth2)
Expand Down
28 changes: 28 additions & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,34 @@ browsers may require enabling this on localhost and not support by default and
ask for permission.


MediaWiki
---------

MediaWiki OAuth2 documentation:
https://www.mediawiki.org/wiki/OAuth/For_Developers

The following MediaWiki settings are available:

.. code-block:: python
SOCIALACCOUNT_PROVIDERS = {
'mediawiki': {
'REST_API': 'https://meta.wikimedia.org/w/rest.php',
'USERPAGE_TEMPLATE': 'https://meta.wikimedia.org/wiki/{username}',
}
}
REST_API:
Base URL of the MediaWiki site's REST API.
USERPAGE_TEMPLATE:
Link template for linking to users. Must have a ``{username}`` format field.

With the default settings, Wikimedia user identities (meta.wikimedia.org) will be used.

App registration for Wikimedia wikis:
https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose


Microsoft Graph
-----------------

Expand Down
1 change: 1 addition & 0 deletions example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.linkedin',
'allauth.socialaccount.providers.mediawiki',
'allauth.socialaccount.providers.openid',
'allauth.socialaccount.providers.persona',
'allauth.socialaccount.providers.reddit',
Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"allauth.socialaccount.providers.linkedin_oauth2",
"allauth.socialaccount.providers.mailchimp",
"allauth.socialaccount.providers.mailru",
"allauth.socialaccount.providers.mediawiki",
"allauth.socialaccount.providers.meetup",
"allauth.socialaccount.providers.microsoft",
"allauth.socialaccount.providers.naver",
Expand Down

0 comments on commit c4faf77

Please sign in to comment.