| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
from django.utils.http import urlencode |
| 5 |
|
| 6 |
from allauth.socialaccount.adapter import get_adapter |
| 7 |
from allauth.socialaccount.providers.oauth.views import ( |
| 8 |
OAuthAdapter, |
| 9 |
OAuthCallbackView, |
| 10 |
OAuthLoginView, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class TrelloOAuthAdapter(OAuthAdapter): |
| 15 |
provider_id = "trello" |
| 16 |
request_token_url = "https://trello.com/1/OAuthGetRequestToken" # nosec |
| 17 |
authorize_url = "https://trello.com/1/OAuthAuthorizeToken" |
| 18 |
access_token_url = "https://trello.com/1/OAuthGetAccessToken" # nosec |
| 19 |
|
| 20 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 21 |
response = kwargs["response"] |
| 22 |
# we need to get the member id and the other information |
| 23 |
info_url = "{base}?{query}".format( |
| 24 |
base="https://api.trello.com/1/members/me", |
| 25 |
query=urlencode({"key": app.key, "token": response.get("oauth_token")}), |
| 26 |
) |
| 27 |
with get_adapter().get_requests_session() as sess: |
| 28 |
resp = sess.get(info_url) |
| 29 |
resp.raise_for_status() |
| 30 |
extra_data = resp.json() |
| 31 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 32 |
|
| 33 |
|
| 34 |
oauth_login = OAuthLoginView.adapter_view(TrelloOAuthAdapter) |
| 35 |
oauth_callback = OAuthCallbackView.adapter_view(TrelloOAuthAdapter) |