refactor: De-duplicate login button code in portico templates.

This commit is contained in:
Harshit Bansal 2019-03-05 17:30:10 +00:00 committed by Tim Abbott
parent 0e401c4f18
commit 3610aaece3
4 changed files with 26 additions and 16 deletions

View File

@ -635,7 +635,7 @@ button.login-google-button {
transform: translateX(15px) translateY(13px); transform: translateX(15px) translateY(13px);
} }
.azure-wrapper::before { .azuread-wrapper::before {
content: "\f17a"; content: "\f17a";
position: absolute; position: absolute;

View File

@ -143,27 +143,16 @@
</div> </div>
{% endif %} {% endif %}
{% if github_auth_enabled %} {% for backend in social_backends %}
<div class="login-social"> <div class="login-social">
<form id='social_login_form' class="form-inline github-wrapper" action="{{ url('login-social', args=('github',)) }}" method="get"> <form id='social_login_form' class="form-inline {{ backend.name }}-wrapper" action="{{ backend.login_url }}" method="get">
<input type="hidden" name="next" value="{{ next }}"> <input type="hidden" name="next" value="{{ next }}">
<button class="login-social-button"> <button class="login-social-button">
{{ _('Log in with %(identity_provider)s', identity_provider="GitHub") }} {{ _('Log in with %(identity_provider)s', identity_provider=backend.display_name) }}
</button> </button>
</form> </form>
</div> </div>
{% endif %} {% endfor %}
{% if azuread_auth_enabled %}
<div class="login-social">
<form id='azure_login_form' class="form-inline azure-wrapper" action="{{ url('login-social', args=('azuread-oauth2',)) }}" method="get">
<input type="hidden" name="next" value="{{ next }}">
<button class="login-social-button">
{{ _('Log in with %(identity_provider)s', identity_provider="Azure AD") }}
</button>
</form>
</div>
{% endif %}
<div class="actions"> <div class="actions">
{% if email_auth_enabled %} {% if email_auth_enabled %}

View File

@ -2,6 +2,7 @@
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from django.http import HttpRequest from django.http import HttpRequest
from django.conf import settings from django.conf import settings
from django.urls import reverse
from zerver.models import UserProfile, get_realm, Realm from zerver.models import UserProfile, get_realm, Realm
from zproject.backends import ( from zproject.backends import (
@ -10,6 +11,7 @@ from zproject.backends import (
require_email_format_usernames, require_email_format_usernames,
auth_enabled_helper, auth_enabled_helper,
AUTH_BACKEND_NAME_MAP, AUTH_BACKEND_NAME_MAP,
SOCIAL_AUTH_BACKENDS,
) )
from zerver.lib.bugdown import convert as bugdown_convert from zerver.lib.bugdown import convert as bugdown_convert
from zerver.lib.send_email import FromAddress from zerver.lib.send_email import FromAddress
@ -147,4 +149,17 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
name_lower = auth_backend_name.lower() name_lower = auth_backend_name.lower()
key = "%s_auth_enabled" % (name_lower,) key = "%s_auth_enabled" % (name_lower,)
context[key] = auth_enabled_helper([auth_backend_name], realm) context[key] = auth_enabled_helper([auth_backend_name], realm)
social_backends = []
for backend in SOCIAL_AUTH_BACKENDS:
if not auth_enabled_helper([backend.auth_backend_name], realm):
continue
social_backends.append({
'name': backend.name,
'display_name': backend.auth_backend_name,
'login_url': reverse('login-social', args=(backend.name,)),
'sort_order': backend.sort_order,
})
context['social_backends'] = sorted(social_backends, key=lambda x: x['sort_order'])
return context return context

View File

@ -650,6 +650,8 @@ def social_auth_finish(backend: Any,
class SocialAuthMixin(ZulipAuthMixin): class SocialAuthMixin(ZulipAuthMixin):
auth_backend_name = "undeclared" auth_backend_name = "undeclared"
# Used to determine how to order buttons on login form
sort_order = 0
def auth_complete(self, *args: Any, **kwargs: Any) -> Optional[HttpResponse]: def auth_complete(self, *args: Any, **kwargs: Any) -> Optional[HttpResponse]:
"""This is a small wrapper around the core `auth_complete` method of """This is a small wrapper around the core `auth_complete` method of
@ -676,6 +678,7 @@ class SocialAuthMixin(ZulipAuthMixin):
class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2): class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
auth_backend_name = "GitHub" auth_backend_name = "GitHub"
sort_order = 50
def get_verified_emails(self, *args: Any, **kwargs: Any) -> List[str]: def get_verified_emails(self, *args: Any, **kwargs: Any) -> List[str]:
access_token = kwargs["response"]["access_token"] access_token = kwargs["response"]["access_token"]
@ -727,6 +730,7 @@ class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
raise AssertionError("Invalid configuration") raise AssertionError("Invalid configuration")
class AzureADAuthBackend(SocialAuthMixin, AzureADOAuth2): class AzureADAuthBackend(SocialAuthMixin, AzureADOAuth2):
sort_order = 100
auth_backend_name = "AzureAD" auth_backend_name = "AzureAD"
AUTH_BACKEND_NAME_MAP = { AUTH_BACKEND_NAME_MAP = {
@ -737,9 +741,11 @@ AUTH_BACKEND_NAME_MAP = {
'RemoteUser': ZulipRemoteUserBackend, 'RemoteUser': ZulipRemoteUserBackend,
} # type: Dict[str, Any] } # type: Dict[str, Any]
OAUTH_BACKEND_NAMES = ["Google"] # type: List[str] OAUTH_BACKEND_NAMES = ["Google"] # type: List[str]
SOCIAL_AUTH_BACKENDS = [] # type: List[BaseOAuth2]
# Authomatically add all of our social auth backends to relevant data structures. # Authomatically add all of our social auth backends to relevant data structures.
for social_auth_subclass in SocialAuthMixin.__subclasses__(): for social_auth_subclass in SocialAuthMixin.__subclasses__():
AUTH_BACKEND_NAME_MAP[social_auth_subclass.auth_backend_name] = social_auth_subclass AUTH_BACKEND_NAME_MAP[social_auth_subclass.auth_backend_name] = social_auth_subclass
if issubclass(social_auth_subclass, BaseOAuth2): if issubclass(social_auth_subclass, BaseOAuth2):
OAUTH_BACKEND_NAMES.append(social_auth_subclass.auth_backend_name) OAUTH_BACKEND_NAMES.append(social_auth_subclass.auth_backend_name)
SOCIAL_AUTH_BACKENDS.append(social_auth_subclass)