sources/oauth: improve default OAuth2 Client, send access_token as Bearer Authz

This commit is contained in:
Jens Langhammer 2020-02-18 21:49:23 +01:00
parent 4e8238603a
commit 4a05bc6e02
1 changed files with 13 additions and 6 deletions

View File

@ -1,6 +1,6 @@
"""OAuth Clients""" """OAuth Clients"""
import json import json
from typing import Dict
from urllib.parse import parse_qs, urlencode from urllib.parse import parse_qs, urlencode
from django.utils.crypto import constant_time_compare, get_random_string from django.utils.crypto import constant_time_compare, get_random_string
@ -33,7 +33,16 @@ class BaseOAuthClient:
def get_profile_info(self, raw_token): def get_profile_info(self, raw_token):
"Fetch user profile information." "Fetch user profile information."
try: try:
response = self.request("get", self.source.profile_url, token=raw_token) token = json.loads(raw_token)
headers = {
"Authorization": f"{token['token_type']} {token['access_token']}"
}
response = self.request(
"get",
self.source.profile_url,
token=token["access_token"],
headers=headers,
)
response.raise_for_status() response.raise_for_status()
except RequestException as exc: except RequestException as exc:
LOGGER.warning("Unable to fetch user profile", exc=exc) LOGGER.warning("Unable to fetch user profile", exc=exc)
@ -41,7 +50,7 @@ class BaseOAuthClient:
else: else:
return response.json() or response.text return response.json() or response.text
def get_redirect_args(self, request, callback): def get_redirect_args(self, request, callback) -> Dict[str, str]:
"Get request parameters for redirect url." "Get request parameters for redirect url."
raise NotImplementedError("Defined in a sub-class") # pragma: no cover raise NotImplementedError("Defined in a sub-class") # pragma: no cover
@ -64,9 +73,7 @@ class BaseOAuthClient:
@property @property
def session_key(self): def session_key(self):
""" """Return Session Key"""
Return Session Key
"""
raise NotImplementedError("Defined in a sub-class") # pragma: no cover raise NotImplementedError("Defined in a sub-class") # pragma: no cover