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/providers): Added Mixer provider
- Loading branch information
Showing
11 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
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
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,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] |
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,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 | ||
}""") |
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 MixerProvider | ||
|
||
|
||
urlpatterns = default_urlpatterns(MixerProvider) |
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 @@ | ||
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) |
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