| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.account.models import EmailAddress |
| 4 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 5 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 6 |
from allauth.socialaccount.providers.slack.views import SlackOAuth2Adapter |
| 7 |
|
| 8 |
|
| 9 |
class SlackAccount(ProviderAccount): |
| 10 |
def get_avatar_url(self): |
| 11 |
return self.account.extra_data.get("user", {}).get("image_192") |
| 12 |
|
| 13 |
|
| 14 |
class SlackProvider(OAuth2Provider): |
| 15 |
id = "slack" |
| 16 |
name = "Slack" |
| 17 |
account_class = SlackAccount |
| 18 |
oauth2_adapter_class = SlackOAuth2Adapter |
| 19 |
|
| 20 |
def extract_uid(self, data): |
| 21 |
team_id = data.get("https://slack.com/team_id") |
| 22 |
user_id = data.get("https://slack.com/user_id") |
| 23 |
if not (team_id and user_id): |
| 24 |
team_id = data.get("team").get("id") |
| 25 |
user_id = data.get("user").get("id") |
| 26 |
return f"{team_id!s}_{user_id!s}" |
| 27 |
|
| 28 |
def extract_common_fields(self, data): |
| 29 |
user = data.get("user", {}) |
| 30 |
return {"name": user.get("name"), "email": user.get("email", None)} |
| 31 |
|
| 32 |
def extract_email_addresses(self, data): |
| 33 |
ret = [] |
| 34 |
email = data.get("email") |
| 35 |
if email: |
| 36 |
verified = data.get("email_verified") |
| 37 |
ret.append(EmailAddress(email=email, verified=verified, primary=True)) |
| 38 |
return ret |
| 39 |
|
| 40 |
def get_default_scope(self): |
| 41 |
return ["openid", "profile", "email"] |
| 42 |
|
| 43 |
|
| 44 |
provider_classes = [SlackProvider] |