From a2725d5b82e0d5bbca40c344115c556019e614e5 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 18 Feb 2020 21:49:40 +0100 Subject: [PATCH] sources/oauth: remove redundant OAuth2Clients --- passbook/sources/oauth/types/azure_ad.py | 26 ------------------ passbook/sources/oauth/types/discord.py | 34 ------------------------ passbook/sources/oauth/types/facebook.py | 1 - passbook/sources/oauth/types/github.py | 1 - passbook/sources/oauth/types/reddit.py | 26 ------------------ passbook/sources/oauth/types/twitter.py | 26 ------------------ 6 files changed, 114 deletions(-) diff --git a/passbook/sources/oauth/types/azure_ad.py b/passbook/sources/oauth/types/azure_ad.py index c1d480164..0ac83f2c6 100644 --- a/passbook/sources/oauth/types/azure_ad.py +++ b/passbook/sources/oauth/types/azure_ad.py @@ -1,41 +1,15 @@ """AzureAD OAuth2 Views""" -import json import uuid -from requests.exceptions import RequestException -from structlog import get_logger - -from passbook.sources.oauth.clients import OAuth2Client from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback -LOGGER = get_logger() - - -class AzureADOAuth2Client(OAuth2Client): - """AzureAD OAuth2 Client""" - - def get_profile_info(self, raw_token): - "Fetch user profile information." - try: - token = json.loads(raw_token)["access_token"] - headers = {"Authorization": "Bearer %s" % token} - response = self.request("get", self.source.profile_url, headers=headers) - response.raise_for_status() - except RequestException as exc: - LOGGER.warning("Unable to fetch user profile", exc=exc) - return None - else: - return response.json() or response.text - @MANAGER.source(kind=RequestKind.callback, name="Azure AD") class AzureADOAuthCallback(OAuthCallback): """AzureAD OAuth2 Callback""" - client_class = AzureADOAuth2Client - def get_user_id(self, source, info): return uuid.UUID(info.get("objectId")).int diff --git a/passbook/sources/oauth/types/discord.py b/passbook/sources/oauth/types/discord.py index a7b26db46..f82089cf5 100644 --- a/passbook/sources/oauth/types/discord.py +++ b/passbook/sources/oauth/types/discord.py @@ -1,16 +1,8 @@ """Discord OAuth Views""" -import json - -from requests.exceptions import RequestException -from structlog import get_logger - -from passbook.sources.oauth.clients import OAuth2Client from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect -LOGGER = get_logger() - @MANAGER.source(kind=RequestKind.redirect, name="Discord") class DiscordOAuthRedirect(OAuthRedirect): @@ -22,36 +14,10 @@ class DiscordOAuthRedirect(OAuthRedirect): } -class DiscordOAuth2Client(OAuth2Client): - """Discord OAuth2 Client""" - - def get_profile_info(self, raw_token): - "Fetch user profile information." - try: - token = json.loads(raw_token) - headers = { - "Authorization": "%s %s" % (token["token_type"], token["access_token"]) - } - response = self.request( - "get", - self.source.profile_url, - token=token["access_token"], - headers=headers, - ) - response.raise_for_status() - except RequestException as exc: - LOGGER.warning("Unable to fetch user profile", exc=exc) - return None - else: - return response.json() or response.text - - @MANAGER.source(kind=RequestKind.callback, name="Discord") class DiscordOAuth2Callback(OAuthCallback): """Discord OAuth2 Callback""" - client_class = DiscordOAuth2Client - def get_or_create_user(self, source, access, info): user_data = { "username": info.get("username"), diff --git a/passbook/sources/oauth/types/facebook.py b/passbook/sources/oauth/types/facebook.py index ab21a2e79..8b20cf3ee 100644 --- a/passbook/sources/oauth/types/facebook.py +++ b/passbook/sources/oauth/types/facebook.py @@ -1,5 +1,4 @@ """Facebook OAuth Views""" - from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect diff --git a/passbook/sources/oauth/types/github.py b/passbook/sources/oauth/types/github.py index efaa232b6..174fe046f 100644 --- a/passbook/sources/oauth/types/github.py +++ b/passbook/sources/oauth/types/github.py @@ -1,5 +1,4 @@ """GitHub OAuth Views""" - from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback diff --git a/passbook/sources/oauth/types/reddit.py b/passbook/sources/oauth/types/reddit.py index 8c9271883..8384d6435 100644 --- a/passbook/sources/oauth/types/reddit.py +++ b/passbook/sources/oauth/types/reddit.py @@ -1,17 +1,11 @@ """Reddit OAuth Views""" -import json - from requests.auth import HTTPBasicAuth -from requests.exceptions import RequestException -from structlog import get_logger from passbook.sources.oauth.clients import OAuth2Client from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect -LOGGER = get_logger() - @MANAGER.source(kind=RequestKind.redirect, name="reddit") class RedditOAuthRedirect(OAuthRedirect): @@ -34,26 +28,6 @@ class RedditOAuth2Client(OAuth2Client): request, callback, auth=auth ) - def get_profile_info(self, raw_token): - "Fetch user profile information." - try: - token = json.loads(raw_token) - headers = { - "Authorization": "%s %s" % (token["token_type"], token["access_token"]) - } - response = self.request( - "get", - self.source.profile_url, - token=token["access_token"], - headers=headers, - ) - response.raise_for_status() - except RequestException as exc: - LOGGER.warning("Unable to fetch user profile", exc=exc) - return None - else: - return response.json() or response.text - @MANAGER.source(kind=RequestKind.callback, name="reddit") class RedditOAuth2Callback(OAuthCallback): diff --git a/passbook/sources/oauth/types/twitter.py b/passbook/sources/oauth/types/twitter.py index a5aaa2801..4a4196f3c 100644 --- a/passbook/sources/oauth/types/twitter.py +++ b/passbook/sources/oauth/types/twitter.py @@ -1,39 +1,13 @@ """Twitter OAuth Views""" - -from requests.exceptions import RequestException -from structlog import get_logger - -from passbook.sources.oauth.clients import OAuthClient from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.views.core import OAuthCallback -LOGGER = get_logger() - - -class TwitterOAuthClient(OAuthClient): - """Twitter OAuth2 Client""" - - def get_profile_info(self, raw_token): - "Fetch user profile information." - try: - response = self.request( - "get", self.source.profile_url + "?include_email=true", token=raw_token - ) - response.raise_for_status() - except RequestException as exc: - LOGGER.warning("Unable to fetch user profile", exc=exc) - return None - else: - return response.json() or response.text - @MANAGER.source(kind=RequestKind.callback, name="Twitter") class TwitterOAuthCallback(OAuthCallback): """Twitter OAuth2 Callback""" - client_class = TwitterOAuthClient - def get_or_create_user(self, source, access, info): user_data = { "username": info.get("screen_name"),