allauth: Add django-allauth extension
This commit is contained in:
parent
43fe9e062d
commit
08c907e89b
|
@ -0,0 +1,35 @@
|
|||
"""passbook provider"""
|
||||
from allauth.socialaccount.providers.base import ProviderAccount
|
||||
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
|
||||
|
||||
|
||||
class PassbookAccount(ProviderAccount):
|
||||
"""passbook account"""
|
||||
|
||||
def to_str(self):
|
||||
dflt = super().to_str()
|
||||
return self.account.extra_data.get('username', dflt)
|
||||
|
||||
|
||||
class PassbookProvider(OAuth2Provider):
|
||||
"""passbook provider"""
|
||||
|
||||
id = 'passbook'
|
||||
name = 'passbook'
|
||||
account_class = PassbookAccount
|
||||
|
||||
def extract_uid(self, data):
|
||||
return str(data['sub'])
|
||||
|
||||
def extract_common_fields(self, data):
|
||||
return {
|
||||
'email': data.get('email'),
|
||||
'username': data.get('preferred_username'),
|
||||
'name': data.get('name'),
|
||||
}
|
||||
|
||||
def get_default_scope(self):
|
||||
return ['openid:userinfo']
|
||||
|
||||
|
||||
provider_classes = [PassbookProvider] # noqa
|
|
@ -0,0 +1,6 @@
|
|||
"""passbook provider"""
|
||||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
|
||||
|
||||
from allauth_passbook.provider import PassbookProvider
|
||||
|
||||
urlpatterns = default_urlpatterns(PassbookProvider)
|
|
@ -0,0 +1,37 @@
|
|||
"""passbook adapter"""
|
||||
import requests
|
||||
from allauth.socialaccount import app_settings
|
||||
from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
|
||||
OAuth2CallbackView,
|
||||
OAuth2LoginView)
|
||||
|
||||
from allauth_passbook.provider import PassbookProvider
|
||||
|
||||
|
||||
class PassbookOAuth2Adapter(OAuth2Adapter):
|
||||
"""passbook OAuth2 Adapter"""
|
||||
provider_id = PassbookProvider.id
|
||||
# pylint: disable=no-member
|
||||
settings = app_settings.PROVIDERS.get(provider_id, {}) # noqa
|
||||
provider_base_url = settings.get("PASSBOOK_URL", 'https://id.beryju.org')
|
||||
|
||||
access_token_url = '{0}/application/oauth/token/'.format(provider_base_url)
|
||||
authorize_url = '{0}/application/oauth/authorize/'.format(provider_base_url)
|
||||
profile_url = '{0}/api/v1/openid/'.format(
|
||||
provider_base_url)
|
||||
|
||||
def complete_login(self, request, app, access_token, **kwargs):
|
||||
headers = {
|
||||
'Authorization': 'Bearer {0}'.format(access_token.token),
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
extra_data = requests.get(self.profile_url, headers=headers)
|
||||
|
||||
return self.get_provider().sociallogin_from_response(
|
||||
request,
|
||||
extra_data.json()
|
||||
)
|
||||
|
||||
|
||||
oauth2_login = OAuth2LoginView.adapter_view(PassbookOAuth2Adapter) # noqa
|
||||
oauth2_callback = OAuth2CallbackView.adapter_view(PassbookOAuth2Adapter) # noqa
|
|
@ -0,0 +1 @@
|
|||
django-allauth
|
|
@ -0,0 +1,33 @@
|
|||
"""passbook allauth setup.py"""
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='django-allauth-passbook',
|
||||
version='1.0.0',
|
||||
description='passbook support for django-allauth',
|
||||
# long_description='\n'.join(read_simple('docs/index.md')[2:]),
|
||||
long_description_content_type='text/markdown',
|
||||
author='BeryJu.org',
|
||||
author_email='hello@beryju.org',
|
||||
packages=['allauth_passbook'],
|
||||
include_package_data=True,
|
||||
install_requires=['django-allauth'],
|
||||
keywords='django allauth passbook',
|
||||
license='MIT',
|
||||
classifiers=[
|
||||
'Intended Audience :: Developers',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||
'Environment :: Web Environment',
|
||||
'Topic :: Internet',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Operating System :: OS Independent',
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Framework :: Django',
|
||||
'Framework :: Django :: 1.11',
|
||||
'Framework :: Django :: 2.0',
|
||||
'Framework :: Django :: 2.1',
|
||||
],
|
||||
)
|
Reference in New Issue