| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.socialaccount.adapter import get_adapter |
| 6 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 7 |
OAuth2Adapter, |
| 8 |
OAuth2CallbackView, |
| 9 |
OAuth2LoginView, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class CoinbaseOAuth2Adapter(OAuth2Adapter): |
| 14 |
provider_id = "coinbase" |
| 15 |
|
| 16 |
@property |
| 17 |
def authorize_url(self): |
| 18 |
return "https://www.coinbase.com/oauth/authorize" |
| 19 |
|
| 20 |
@property |
| 21 |
def access_token_url(self): |
| 22 |
return "https://www.coinbase.com/oauth/token" |
| 23 |
|
| 24 |
@property |
| 25 |
def profile_url(self): |
| 26 |
return "https://api.coinbase.com/v2/user" |
| 27 |
|
| 28 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 29 |
with get_adapter().get_requests_session() as sess: |
| 30 |
response = sess.get(self.profile_url, params={"access_token": token}) |
| 31 |
extra_data = response.json() |
| 32 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 33 |
|
| 34 |
|
| 35 |
oauth2_login = OAuth2LoginView.adapter_view(CoinbaseOAuth2Adapter) |
| 36 |
oauth2_callback = OAuth2CallbackView.adapter_view(CoinbaseOAuth2Adapter) |