| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import requests |
| 4 |
from http import HTTPStatus |
| 5 |
from urllib.parse import parse_qsl |
| 6 |
|
| 7 |
from django.http import HttpRequest |
| 8 |
from django.utils.http import urlencode |
| 9 |
|
| 10 |
from allauth.socialaccount.adapter import get_adapter |
| 11 |
|
| 12 |
|
| 13 |
class OAuth2Error(Exception): |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
class OAuth2Client: |
| 18 |
client_id_parameter = "client_id" |
| 19 |
|
| 20 |
def __init__( |
| 21 |
self, |
| 22 |
request: HttpRequest, |
| 23 |
consumer_key, |
| 24 |
consumer_secret, |
| 25 |
access_token_method, |
| 26 |
access_token_url, |
| 27 |
callback_url, |
| 28 |
scope_delimiter=" ", |
| 29 |
headers=None, |
| 30 |
basic_auth=False, |
| 31 |
) -> None: |
| 32 |
self.request = request |
| 33 |
self.access_token_method = access_token_method |
| 34 |
self.access_token_url = access_token_url |
| 35 |
self.callback_url = callback_url |
| 36 |
self.consumer_key = consumer_key |
| 37 |
self.consumer_secret = consumer_secret |
| 38 |
self.scope_delimiter = scope_delimiter |
| 39 |
self.state = None |
| 40 |
self.headers = headers |
| 41 |
self.basic_auth = basic_auth |
| 42 |
|
| 43 |
def get_redirect_url(self, authorization_url, scope, extra_params) -> str: |
| 44 |
scope = self.scope_delimiter.join(set(scope)) |
| 45 |
params = { |
| 46 |
self.client_id_parameter: self.consumer_key, |
| 47 |
"redirect_uri": self.callback_url, |
| 48 |
"scope": scope, |
| 49 |
"response_type": "code", |
| 50 |
} |
| 51 |
if self.state: |
| 52 |
params["state"] = self.state |
| 53 |
params.update(extra_params) |
| 54 |
return f"{authorization_url}?{urlencode(params)}" |
| 55 |
|
| 56 |
def get_access_token(self, code, pkce_code_verifier=None, extra_data=None): |
| 57 |
data = { |
| 58 |
"redirect_uri": self.callback_url, |
| 59 |
"grant_type": "authorization_code", |
| 60 |
"code": code, |
| 61 |
} |
| 62 |
if self.basic_auth: |
| 63 |
auth = requests.auth.HTTPBasicAuth(self.consumer_key, self.consumer_secret) |
| 64 |
else: |
| 65 |
auth = None |
| 66 |
data.update( |
| 67 |
{ |
| 68 |
self.client_id_parameter: self.consumer_key, |
| 69 |
"client_secret": self.consumer_secret, |
| 70 |
} |
| 71 |
) |
| 72 |
if extra_data: |
| 73 |
data.update(extra_data) |
| 74 |
params = None |
| 75 |
self._strip_empty_keys(data) |
| 76 |
url = self.access_token_url |
| 77 |
if self.access_token_method == "GET": # nosec |
| 78 |
params = data |
| 79 |
data = None |
| 80 |
if data and pkce_code_verifier: |
| 81 |
data["code_verifier"] = pkce_code_verifier |
| 82 |
# TODO: Proper exception handling |
| 83 |
with get_adapter().get_requests_session() as sess: |
| 84 |
resp = sess.request( |
| 85 |
self.access_token_method, |
| 86 |
url, |
| 87 |
params=params, |
| 88 |
data=data, |
| 89 |
headers=self.headers, |
| 90 |
auth=auth, |
| 91 |
) |
| 92 |
access_token = None |
| 93 |
if resp.status_code in [HTTPStatus.OK, HTTPStatus.CREATED]: |
| 94 |
# Weibo sends json via 'text/plain;charset=UTF-8' |
| 95 |
if ( |
| 96 |
resp.headers["content-type"].split(";")[0] == "application/json" |
| 97 |
or resp.text[:2] == '{"' |
| 98 |
): |
| 99 |
access_token = resp.json() |
| 100 |
else: |
| 101 |
access_token = dict(parse_qsl(resp.text)) |
| 102 |
if not access_token or "access_token" not in access_token: |
| 103 |
raise OAuth2Error(f"Error retrieving access token: {resp.content}") |
| 104 |
return access_token |
| 105 |
|
| 106 |
def _strip_empty_keys(self, params) -> None: |
| 107 |
"""Added because the Dropbox OAuth2 flow doesn't |
| 108 |
work when scope is passed in, which is empty. |
| 109 |
""" |
| 110 |
keys = [k for k, v in params.items() if v == ""] |
| 111 |
for key in keys: |
| 112 |
del params[key] |