sources/oauth: remove redundant OAuth2Clients
This commit is contained in:
parent
4a05bc6e02
commit
a2725d5b82
|
@ -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
|
||||
|
||||
|
|
|
@ -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"),
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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"),
|
||||
|
|
Reference in New Issue