From 15d82232d0dac85994cf5d45f76dbd29f7f9af7a Mon Sep 17 00:00:00 2001 From: odkhang Date: Wed, 23 Oct 2024 18:02:09 +0700 Subject: [PATCH] fix UT --- src/pretalx/settings.py | 13 +- src/pretalx/sso_provider/__init__.py | 0 src/pretalx/sso_provider/apps.py | 12 -- src/pretalx/sso_provider/forms.py | 7 - src/pretalx/sso_provider/providers.py | 98 ----------- .../socialaccount/authentication_error.html | 17 -- .../templates/socialaccount/base.html | 163 ------------------ .../templates/socialaccount/signup.html | 39 ----- src/pretalx/sso_provider/urls.py | 5 - src/pretalx/sso_provider/views.py | 97 ----------- src/pretalx/urls.py | 1 - 11 files changed, 1 insertion(+), 451 deletions(-) delete mode 100644 src/pretalx/sso_provider/__init__.py delete mode 100644 src/pretalx/sso_provider/apps.py delete mode 100644 src/pretalx/sso_provider/forms.py delete mode 100644 src/pretalx/sso_provider/providers.py delete mode 100644 src/pretalx/sso_provider/templates/socialaccount/authentication_error.html delete mode 100644 src/pretalx/sso_provider/templates/socialaccount/base.html delete mode 100644 src/pretalx/sso_provider/templates/socialaccount/signup.html delete mode 100644 src/pretalx/sso_provider/urls.py delete mode 100644 src/pretalx/sso_provider/views.py diff --git a/src/pretalx/settings.py b/src/pretalx/settings.py index 76b2716b3..22bb381a4 100644 --- a/src/pretalx/settings.py +++ b/src/pretalx/settings.py @@ -93,7 +93,6 @@ "pretalx.agenda", "pretalx.cfp", "pretalx.orga", - "pretalx.sso_provider", "pretalx.eventyay_common", ] FALLBACK_APPS = [ @@ -555,7 +554,7 @@ def merge_csp(*options, config=None): "DIRS": [ DATA_DIR / "templates", BASE_DIR / "templates", - BASE_DIR / "pretalx" / "sso_provider" / "templates", + BASE_DIR / "pretalx" / "templates", ], "OPTIONS": { "context_processors": [ @@ -687,8 +686,6 @@ def merge_csp(*options, config=None): plugins=PLUGINS, ) -# Below is configuration for SSO using eventyay-ticket - EVENTYAY_TICKET_BASE_PATH = config.get( "urls", "eventyay-ticket", fallback="https://app-test.eventyay.com/tickets" ) @@ -700,15 +697,7 @@ def merge_csp(*options, config=None): ACCOUNT_USER_MODEL_USERNAME_FIELD = "name" # redirect to home page after login with eventyay-ticket LOGIN_REDIRECT_URL = BASE_PATH -# custom form for signup and adapter -SOCIALACCOUNT_FORMS = {"signup": "pretalx.sso_provider.forms.CustomSignUpForm"} -SOCIALACCOUNT_ADAPTER = "pretalx.sso_provider.views.CustomSocialAccountAdapter" # disable confirm step when using eventyay-ticket to login -SOCIALACCOUNT_LOGIN_ON_GET = True -# eventyay-ticket provider configuration -EVENTYAY_TICKET_SSO_WELL_KNOW_URL = "/".join( - [EVENTYAY_TICKET_BASE_PATH, "{org}", ".well-known/openid-configuration"] -) # redirect_url as https ACCOUNT_DEFAULT_HTTP_PROTOCOL = "https" diff --git a/src/pretalx/sso_provider/__init__.py b/src/pretalx/sso_provider/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/pretalx/sso_provider/apps.py b/src/pretalx/sso_provider/apps.py deleted file mode 100644 index 1f2f0c97b..000000000 --- a/src/pretalx/sso_provider/apps.py +++ /dev/null @@ -1,12 +0,0 @@ -from django.apps import AppConfig - - -class SSOProviderConfig(AppConfig): - name = "pretalx.sso_provider" - - def ready(self): - from allauth.socialaccount import providers - - from .providers import EventyayProvider - - providers.registry.register(EventyayProvider) diff --git a/src/pretalx/sso_provider/forms.py b/src/pretalx/sso_provider/forms.py deleted file mode 100644 index 0eea30e1b..000000000 --- a/src/pretalx/sso_provider/forms.py +++ /dev/null @@ -1,7 +0,0 @@ -from allauth.socialaccount.forms import SignupForm - - -class CustomSignUpForm(SignupForm): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - # TODO add custom fields here diff --git a/src/pretalx/sso_provider/providers.py b/src/pretalx/sso_provider/providers.py deleted file mode 100644 index 850e15ae4..000000000 --- a/src/pretalx/sso_provider/providers.py +++ /dev/null @@ -1,98 +0,0 @@ -import logging -from urllib.parse import urlencode - -import requests -from allauth.account.models import EmailAddress -from allauth.core.exceptions import ImmediateHttpResponse -from allauth.socialaccount.helpers import render_authentication_error -from allauth.socialaccount.models import SocialApp -from allauth.socialaccount.providers.base import ProviderAccount -from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider -from django.conf import settings -from django.urls import reverse - -from .views import EventyayTicketOAuth2Adapter - -logger = logging.getLogger(__name__) - - -class Scope(object): - OPEN_ID = "openid" - EMAIL = "email" - PROFILE = "profile" - - -class EventYayTicketAccount(ProviderAccount): - - def get_profile_url(self): - return self.account.extra_data.get("link") - - def get_avatar_url(self): - return self.account.extra_data.get("picture") - - -class EventyayProvider(OAuth2Provider): - id = "eventyay" - name = "Eventyay" - account_class = EventYayTicketAccount - oauth2_adapter_class = EventyayTicketOAuth2Adapter - - def __init__(self, request, app=None): - if hasattr(request, "event"): - app = SocialApp.objects.get(provider=request.event.organiser.slug) - self.id = request.event.organiser.slug - elif request.session.get("org") is not None: - app = SocialApp.objects.get(provider=request.session.get("org")) - self.id = request.session.get("org") - super(EventyayProvider, self).__init__(request, app=app) - - def get_openid_config(self): - try: - response = requests.get( - settings.EVENTYAY_TICKET_SSO_WELL_KNOW_URL.format( - org=self.request.session.get("org") - ) - ) - response.raise_for_status() - except Exception as e: - logger.error(f"Error when getting openid config: {e}") - raise ImmediateHttpResponse( - render_authentication_error( - self.request, - "Error happened when trying get configurations from Eventyay-ticket", - ) - ) - return response.json() - - def get_default_scope(self): - scope = [Scope.PROFILE] - scope.append(Scope.EMAIL) - scope.append(Scope.OPEN_ID) - return scope - - def extract_uid(self, data): - if "sub" in data: - return data["sub"] - return data["id"] - - def extract_common_fields(self, data): - return dict(email=data.get("email"), username=data.get("name")) - - def extract_email_addresses(self, data): - ret = [] - email = data.get("email") - if email: - verified = bool(data.get("email_verified") or data.get("verified_email")) - ret.append(EmailAddress(email=email, verified=verified, primary=True)) - return ret - - def get_login_url(self, request, **kwargs): - current_event = request.event - request.session["org"] = current_event.organiser.slug - url = reverse("eventyay_login") # Base login url for sso with eventyay-ticker - if kwargs: - url = url + "?" + urlencode(kwargs) - return url - - -provider_classes = [EventyayProvider] diff --git a/src/pretalx/sso_provider/templates/socialaccount/authentication_error.html b/src/pretalx/sso_provider/templates/socialaccount/authentication_error.html deleted file mode 100644 index 8b5949a2f..000000000 --- a/src/pretalx/sso_provider/templates/socialaccount/authentication_error.html +++ /dev/null @@ -1,17 +0,0 @@ -{% extends "socialaccount/base.html" %} -{% load compress %} -{% load i18n %} -{% load static %} -{% load allauth %} - -{% block head_title %} - {% trans "Third-Party Login Failure" %} -{% endblock head_title %} -{% block content %} - {% element h1 %} - {% trans "Eventyay-ticket Login Failure" %} - {% endelement %} - {% element p %} - {% trans "An error occurred while attempting to login via your Eventyay-ticket account." %} - {% endelement %} -{% endblock content %} diff --git a/src/pretalx/sso_provider/templates/socialaccount/base.html b/src/pretalx/sso_provider/templates/socialaccount/base.html deleted file mode 100644 index 7cfd0ea57..000000000 --- a/src/pretalx/sso_provider/templates/socialaccount/base.html +++ /dev/null @@ -1,163 +0,0 @@ -{% load compress %} -{% load i18n %} -{% load rules %} -{% load static %} - - - - - {% block title %}{% endblock %} {% if request.event %} {{ request.event.name }} {% endif %}:: {{site_name}} - - - - - - {{ html_head|safe }} - {% if request.event and request.event.display_settings.meta_noindex %}{% else %}{% endif %} - - - - {% block meta_image %}{% if request.event %} - - {% endif %}{% endblock %} - - - - - - {% include "common/includes/favicon.html" %} - - - {% if request.event and request.event.settings.cfp_css_file %} - - - - {% else %} - {% compress css %} - - {% endcompress %} - {% endif %} - {% if request.event and request.event.custom_css %}{% endif %} - {% compress js %} - - - - - - {% endcompress %} - {% block custom_header %}{% endblock %} - - -
- {% if request.event and request.event.header_image %} - {# we're not lazy-loading the header image, even though it can be large, because it's a bit jarring to see it flash in 100ms after the page load #} - - {% endif %} -
- {% if request.event and not request.event.is_public and not is_html_export %} -
- - {% blocktranslate trimmed %} - This event is currently non-public. Only organisers can see it. - {% endblocktranslate %} -
- {% endif %} -
-
-

- - {% if request.event and request.event.logo %} - - {% elif request.event %} - {{ request.event.name }} - {% endif %} - -

-
- {% if request.event and request.event.locales|length > 1 and not is_html_export %} -
- {% for l, name in request.event.named_locales %} - {{ name }} - {% endfor %} - •  -
- {% endif %} - {% if request.event and request.user.is_authenticated and not is_html_export %} - - {% elif request.event and not is_html_export %} - login - {% endif %} -
-
-
-
- {% if messages %} - {% for message in messages %} -
- {{ message }} -
- {% endfor %} - {% endif %} - - {% block content %} - {% endblock %} -
-
-
- {% if is_html_export %} -
- {% now "Y-m-d H:i T" as timestamp %} - {% blocktranslate trimmed %} - This is a static export generated at {{ timestamp }} - {% endblocktranslate %} -
- {% endif %} - {% include "common/powered_by.html" %} - {% if request.event %} - · - {% translate "Contact us" %} - {% endif %} - {% if request.event.display_settings.imprint_url %} - · - {% translate "Imprint" %} - {% endif %} - {% for footer in footer_links %} - · - {{ footer.label }} - {% endfor %} -
-
- - diff --git a/src/pretalx/sso_provider/templates/socialaccount/signup.html b/src/pretalx/sso_provider/templates/socialaccount/signup.html deleted file mode 100644 index 8dbe53d5b..000000000 --- a/src/pretalx/sso_provider/templates/socialaccount/signup.html +++ /dev/null @@ -1,39 +0,0 @@ -{% extends "socialaccount/base.html" %} -{% load bootstrap4 %} -{% load compress %} -{% load i18n %} -{% load static %} -{% load allauth %} - -{% block content %} - -{% bootstrap_form_errors form %} - {% element h1 %} - {% trans "Sign Up" %} - {% endelement %} - {% element p %} - {% blocktrans with provider_name=account.get_provider.name site_name=site.name %}You are about to use your {{provider_name}} account to login. - But your info username/email from Eventyay-ticket already existed in our system, please choose other username/email:{% endblocktrans %} - {% endelement %} - {% url 'socialaccount_signup' as action_url %} - {% element form form=form method="post" action=action_url %} - {% slot body %} - {% csrf_token %} - {% for field in form %} - {% if field.name == 'email' %} - {% bootstrap_field field layout="inline" %} - {% else %} - {% bootstrap_field field layout="inline" %} - {% endif %} - {% endfor %} - {{ redirect_field }} - {% endslot %} - {% slot actions %} - - {% endslot %} - {% endelement %} - -{% endblock content %} - diff --git a/src/pretalx/sso_provider/urls.py b/src/pretalx/sso_provider/urls.py deleted file mode 100644 index 2c2f8575f..000000000 --- a/src/pretalx/sso_provider/urls.py +++ /dev/null @@ -1,5 +0,0 @@ -from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns - -from .providers import EventyayProvider - -urlpatterns = default_urlpatterns(EventyayProvider) diff --git a/src/pretalx/sso_provider/views.py b/src/pretalx/sso_provider/views.py deleted file mode 100644 index b0a982e7d..000000000 --- a/src/pretalx/sso_provider/views.py +++ /dev/null @@ -1,97 +0,0 @@ -import requests -from allauth.socialaccount.adapter import DefaultSocialAccountAdapter -from allauth.socialaccount.providers.oauth2.views import ( - OAuth2Adapter, - OAuth2CallbackView, - OAuth2LoginView, -) -from allauth.utils import build_absolute_uri -from django.core.exceptions import ImproperlyConfigured -from django.db import IntegrityError -from django.urls import NoReverseMatch, reverse - - -class EventyayTicketOAuth2Adapter(OAuth2Adapter): - - def __init__(self, request): - if request.session.get("org") is not None: - self.provider_id = request.session.get("org") - else: - self.provider_id = "eventyay" - super().__init__(request) - - @property - def access_token_url(self): - config = self.get_provider().get_openid_config() - return config["token_endpoint"] - - @property - def authorize_url(self): - config = self.get_provider().get_openid_config() - return config["authorization_endpoint"] - - @property - def profile_url(self): - config = self.get_provider().get_openid_config() - return config["userinfo_endpoint"] - - def complete_login(self, request, app, token, **kwargs): - headers = {"Authorization": f"Bearer {token.token}"} - response = requests.get(self.profile_url, headers=headers) - response.raise_for_status() - extra_data = response.json() - return self.get_provider().sociallogin_from_response(request, extra_data) - - def get_callback_url(self, request, app): - try: - callback_url = reverse(self.provider_id + "_callback") - except NoReverseMatch: - callback_url = reverse("eventyay_callback") # Default call back url - protocol = self.redirect_uri_protocol - return build_absolute_uri(request, callback_url, protocol) - - -class CustomSocialAccountAdapter(DefaultSocialAccountAdapter): - - def get_provider(self, request, provider, client_id=None): - """Looks up a `provider`, supporting subproviders by looking up by - `provider_id`. - """ - from allauth.socialaccount.providers import registry - - try: - provider_class = registry.get_class(provider) - if provider_class is None or provider_class.uses_apps: - app = self.get_app(request, provider=provider, client_id=client_id) - if not provider_class: - provider_class = registry.get_class(app.provider) - if not provider_class: - raise ImproperlyConfigured("unknown provider: %s", app.provider) - return provider_class(request, app=app) - elif provider_class: - assert not provider_class.uses_apps - return provider_class(request, app=None) - else: - raise ImproperlyConfigured("unknown provider: %s", provider) - except ImproperlyConfigured: - app = self.get_app(request, provider=provider, client_id=client_id) - if app is not None: - provider_class = registry.get_class( - "eventyay" - ) # Get default custom provider - return provider_class(request, app=app) - else: - raise ImproperlyConfigured("unknown provider: " + app.provider) - - def save_user(self, request, sociallogin, form=None): - try: - sociallogin.user.code = sociallogin.account.extra_data.get("sub") - super().save_user(request, sociallogin, form) - except IntegrityError: - # bypass the error if the user with this email created in eventyay-talk - # before - pass - - -oauth2_login = OAuth2LoginView.adapter_view(EventyayTicketOAuth2Adapter) -oauth2_callback = OAuth2CallbackView.adapter_view(EventyayTicketOAuth2Adapter) diff --git a/src/pretalx/urls.py b/src/pretalx/urls.py index 7dae97b9e..b82b28308 100644 --- a/src/pretalx/urls.py +++ b/src/pretalx/urls.py @@ -30,7 +30,6 @@ path("", include("pretalx.agenda.urls", namespace="agenda")), path("", include("pretalx.cfp.urls", namespace="cfp")), path("", include((plugin_patterns, "plugins"))), - re_path(r"^accounts/", include("allauth.urls")), ] handler500 = "pretalx.common.views.handle_500"