Files
benchcoach-django/teamsnap/views.py
2022-06-01 15:30:52 -05:00

47 lines
1.7 KiB
Python

import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import TeamsnapProvider
class TeamsnapAdapter(OAuth2Adapter):
provider_id = TeamsnapProvider.id
# Fetched programmatically, must be reachable from container
access_token_url = "{}/oauth/token/".format("https://auth.teamsnap.com")
profile_url = "{}/me/".format("https://api.teamsnap.com/v3/")
# Accessed by the user browser, must be reachable by the host
authorize_url = "{}/oauth/authorize/".format("https://auth.teamsnap.com/")
# NOTE: trailing slashes in URLs are important, don't miss it
def complete_login(self, request, app, token, **kwargs):
headers = {"Authorization": f"Bearer {token.token}"}
resp = requests.get(self.profile_url, headers=headers)
j = resp.json()
if j.get("collection", {}).get("items"):
extra_data = {
i["name"]: i["value"] for i in j["collection"]["items"][0]["data"]
}
return self.get_provider().sociallogin_from_response(request, extra_data)
def populate_user(self, request, sociallogin, data):
user = super().populate_user(request, sociallogin, data)
user.username = user.email
return user
# def get_callback_url(self, request, app):
# callback_url = reverse(self.provider_id + "_callback")
# protocol = self.redirect_uri_protocol
# return build_absolute_uri(request, callback_url, protocol)
# return "urn:ietf:wg:oauth:2.0:oob"
oauth2_login = OAuth2LoginView.adapter_view(TeamsnapAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter)