Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

backend: Make display_icon be lazily resolved.

Alex Vandiver62c2acbde85d
diff --git a/zproject/backends.py b/zproject/backends.py
index 33ce3f708de..c8d2c02f6ec 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -1574,12 +1574,16 @@ class ExternalAuthMethod(ABC):
auth_backend_name = "undeclared"
name = "undeclared"
- display_icon: str | None = None
# Used to determine how to order buttons on login form, backend with
# higher sort order are displayed first.
sort_order = 0
+ @classmethod
+ @abstractmethod
+ def display_icon(cls) -> str | None:
+ pass
+
@classmethod
@abstractmethod
def dict_representation(cls, realm: Realm | None = None) -> list[ExternalAuthMethodDictT]:
@@ -1763,12 +1767,16 @@ class ZulipRemoteUserBackend(ZulipAuthMixin, RemoteUserBackend, ExternalAuthMeth
auth_backend_name = "RemoteUser"
name = "remoteuser"
- display_icon = None
# If configured, this backend should have its button near the top of the list.
sort_order = 9000
create_unknown_user = False
+ @classmethod
+ @override
+ def display_icon(cls) -> str | None:
+ return None
+
@override
def authenticate( # type: ignore[override] # authenticate has an incompatible signature with ModelBackend and BaseBackend
self,
@@ -1791,7 +1799,7 @@ class ZulipRemoteUserBackend(ZulipAuthMixin, RemoteUserBackend, ExternalAuthMeth
dict(
name=cls.name,
display_name="SSO",
- display_icon=cls.display_icon,
+ display_icon=cls.display_icon(),
# The user goes to the same URL for both login and signup:
login_url=reverse("start-login-sso"),
signup_url=reverse("start-login-sso"),
@@ -2662,7 +2670,7 @@ class SocialAuthMixin(ZulipAuthMixin, ExternalAuthMethod, BaseAuth):
dict(
name=cls.name,
display_name=cls.auth_backend_name,
- display_icon=cls.display_icon,
+ display_icon=cls.display_icon(),
login_url=reverse("login-social", args=(cls.name,)),
signup_url=reverse("signup-social", args=(cls.name,)),
)
@@ -2674,7 +2682,11 @@ class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
name = "github"
auth_backend_name = "GitHub"
sort_order = 100
- display_icon = staticfiles_storage.url("images/authentication_backends/github-icon.png")
+
+ @classmethod
+ @override
+ def display_icon(cls) -> str:
+ return staticfiles_storage.url("images/authentication_backends/github-icon.png")
def get_all_associated_email_objects(self, *args: Any, **kwargs: Any) -> list[dict[str, Any]]:
access_token = kwargs["response"]["access_token"]
@@ -2779,7 +2791,6 @@ class AzureADAuthBackend(SocialAuthMixin, AzureADOAuth2):
sort_order = 50
name = "azuread-oauth2"
auth_backend_name = "AzureAD"
- display_icon = staticfiles_storage.url("images/authentication_backends/azuread-icon.png")
available_for_cloud_plans = [
Realm.PLAN_TYPE_STANDARD,
@@ -2787,13 +2798,17 @@ class AzureADAuthBackend(SocialAuthMixin, AzureADOAuth2):
Realm.PLAN_TYPE_PLUS,
]
+ @classmethod
+ @override
+ def display_icon(cls) -> str:
+ return staticfiles_storage.url("images/authentication_backends/azuread-icon.png")
+
@external_auth_method
class GitLabAuthBackend(SocialAuthMixin, GitLabOAuth2):
sort_order = 75
name = "gitlab"
auth_backend_name = "GitLab"
- display_icon = staticfiles_storage.url("images/authentication_backends/gitlab-icon.png")
# Note: GitLab as of early 2020 supports having multiple email
# addresses connected with a GitLab account, and we could access
@@ -2803,13 +2818,22 @@ class GitLabAuthBackend(SocialAuthMixin, GitLabOAuth2):
# we just use the primary email address, which is always verified.
# (No code is required to do so, as that's the default behavior).
+ @classmethod
+ @override
+ def display_icon(cls) -> str:
+ return staticfiles_storage.url("images/authentication_backends/gitlab-icon.png")
+
@external_auth_method
class GoogleAuthBackend(SocialAuthMixin, GoogleOAuth2):
sort_order = 150
auth_backend_name = "Google"
name = "google"
- display_icon = staticfiles_storage.url("images/authentication_backends/googl_e-icon.png")
+
+ @classmethod
+ @override
+ def display_icon(cls) -> str:
+ return staticfiles_storage.url("images/authentication_backends/googl_e-icon.png")
def get_verified_emails(self, *args: Any, **kwargs: Any) -> list[str]:
verified_emails: list[str] = []
@@ -2840,7 +2864,6 @@ class AppleAuthBackend(SocialAuthMixin, AppleIdAuth):
sort_order = 10
name = "apple"
auth_backend_name = "Apple"
- display_icon = staticfiles_storage.url("images/authentication_backends/apple-icon.png")
# Apple only sends `name` in its response the first time a user
# tries to sign up, so we won't have it in consecutive attempts.
@@ -2851,6 +2874,11 @@ class AppleAuthBackend(SocialAuthMixin, AppleIdAuth):
SCOPE_SEPARATOR = "%20" # https://github.com/python-social-auth/social-core/issues/470
+ @classmethod
+ @override
+ def display_icon(cls) -> str:
+ return staticfiles_storage.url("images/authentication_backends/apple-icon.png")
+
@classmethod
def check_config(cls) -> bool:
obligatory_apple_settings_list = [
@@ -3192,8 +3220,6 @@ class SAMLAuthBackend(SocialAuthMixin, SAMLAuth):
# to have it as their main authentication method, so it seems appropriate to have
# SAML buttons at the top.
sort_order = 9999
- # There's no common default logo for SAML authentication.
- display_icon = None
# The full_name provided by the IdP is very likely the standard
# employee directory name for the user, and thus what they and
@@ -3203,6 +3229,12 @@ class SAMLAuthBackend(SocialAuthMixin, SAMLAuth):
available_for_cloud_plans = [Realm.PLAN_TYPE_PLUS]
+ @classmethod
+ @override
+ def display_icon(cls) -> str | None:
+ # There's no common default logo for SAML authentication.
+ return None
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
if settings.SAML_REQUIRE_LIMIT_TO_SUBDOMAINS:
idps_without_limit_to_subdomains = [
@@ -3607,7 +3639,7 @@ class SAMLAuthBackend(SocialAuthMixin, SAMLAuth):
saml_dict: ExternalAuthMethodDictT = dict(
name=f"saml:{idp_name}",
display_name=idp_dict.get("display_name", cls.auth_backend_name),
- display_icon=idp_dict.get("display_icon", cls.display_icon),
+ display_icon=idp_dict.get("display_icon", cls.display_icon()),
login_url=reverse("login-social", args=("saml", idp_name)),
signup_url=reverse("signup-social", args=("saml", idp_name)),
)
@@ -3678,6 +3710,12 @@ class GenericOpenIdConnectBackend(SocialAuthMixin, OpenIdConnectAuth):
super().__init__(*args, **kwargs)
+ @classmethod
+ @override
+ def display_icon(cls) -> str | None:
+ # There's no common default logo for OIDC authentication
+ return None
+
@override
def get_key_and_secret(self) -> tuple[str, str]:
client_id = self.settings_dict.get("client_id", "")
@@ -3870,7 +3908,7 @@ class GenericOpenIdConnectBackend(SocialAuthMixin, OpenIdConnectAuth):
auth_dict: ExternalAuthMethodDictT = dict(
name=f"oidc:{idp_name}",
display_name=idp_dict.get("display_name", cls.auth_backend_name),
- display_icon=idp_dict.get("display_icon", cls.display_icon),
+ display_icon=idp_dict.get("display_icon", cls.display_icon()),
login_url=reverse("login-social", args=("oidc", idp_name)),
signup_url=reverse("signup-social", args=("oidc", idp_name)),
)