2020-09-25 23:26:06 +00:00
|
|
|
"""OAuth 1 Clients"""
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from urllib.parse import parse_qsl
|
2020-09-25 21:58:58 +00:00
|
|
|
|
|
|
|
from requests.exceptions import RequestException
|
2020-09-25 22:34:57 +00:00
|
|
|
from requests.models import Response
|
2020-09-25 21:58:58 +00:00
|
|
|
from requests_oauthlib import OAuth1
|
|
|
|
from structlog import get_logger
|
|
|
|
|
|
|
|
from passbook.sources.oauth.clients.base import BaseOAuthClient
|
2020-09-25 22:34:57 +00:00
|
|
|
from passbook.sources.oauth.exceptions import OAuthSourceException
|
2020-09-25 21:58:58 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
class OAuthClient(BaseOAuthClient):
|
|
|
|
"""OAuth1 Client"""
|
|
|
|
|
|
|
|
_default_headers = {
|
|
|
|
"Accept": "application/json",
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
def get_access_token(self, **request_kwargs) -> Optional[Dict[str, Any]]:
|
2020-09-25 21:58:58 +00:00
|
|
|
"Fetch access token from callback request."
|
2020-09-25 22:34:57 +00:00
|
|
|
raw_token = self.request.session.get(self.session_key, None)
|
|
|
|
verifier = self.request.GET.get("oauth_verifier", None)
|
2020-09-26 15:43:58 +00:00
|
|
|
callback = self.request.build_absolute_uri(self.callback)
|
2020-09-25 21:58:58 +00:00
|
|
|
if raw_token is not None and verifier is not None:
|
2020-09-25 23:26:06 +00:00
|
|
|
token = self.parse_raw_token(raw_token)
|
2020-09-25 21:58:58 +00:00
|
|
|
try:
|
2020-09-25 23:26:06 +00:00
|
|
|
response = self.do_request(
|
2020-09-25 21:58:58 +00:00
|
|
|
"post",
|
|
|
|
self.source.access_token_url,
|
2020-09-25 23:26:06 +00:00
|
|
|
token=token,
|
2020-09-25 21:58:58 +00:00
|
|
|
headers=self._default_headers,
|
2020-09-26 15:43:58 +00:00
|
|
|
oauth_verifier=verifier,
|
2020-09-26 16:17:53 +00:00
|
|
|
oauth_callback=callback,
|
2020-09-25 21:58:58 +00:00
|
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
except RequestException as exc:
|
|
|
|
LOGGER.warning("Unable to fetch access token", exc=exc)
|
|
|
|
return None
|
|
|
|
else:
|
2020-09-25 23:26:06 +00:00
|
|
|
return self.parse_raw_token(response.text)
|
2020-09-25 21:58:58 +00:00
|
|
|
return None
|
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
def get_request_token(self) -> str:
|
2020-09-25 21:58:58 +00:00
|
|
|
"Fetch the OAuth request token. Only required for OAuth 1.0."
|
2020-09-25 22:34:57 +00:00
|
|
|
callback = self.request.build_absolute_uri(self.callback)
|
2020-09-25 21:58:58 +00:00
|
|
|
try:
|
2020-09-25 23:26:06 +00:00
|
|
|
response = self.do_request(
|
2020-09-25 21:58:58 +00:00
|
|
|
"post",
|
|
|
|
self.source.request_token_url,
|
|
|
|
headers=self._default_headers,
|
2020-09-26 15:43:58 +00:00
|
|
|
oauth_callback=callback,
|
2020-09-25 21:58:58 +00:00
|
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
except RequestException as exc:
|
2020-09-25 22:34:57 +00:00
|
|
|
raise OAuthSourceException from exc
|
2020-09-25 21:58:58 +00:00
|
|
|
else:
|
|
|
|
return response.text
|
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
def get_redirect_args(self) -> Dict[str, Any]:
|
2020-09-25 21:58:58 +00:00
|
|
|
"Get request parameters for redirect url."
|
2020-09-25 22:34:57 +00:00
|
|
|
callback = self.request.build_absolute_uri(self.callback)
|
|
|
|
raw_token = self.get_request_token()
|
2020-09-25 23:26:06 +00:00
|
|
|
token = self.parse_raw_token(raw_token)
|
2020-09-25 22:34:57 +00:00
|
|
|
self.request.session[self.session_key] = raw_token
|
2020-09-25 21:58:58 +00:00
|
|
|
return {
|
2020-09-25 23:26:06 +00:00
|
|
|
"oauth_token": token["oauth_token"],
|
2020-09-25 21:58:58 +00:00
|
|
|
"oauth_callback": callback,
|
|
|
|
}
|
|
|
|
|
2020-09-25 23:26:06 +00:00
|
|
|
def parse_raw_token(self, raw_token: str) -> Dict[str, Any]:
|
2020-09-25 21:58:58 +00:00
|
|
|
"Parse token and secret from raw token response."
|
2020-09-25 23:26:06 +00:00
|
|
|
return dict(parse_qsl(raw_token))
|
2020-09-25 21:58:58 +00:00
|
|
|
|
2020-09-25 22:34:57 +00:00
|
|
|
def do_request(self, method: str, url: str, **kwargs) -> Response:
|
2020-09-25 21:58:58 +00:00
|
|
|
"Build remote url request. Constructs necessary auth."
|
2020-09-25 23:26:06 +00:00
|
|
|
resource_owner_key = None
|
|
|
|
resource_owner_secret = None
|
|
|
|
if "token" in kwargs:
|
|
|
|
user_token: Dict[str, Any] = kwargs.pop("token")
|
|
|
|
resource_owner_key = user_token["oauth_token"]
|
|
|
|
resource_owner_secret = user_token["oauth_token_secret"]
|
|
|
|
|
2020-09-25 21:58:58 +00:00
|
|
|
callback = kwargs.pop("oauth_callback", None)
|
2020-09-26 15:43:58 +00:00
|
|
|
verifier = kwargs.pop("oauth_verifier", None)
|
2020-09-25 21:58:58 +00:00
|
|
|
oauth = OAuth1(
|
2020-09-25 23:26:06 +00:00
|
|
|
resource_owner_key=resource_owner_key,
|
|
|
|
resource_owner_secret=resource_owner_secret,
|
2020-09-25 21:58:58 +00:00
|
|
|
client_key=self.source.consumer_key,
|
|
|
|
client_secret=self.source.consumer_secret,
|
|
|
|
verifier=verifier,
|
|
|
|
callback_uri=callback,
|
|
|
|
)
|
|
|
|
kwargs["auth"] = oauth
|
2020-09-25 22:34:57 +00:00
|
|
|
return super().do_request(method, url, **kwargs)
|
2020-09-25 21:58:58 +00:00
|
|
|
|
|
|
|
@property
|
2020-09-25 22:34:57 +00:00
|
|
|
def session_key(self) -> str:
|
|
|
|
return f"oauth-client-{self.source.name}-request-token"
|