| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
from urllib.parse import urlencode |
| 5 |
|
| 6 |
from django.http import HttpResponseRedirect |
| 7 |
from django.utils.translation import gettext as _ |
| 8 |
|
| 9 |
from allauth.socialaccount.adapter import get_adapter |
| 10 |
from allauth.socialaccount.providers.oauth.client import ( |
| 11 |
OAuthClient, |
| 12 |
OAuthError, |
| 13 |
get_token_prefix, |
| 14 |
) |
| 15 |
from allauth.utils import build_absolute_uri |
| 16 |
|
| 17 |
|
| 18 |
class PocketOAuthClient(OAuthClient): |
| 19 |
def _get_request_token(self): |
| 20 |
""" |
| 21 |
Obtain a temporary request token to authorize an access token and to |
| 22 |
sign the request to obtain the access token |
| 23 |
""" |
| 24 |
if self.request_token is None: |
| 25 |
redirect_url = build_absolute_uri(self.request, self.callback_url) |
| 26 |
headers = { |
| 27 |
"X-Accept": "application/json", |
| 28 |
} |
| 29 |
data = { |
| 30 |
"consumer_key": self.consumer_key, |
| 31 |
"redirect_uri": redirect_url, |
| 32 |
} |
| 33 |
with get_adapter().get_requests_session() as sess: |
| 34 |
response = sess.post( |
| 35 |
url=self.request_token_url, |
| 36 |
json=data, |
| 37 |
headers=headers, |
| 38 |
) |
| 39 |
if response.status_code != HTTPStatus.OK: |
| 40 |
raise OAuthError( |
| 41 |
_('Invalid response while obtaining request token from "%s".') |
| 42 |
% get_token_prefix(self.request_token_url) |
| 43 |
) |
| 44 |
self.request_token = response.json()["code"] |
| 45 |
self.request.session[ |
| 46 |
f"oauth_{get_token_prefix(self.request_token_url)}_request_token" |
| 47 |
] = self.request_token |
| 48 |
return self.request_token |
| 49 |
|
| 50 |
def get_redirect(self, authorization_url, extra_params): |
| 51 |
""" |
| 52 |
Returns a ``HttpResponseRedirect`` object to redirect the user |
| 53 |
to the Pocket authorization URL. |
| 54 |
""" |
| 55 |
request_token = self._get_request_token() |
| 56 |
params = { |
| 57 |
"request_token": request_token, |
| 58 |
"redirect_uri": self.request.build_absolute_uri(self.callback_url), |
| 59 |
} |
| 60 |
params.update(extra_params) |
| 61 |
url = f"{authorization_url}?{urlencode(params)}" |
| 62 |
return HttpResponseRedirect(url) |
| 63 |
|
| 64 |
def get_access_token(self): |
| 65 |
""" |
| 66 |
Obtain the access token to access private resources at the API |
| 67 |
endpoint. |
| 68 |
""" |
| 69 |
if self.access_token is None: |
| 70 |
request_token = self._get_rt_from_session() |
| 71 |
url = self.access_token_url |
| 72 |
headers = { |
| 73 |
"X-Accept": "application/json", |
| 74 |
} |
| 75 |
data = { |
| 76 |
"consumer_key": self.consumer_key, |
| 77 |
"code": request_token, |
| 78 |
} |
| 79 |
with get_adapter().get_requests_session() as sess: |
| 80 |
response = sess.post(url=url, headers=headers, json=data) |
| 81 |
if response.status_code != HTTPStatus.OK: |
| 82 |
raise OAuthError( |
| 83 |
_('Invalid response while obtaining access token from "%s".') |
| 84 |
% get_token_prefix(self.request_token_url) |
| 85 |
) |
| 86 |
r = response.json() |
| 87 |
self.access_token = { |
| 88 |
"oauth_token": request_token, |
| 89 |
"oauth_token_secret": r["access_token"], |
| 90 |
"username": r["username"], |
| 91 |
} |
| 92 |
|
| 93 |
self.request.session[ |
| 94 |
f"oauth_{get_token_prefix(self.request_token_url)}_access_token" |
| 95 |
] = self.access_token |
| 96 |
return self.access_token |