Skip to content

Commit

Permalink
feat(socialaccount): adapter.get_requests_session()
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Dec 8, 2023
1 parent 8324320 commit 170530a
Show file tree
Hide file tree
Showing 116 changed files with 781 additions and 497 deletions.
4 changes: 4 additions & 0 deletions allauth/socialaccount/adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import requests
import warnings

from django.core.exceptions import (
Expand Down Expand Up @@ -291,6 +292,9 @@ def get_app(self, request, provider, client_id=None):
raise SocialApp.DoesNotExist()
return apps[0]

def get_requests_session(self):
return requests.Session()


def get_adapter(request=None):
return import_attribute(app_settings.ADAPTER)(request)
19 changes: 11 additions & 8 deletions allauth/socialaccount/providers/agave/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.agave.provider import AgaveProvider
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
Expand All @@ -20,12 +19,16 @@ class AgaveAdapter(OAuth2Adapter):
profile_url = "{0}/profiles/v2/me".format(provider_base_url)

def complete_login(self, request, app, token, response):
extra_data = requests.get(
self.profile_url,
params={"access_token": token.token},
headers={
"Authorization": "Bearer " + token.token,
},
extra_data = (
get_adapter()
.get_requests_session()
.get(
self.profile_url,
params={"access_token": token.token},
headers={
"Authorization": "Bearer " + token.token,
},
)
)

user_profile = (
Expand Down
9 changes: 6 additions & 3 deletions allauth/socialaccount/providers/amazon/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -17,7 +16,11 @@ class AmazonOAuth2Adapter(OAuth2Adapter):
supports_state = False

def complete_login(self, request, app, token, **kwargs):
response = requests.get(self.profile_url, params={"access_token": token})
response = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token})
)
extra_data = response.json()
if "Profile" in extra_data:
extra_data = {
Expand Down
7 changes: 4 additions & 3 deletions allauth/socialaccount/providers/amazon_cognito/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.amazon_cognito.provider import (
AmazonCognitoProvider,
)
Expand Down Expand Up @@ -47,7 +46,9 @@ def complete_login(self, request, app, access_token, **kwargs):
headers = {
"Authorization": "Bearer {}".format(access_token),
}
extra_data = requests.get(self.profile_url, headers=headers)
extra_data = (
get_adapter().get_requests_session().get(self.profile_url, headers=headers)
)
extra_data.raise_for_status()

return self.get_provider().sociallogin_from_response(request, extra_data.json())
Expand Down
9 changes: 6 additions & 3 deletions allauth/socialaccount/providers/angellist/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -17,7 +16,11 @@ class AngelListOAuth2Adapter(OAuth2Adapter):
supports_state = False

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)

Expand Down
7 changes: 4 additions & 3 deletions allauth/socialaccount/providers/apple/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
from datetime import datetime, timedelta
from urllib.parse import parse_qsl, quote, urlencode

Expand Down Expand Up @@ -72,8 +71,10 @@ def get_access_token(self, code, pkce_code_verifier=None):
if pkce_code_verifier:
data["code_verifier"] = pkce_code_verifier
self._strip_empty_keys(data)
resp = requests.request(
self.access_token_method, url, data=data, headers=self.headers
resp = (
get_adapter()
.get_requests_session()
.request(self.access_token_method, url, data=data, headers=self.headers)
)
access_token = None
if resp.status_code in [200, 201]:
Expand Down
3 changes: 1 addition & 2 deletions allauth/socialaccount/providers/apple/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import requests
from datetime import timedelta

from django.http import HttpResponseNotAllowed, HttpResponseRedirect
Expand Down Expand Up @@ -33,7 +32,7 @@ class AppleOAuth2Adapter(OAuth2Adapter):
public_key_url = "https://appleid.apple.com/auth/keys"

def _get_apple_public_key(self, kid):
response = requests.get(self.public_key_url)
response = get_adapter().get_requests_session().get(self.public_key_url)
response.raise_for_status()
try:
data = response.json()
Expand Down
9 changes: 6 additions & 3 deletions allauth/socialaccount/providers/asana/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -16,7 +15,11 @@ class AsanaOAuth2Adapter(OAuth2Adapter):
profile_url = "https://app.asana.com/api/1.0/users/me"

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)
extra_data = resp.json()["data"]
return self.get_provider().sociallogin_from_response(request, extra_data)

Expand Down
12 changes: 7 additions & 5 deletions allauth/socialaccount/providers/auth0/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import requests

from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.auth0.provider import Auth0Provider
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
Expand All @@ -22,9 +21,12 @@ class Auth0OAuth2Adapter(OAuth2Adapter):
profile_url = "{0}/userinfo".format(provider_base_url)

def complete_login(self, request, app, token, response):
extra_data = requests.get(
self.profile_url, params={"access_token": token.token}
).json()
extra_data = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
.json()
)
return self.get_provider().sociallogin_from_response(request, extra_data)


Expand Down
4 changes: 2 additions & 2 deletions allauth/socialaccount/providers/authentiq/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
from urllib.parse import urljoin

from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -26,7 +26,7 @@ class AuthentiqOAuth2Adapter(OAuth2Adapter):

def complete_login(self, request, app, token, **kwargs):
auth = {"Authorization": "Bearer " + token.token}
resp = requests.get(self.profile_url, headers=auth)
resp = get_adapter().get_requests_session().get(self.profile_url, headers=auth)
resp.raise_for_status()
extra_data = resp.json()
login = self.get_provider().sociallogin_from_response(request, extra_data)
Expand Down
9 changes: 6 additions & 3 deletions allauth/socialaccount/providers/baidu/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -18,7 +17,11 @@ class BaiduOAuth2Adapter(OAuth2Adapter):
)

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)

Expand Down
7 changes: 4 additions & 3 deletions allauth/socialaccount/providers/basecamp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -19,7 +18,9 @@ class BasecampOAuth2Adapter(OAuth2Adapter):

def complete_login(self, request, app, token, **kwargs):
headers = {"Authorization": "Bearer {0}".format(token.token)}
resp = requests.get(self.profile_url, headers=headers)
resp = (
get_adapter().get_requests_session().get(self.profile_url, headers=headers)
)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)

Expand Down
7 changes: 4 additions & 3 deletions allauth/socialaccount/providers/battlenet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
* The Battle.net API forum:
https://us.battle.net/en/forum/15051532/
"""
import requests

from django.conf import settings

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Error
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
Expand Down Expand Up @@ -131,7 +130,9 @@ def profile_url(self):

def complete_login(self, request, app, token, **kwargs):
params = {"access_token": token.token}
response = requests.get(self.profile_url, params=params)
response = (
get_adapter().get_requests_session().get(self.profile_url, params=params)
)
data = _check_errors(response)

# Add the region to the data so that we can have it in `extra_data`.
Expand Down
15 changes: 11 additions & 4 deletions allauth/socialaccount/providers/bitbucket_oauth2/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests

from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -18,15 +17,23 @@ class BitbucketOAuth2Adapter(OAuth2Adapter):
emails_url = "https://api.bitbucket.org/2.0/user/emails"

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)
extra_data = resp.json()
if app_settings.QUERY_EMAIL and not extra_data.get("email"):
extra_data["email"] = self.get_email(token)
return self.get_provider().sociallogin_from_response(request, extra_data)

def get_email(self, token):
"""Fetches email address from email API endpoint"""
resp = requests.get(self.emails_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.emails_url, params={"access_token": token.token})
)
emails = resp.json().get("values", [])
email = ""
try:
Expand Down
9 changes: 6 additions & 3 deletions allauth/socialaccount/providers/bitly/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -17,7 +16,11 @@ class BitlyOAuth2Adapter(OAuth2Adapter):
supports_state = False

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"access_token": token.token})
resp = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)
extra_data = resp.json()["data"]
return self.get_provider().sociallogin_from_response(request, extra_data)

Expand Down
9 changes: 5 additions & 4 deletions allauth/socialaccount/providers/box/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -17,8 +16,10 @@ class BoxOAuth2Adapter(OAuth2Adapter):
redirect_uri_protocol = None

def complete_login(self, request, app, token, **kwargs):
extra_data = requests.get(
self.profile_url, params={"access_token": token.token}
extra_data = (
get_adapter()
.get_requests_session()
.get(self.profile_url, params={"access_token": token.token})
)

# This only here because of weird response from the test suite
Expand Down
13 changes: 8 additions & 5 deletions allauth/socialaccount/providers/cilogon/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
Expand All @@ -16,9 +15,13 @@ class CILogonOAuth2Adapter(OAuth2Adapter):
profile_url = "https://cilogon.org/oauth2/userinfo"

def complete_login(self, request, app, token, **kwargs):
resp = requests.get(
self.profile_url,
params={"access_token": token.token, "alt": "json"},
resp = (
get_adapter()
.get_requests_session()
.get(
self.profile_url,
params={"access_token": token.token, "alt": "json"},
)
)
resp.raise_for_status()
extra_data = resp.json()
Expand Down
21 changes: 14 additions & 7 deletions allauth/socialaccount/providers/clever/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests

from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.providers.oauth2.client import OAuth2Error
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
Expand All @@ -26,16 +25,24 @@ def complete_login(self, request, app, token, **kwargs):

def get_data(self, token):
# Verify the user first
resp = requests.get(
self.identity_url, headers={"Authorization": "Bearer {}".format(token)}
resp = (
get_adapter()
.get_requests_session()
.get(
self.identity_url, headers={"Authorization": "Bearer {}".format(token)}
)
)
if resp.status_code != 200:
raise OAuth2Error()
resp = resp.json()
user_id = resp["data"]["id"]
user_details = requests.get(
"{}/{}".format(self.user_details_url, user_id),
headers={"Authorization": "Bearer {}".format(token)},
user_details = (
get_adapter()
.get_requests_session()
.get(
"{}/{}".format(self.user_details_url, user_id),
headers={"Authorization": "Bearer {}".format(token)},
)
)
user_details.raise_for_status()
user_details = user_details.json()
Expand Down
Loading

0 comments on commit 170530a

Please sign in to comment.