| 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.client import OAuth2Error |
| 7 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 8 |
OAuth2Adapter, |
| 9 |
OAuth2CallbackView, |
| 10 |
OAuth2LoginView, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class SlackOAuth2Adapter(OAuth2Adapter): |
| 15 |
provider_id = "slack" |
| 16 |
|
| 17 |
access_token_url = "https://slack.com/api/openid.connect.token" # nosec |
| 18 |
authorize_url = "https://slack.com//openid/connect/authorize" |
| 19 |
identity_url = "https://slack.com/api/openid.connect.userInfo" |
| 20 |
|
| 21 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 22 |
extra_data = self.get_data(token.token) |
| 23 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 24 |
|
| 25 |
def get_data(self, token): |
| 26 |
# Verify the user first |
| 27 |
headers = {"Authorization": f"Bearer {token}"} |
| 28 |
with get_adapter().get_requests_session() as sess: |
| 29 |
resp = sess.get(self.identity_url, headers=headers) |
| 30 |
data = resp.json() |
| 31 |
if not data.get("ok"): |
| 32 |
raise OAuth2Error() |
| 33 |
return data |
| 34 |
|
| 35 |
|
| 36 |
oauth2_login = OAuth2LoginView.adapter_view(SlackOAuth2Adapter) |
| 37 |
oauth2_callback = OAuth2CallbackView.adapter_view(SlackOAuth2Adapter) |