Skip to content

Commit

Permalink
feat(socialaccount): Add Wikimedia 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 c4c50cc
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 2 deletions.
Empty file.
58 changes: 58 additions & 0 deletions allauth/socialaccount/providers/wikimedia/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("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]
31 changes: 31 additions & 0 deletions allauth/socialaccount/providers/wikimedia/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 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"
}
""",
)
6 changes: 6 additions & 0 deletions allauth/socialaccount/providers/wikimedia/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 WikimediaProvider


urlpatterns = default_urlpatterns(WikimediaProvider)
36 changes: 36 additions & 0 deletions allauth/socialaccount/providers/wikimedia/views.py
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)
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ settings.py (Important - Please note 'django.contrib.sites' is required as INSTA
'allauth.socialaccount.providers.vk',
'allauth.socialaccount.providers.weibo',
'allauth.socialaccount.providers.weixin',
'allauth.socialaccount.providers.wikimedia',
'allauth.socialaccount.providers.windowslive',
'allauth.socialaccount.providers.xing',
'allauth.socialaccount.providers.yahoo',
Expand Down
2 changes: 2 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ Supported Providers

- Weixin (OAuth2)

- Wikimedia (OAuth2)

- Windows Live (OAuth2)

- Xing (OAuth)
Expand Down
27 changes: 25 additions & 2 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,8 @@ The following AccessManager settings are available:
'NETIQ_URL': 'https://my.identity.provider.example.org',
}
}
App registration (get your key and secret here) is done by the administrator of your NetIQ/Microfocus AccessManager.


Expand Down Expand Up @@ -1984,6 +1984,29 @@ registration). Other ``SCOPE`` options are: snsapi_base, snsapi_userinfo.
}
Wikimedia
---------

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

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

The following Wikimedia settings are available:

.. code-block:: python
SOCIALACCOUNT_PROVIDERS = {
'wikimedia': {
'DOMAIN': 'meta.wikimedia.org',
}
}
DOMAIN:
Domain of the Wikimedia wiki to use, e.g. ``en.wikipedia.org``.


Xing
----

Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"allauth.socialaccount.providers.vk",
"allauth.socialaccount.providers.weibo",
"allauth.socialaccount.providers.weixin",
"allauth.socialaccount.providers.wikimedia",
"allauth.socialaccount.providers.windowslive",
"allauth.socialaccount.providers.xing",
"allauth.socialaccount.providers.yahoo",
Expand Down

0 comments on commit c4c50cc

Please sign in to comment.