sources/oauth: remove redundant OAuth2Clients

This commit is contained in:
Jens Langhammer 2020-02-18 21:49:40 +01:00
parent 4a05bc6e02
commit a2725d5b82
6 changed files with 0 additions and 114 deletions

View File

@ -1,41 +1,15 @@
"""AzureAD OAuth2 Views""" """AzureAD OAuth2 Views"""
import json
import uuid 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.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback 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") @MANAGER.source(kind=RequestKind.callback, name="Azure AD")
class AzureADOAuthCallback(OAuthCallback): class AzureADOAuthCallback(OAuthCallback):
"""AzureAD OAuth2 Callback""" """AzureAD OAuth2 Callback"""
client_class = AzureADOAuth2Client
def get_user_id(self, source, info): def get_user_id(self, source, info):
return uuid.UUID(info.get("objectId")).int return uuid.UUID(info.get("objectId")).int

View File

@ -1,16 +1,8 @@
"""Discord OAuth Views""" """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.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect
LOGGER = get_logger()
@MANAGER.source(kind=RequestKind.redirect, name="Discord") @MANAGER.source(kind=RequestKind.redirect, name="Discord")
class DiscordOAuthRedirect(OAuthRedirect): 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") @MANAGER.source(kind=RequestKind.callback, name="Discord")
class DiscordOAuth2Callback(OAuthCallback): class DiscordOAuth2Callback(OAuthCallback):
"""Discord OAuth2 Callback""" """Discord OAuth2 Callback"""
client_class = DiscordOAuth2Client
def get_or_create_user(self, source, access, info): def get_or_create_user(self, source, access, info):
user_data = { user_data = {
"username": info.get("username"), "username": info.get("username"),

View File

@ -1,5 +1,4 @@
"""Facebook OAuth Views""" """Facebook OAuth Views"""
from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect

View File

@ -1,5 +1,4 @@
"""GitHub OAuth Views""" """GitHub OAuth Views"""
from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback from passbook.sources.oauth.views.core import OAuthCallback

View File

@ -1,17 +1,11 @@
"""Reddit OAuth Views""" """Reddit OAuth Views"""
import json
from requests.auth import HTTPBasicAuth 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.clients import OAuth2Client
from passbook.sources.oauth.types.manager import MANAGER, RequestKind from passbook.sources.oauth.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect from passbook.sources.oauth.views.core import OAuthCallback, OAuthRedirect
LOGGER = get_logger()
@MANAGER.source(kind=RequestKind.redirect, name="reddit") @MANAGER.source(kind=RequestKind.redirect, name="reddit")
class RedditOAuthRedirect(OAuthRedirect): class RedditOAuthRedirect(OAuthRedirect):
@ -34,26 +28,6 @@ class RedditOAuth2Client(OAuth2Client):
request, callback, auth=auth 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") @MANAGER.source(kind=RequestKind.callback, name="reddit")
class RedditOAuth2Callback(OAuthCallback): class RedditOAuth2Callback(OAuthCallback):

View File

@ -1,39 +1,13 @@
"""Twitter OAuth Views""" """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.types.manager import MANAGER, RequestKind
from passbook.sources.oauth.utils import user_get_or_create from passbook.sources.oauth.utils import user_get_or_create
from passbook.sources.oauth.views.core import OAuthCallback 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") @MANAGER.source(kind=RequestKind.callback, name="Twitter")
class TwitterOAuthCallback(OAuthCallback): class TwitterOAuthCallback(OAuthCallback):
"""Twitter OAuth2 Callback""" """Twitter OAuth2 Callback"""
client_class = TwitterOAuthClient
def get_or_create_user(self, source, access, info): def get_or_create_user(self, source, access, info):
user_data = { user_data = {
"username": info.get("screen_name"), "username": info.get("screen_name"),