Skip to content

Commit

Permalink
feat(socialaccount/providers): Added Mixer provider
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed May 16, 2020
1 parent c40d4c4 commit 278c0ae
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Marjori Pomarole
Markus Kaiserswerth
Markus Thielen
Martin Bächtold
Matt Nishi-Broach
Mauro Stettler
Morgante Pell
Nariman Gharib
Expand Down
2 changes: 1 addition & 1 deletion ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Note worthy changes
-------------------

- New provider: Yandex (OAuth2)
- New providers: Yandex, Mixer.

- Fixed Twitch ``get_avatar_url()`` method to use the profile picture retrieved
by new user details endpoint introduced in version 0.40.0.
Expand Down
Empty file.
37 changes: 37 additions & 0 deletions allauth/socialaccount/providers/mixer/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class MixerAccount(ProviderAccount):
def _get_token(self):
return self.account.extra_data.get('channel', {}).get('token')

def get_profile_url(self):
return 'https://mixer.com/' + self._get_token()

def get_avatar_url(self):
return self.account.extra_data.get('avatarUrl')

def to_str(self):
return self._get_token()


class MixerProvider(OAuth2Provider):
id = 'mixer'
name = 'Mixer'
account_class = MixerAccount

def get_default_scope(self):
return ['user:details:self']

def extract_uid(self, data):
return str(data['id'])

def extract_common_fields(self, data):
return {
'username': data.get('username'),
'email': data.get('email'),
}


provider_classes = [MixerProvider]
45 changes: 45 additions & 0 deletions allauth/socialaccount/providers/mixer/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase

from .provider import MixerProvider


class MixerTests(OAuth2TestsMixin, TestCase):
provider_id = MixerProvider.id

def get_mocked_response(self):
return MockedResponse(200, """
{
"avatarUrl": "https://uploads.mixer.com/avatar/2evg7cnb.jpg",
"id": 101052282,
"bio": "Ninja is the one of the world's most popular gamers.",
"channel": {
"id": 90571077,
"audience": "teen",
"token": "ninja",
"viewersCurrent": 0,
"viewersTotal": 43349053,
"numFollowers": 2840127,
"name": "TEM TEM TIME",
"online": false,
"languageId": "en"
},
"createdAt": "2019-08-01T16:45:00.000Z",
"deleatedAt": null,
"experience": 401922,
"frontendVersion": null,
"groups": [
{"id": 1, "name": "User"},
{"id": 19, "name": "Partner"},
{"id": 12, "name": "Pro"}
],
"level": 103,
"primaryTeam": null,
"social": {
"facebook": "https://www.facebook.com/NinjaTB"
},
"sparks": 7874598,
"updatedAt": "2020-01-06T19:26:43.640Z",
"username": "Ninja",
"verified": true
}""")
6 changes: 6 additions & 0 deletions allauth/socialaccount/providers/mixer/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 MixerProvider


urlpatterns = default_urlpatterns(MixerProvider)
31 changes: 31 additions & 0 deletions allauth/socialaccount/providers/mixer/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import requests

from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)

from .provider import MixerProvider


class MixerOAuth2Adapter(OAuth2Adapter):
provider_id = MixerProvider.id
access_token_url = 'https://mixer.com/api/v1/oauth/token'
authorize_url = 'https://mixer.com/oauth/authorize'
profile_url = 'https://mixer.com/api/v1/users/current'

def complete_login(self, request, app, token, **kwargs):
headers = {'Authorization': 'Bearer {}'.format(token.token)}
response = requests.get(self.profile_url, headers=headers)
response.raise_for_status()

data = response.json()

return self.get_provider().sociallogin_from_response(
request, data
)


oauth2_login = OAuth2LoginView.adapter_view(MixerOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(MixerOAuth2Adapter)
1 change: 1 addition & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ settings.py (Important - Please note 'django.contrib.sites' is required as INSTA
'allauth.socialaccount.providers.mailchimp',
'allauth.socialaccount.providers.meetup',
'allauth.socialaccount.providers.microsoft',
'allauth.socialaccount.providers.mixer',
'allauth.socialaccount.providers.naver',
'allauth.socialaccount.providers.nextcloud',
'allauth.socialaccount.providers.odnoklassniki',
Expand Down
2 changes: 2 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Supported Providers

- Microsoft (Graph) (OAuth2)

- Mixer (OAuth2)

- NextCloud (OAuth2)

- Naver (OAuth2)
Expand Down
27 changes: 27 additions & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,33 @@ for the login. To restrict it, change the `tenant` setting as shown below.
}
Mixer
-----

API documentation
https://dev.mixer.com/guides/core/introduction

App registration (get your key and secret here)
https://mixer.com/lab/oauth

Development callback URL
http://localhost:8000/accounts/mixer/login/callback/

You can change scopes for Mixer using the ``SCOPE`` parameter. For example, to add the ability to edit your mixer profile, you'd use:

.. code-block:: python
SOCIALACCOUNT_PROVIDERS = {
'mixer': {
'SCOPE': [
'user:details:self',
'user:update:self',
]
}
}
The default scope list is ``['user:details:self']``, which is required to get your email address from Mixer. The full list of scopes is available at https://dev.mixer.com/reference/oauth/scopes

Naver
-----

Expand Down
1 change: 1 addition & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
'allauth.socialaccount.providers.mailru',
'allauth.socialaccount.providers.meetup',
'allauth.socialaccount.providers.microsoft',
'allauth.socialaccount.providers.mixer',
'allauth.socialaccount.providers.naver',
'allauth.socialaccount.providers.nextcloud',
'allauth.socialaccount.providers.odnoklassniki',
Expand Down

0 comments on commit 278c0ae

Please sign in to comment.