This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
django-allauth-authentic2/views.py

44 lines
1.4 KiB
Python

import urlparse
import requests
from django.core.exceptions import ImproperlyConfigured
from allauth.socialaccount.providers.oauth2.views import (OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView)
from .provider import Authentic2Provider
class Authentic2OAuth2Adapter(OAuth2Adapter):
provider_id = Authentic2Provider.id
def get_url(self):
provider = self.get_provider()
try:
return provider.get_settings()['URL']
except IndexError:
raise ImproperlyConfigured('The authentic2 provider needs an URL defined in settings')
@property
def access_token_url(self):
return urlparse.urljoin(self.get_url(), 'access_token')
@property
def authorize_url(self):
return urlparse.urljoin(self.get_url(), 'authorize')
@property
def profile_url(self):
return urlparse.urljoin(self.get_url(), 'user-info')
def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url,
headers={'authorization': 'Bearer %s' % token.token})
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request,
extra_data)
oauth2_login = OAuth2LoginView.adapter_view(Authentic2OAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(Authentic2OAuth2Adapter)