Browse Source

Support custom social auth errors

Rafał Pitoń 7 years ago
parent
commit
cbaf8c3395

+ 2 - 2
misago/core/errorpages.py

@@ -61,8 +61,8 @@ def page_not_found(request, exception):
 def social_auth_failed(request, exception):
 def social_auth_failed(request, exception):
     backend_name = None
     backend_name = None
     ban = None
     ban = None
-    message = None
     help_text = None
     help_text = None
+    message = None
 
 
     try:
     try:
         backend_name = exception.backend_name
         backend_name = exception.backend_name
@@ -103,7 +103,7 @@ def social_auth_failed(request, exception):
         'ban': ban,
         'ban': ban,
         'message': message,
         'message': message,
         'help_text': help_text,
         'help_text': help_text,
-    }, status=400)
+    }, status=403)
 
 
 
 
 @admin_csrf_failure
 @admin_csrf_failure

+ 2 - 0
misago/core/testproject/urls.py

@@ -73,6 +73,8 @@ urlpatterns = [
     url(r'^forum/social-auth-failed/$', views.raise_social_auth_failed, name='raise-social-auth-failed'),
     url(r'^forum/social-auth-failed/$', views.raise_social_auth_failed, name='raise-social-auth-failed'),
     url(r'^forum/social-wrong-backend/$', views.raise_social_wrong_backend, name='raise-social-wrong-backend'),
     url(r'^forum/social-wrong-backend/$', views.raise_social_wrong_backend, name='raise-social-wrong-backend'),
     url(r'^forum/social-not-allowed-to-disconnect/$', views.raise_social_not_allowed_to_disconnect, name='raise-social-not-allowed-to-disconnect'),
     url(r'^forum/social-not-allowed-to-disconnect/$', views.raise_social_not_allowed_to_disconnect, name='raise-social-not-allowed-to-disconnect'),
+    url(r'^forum/raise-social-auth-failed-message/$', views.raise_social_auth_failed_message, name='raise-social-auth-failed-message'),
+    url(r'^forum/raise-social-auth-banned/$', views.raise_social_auth_banned, name='raise-social-auth-banned'),
     url(r'^test-403/$', views.raise_403, name='raise-403'),
     url(r'^test-403/$', views.raise_403, name='raise-403'),
     url(r'^test-404/$', views.raise_404, name='raise-404'),
     url(r'^test-404/$', views.raise_404, name='raise-404'),
     url(r'^test-redirect/$', views.test_redirect, name='test-redirect'),
     url(r'^test-redirect/$', views.test_redirect, name='test-redirect'),

+ 10 - 1
misago/core/testproject/views.py

@@ -8,7 +8,7 @@ from social_core.backends.github import GithubOAuth2
 
 
 from misago.core import errorpages, mail
 from misago.core import errorpages, mail
 from misago.core.decorators import require_POST
 from misago.core.decorators import require_POST
-from misago.core.exceptions import Banned
+from misago.core.exceptions import Banned, SocialAuthBanned, SocialAuthFailed
 from misago.core.shortcuts import paginate, paginated_response, validate_slug
 from misago.core.shortcuts import paginate, paginated_response, validate_slug
 from misago.core.views import home_redirect
 from misago.core.views import home_redirect
 from misago.users.models import Ban
 from misago.users.models import Ban
@@ -135,6 +135,15 @@ def raise_social_not_allowed_to_disconnect(request):
     raise NotAllowedToDisconnect()
     raise NotAllowedToDisconnect()
 
 
 
 
+def raise_social_auth_failed_message(request):
+    raise SocialAuthFailed(GithubOAuth2, "This message will be shown to user!")
+
+
+def raise_social_auth_banned(request):
+    ban = Ban(user_message="Banned in auth!")
+    raise SocialAuthBanned(GithubOAuth2, ban)
+
+
 def test_redirect(request):
 def test_redirect(request):
     return home_redirect(request)
     return home_redirect(request)
 
 

+ 23 - 10
misago/core/tests/test_errorpages.py

@@ -23,6 +23,7 @@ class ErrorPageViewsTests(TestCase):
         """banned error page has no show-stoppers"""
         """banned error page has no show-stoppers"""
         response = self.client.get(reverse('raise-misago-banned'))
         response = self.client.get(reverse('raise-misago-banned'))
         self.assertContains(response, "misago:error-banned", status_code=403)
         self.assertContains(response, "misago:error-banned", status_code=403)
+        self.assertContains(response, "<p>Banned for test!</p>", status_code=403)
         self.assertContains(response, encode_json_html("<p>Banned for test!</p>"), status_code=403)
         self.assertContains(response, encode_json_html("<p>Banned for test!</p>"), status_code=403)
 
 
     def test_permission_denied_returns_403(self):
     def test_permission_denied_returns_403(self):
@@ -43,21 +44,33 @@ class ErrorPageViewsTests(TestCase):
         self.assertContains(response, "misago:error-405", status_code=405)
         self.assertContains(response, "misago:error-405", status_code=405)
         self.assertContains(response, "Wrong way", status_code=405)
         self.assertContains(response, "Wrong way", status_code=405)
 
 
-    def test_social_auth_failed_returns_400(self):
-        """social auth's failed error returns 400"""
+    def test_social_auth_failed_returns_403(self):
+        """social auth's failed error returns 403"""
         response = self.client.get(reverse('raise-social-auth-failed'))
         response = self.client.get(reverse('raise-social-auth-failed'))
-        self.assertContains(response, "page-error-social", status_code=400)
-        self.assertContains(response, "GitHub", status_code=400)
+        self.assertContains(response, "page-error-social", status_code=403)
+        self.assertContains(response, "GitHub", status_code=403)
 
 
-    def test_social_wrong_backend_returns_400(self):
-        """social auth's wrong backend error returns 400"""
+    def test_social_wrong_backend_returns_403(self):
+        """social auth's wrong backend error returns 403"""
         response = self.client.get(reverse('raise-social-wrong-backend'))
         response = self.client.get(reverse('raise-social-wrong-backend'))
-        self.assertContains(response, "page-error-social", status_code=400)
+        self.assertContains(response, "page-error-social", status_code=403)
 
 
-    def test_social_not_allowed_to_disconnect_returns_400(self):
-        """social auth's not allowed to disconnect error returns 400"""
+    def test_social_not_allowed_to_disconnect_returns_403(self):
+        """social auth's not allowed to disconnect error returns 403"""
         response = self.client.get(reverse('raise-social-not-allowed-to-disconnect'))
         response = self.client.get(reverse('raise-social-not-allowed-to-disconnect'))
-        self.assertContains(response, "page-error-social", status_code=400)
+        self.assertContains(response, "page-error-social", status_code=403)
+
+    def test_social_failed_message(self):
+        """misago-specific social auth failed exception error page returns 403 with message"""
+        response = self.client.get(reverse('raise-social-auth-failed-message'))
+        self.assertContains(response, "page-error-social", status_code=403)
+        self.assertContains(response, "This message will be shown to user!", status_code=403)
+
+    def test_social_auth_banned(self):
+        """misago-specific social auth banned exception error page returns 403 with ban message"""
+        response = self.client.get(reverse('raise-social-auth-banned'))
+        self.assertContains(response, "page-error-social", status_code=403)
+        self.assertContains(response, "Banned in auth!", status_code=403)
 
 
 
 
 @override_settings(ROOT_URLCONF='misago.core.testproject.urlswitherrorhandlers')
 @override_settings(ROOT_URLCONF='misago.core.testproject.urlswitherrorhandlers')

+ 21 - 0
misago/templates/misago/errorpages/ban_message.html

@@ -0,0 +1,21 @@
+{% load i18n %}
+{% with ban.get_serialized_message.message as ban_message %}
+    {% if ban_message.html %}
+        <div class="lead">
+            {{ ban_message.html|safe }}
+        </div>
+    {% else %}
+        <p class="lead">
+            {{ ban_message.plain }}
+        </p>
+    {% endif %}
+{% endwith %}
+<p className="message-footnote">
+    {% if ban.expires_on %}
+        {% blocktrans trimmed with expires_on=ban.expires_on|date:"DATETIME_FORMAT" %}
+            This ban expires on {{ expires_on }}.
+        {% endblocktrans %}
+    {% else %}
+        {% trans "This ban is permanent." %}
+    {% endif %}
+</p>

+ 1 - 19
misago/templates/misago/errorpages/banned.html

@@ -21,25 +21,7 @@
       </div>
       </div>
 
 
       <div class="message-body">
       <div class="message-body">
-        {% if ban.message.html %}
-        <div class="lead">
-          {{ ban.message.html|safe }}
-        </div>
-        {% else %}
-        <p class="lead">
-          {{ ban.message.plain }}
-        </p>
-        {% endif %}
-
-        <p className="message-footnote">
-          {% if ban.expires_on %}
-            {% blocktrans trimmed with expires_on=ban.expires_on|date:"DATETIME_FORMAT" %}
-              This ban expires on {{ expires_on }}.
-            {% endblocktrans %}
-          {% else %}
-            {% trans "This ban is permanent." %}
-          {% endif %}
-        </p>
+        {% include "misago/errorpages/ban_message.html" %}
       </div>
       </div>
 
 
     </div>
     </div>

+ 12 - 8
misago/templates/misago/errorpages/social.html

@@ -29,14 +29,18 @@
       </div>
       </div>
 
 
       <div class="message-body">
       <div class="message-body">
-        <p class="lead">{{ message }}</p>
-        <p>
-          {% if help_text %}
-            {{ help_text }}
-          {% else %}
-            {% trans "Please try again or use another method to sign in if the problem persists." %}
-          {% endif %}
-        </p>
+        {% if ban %}
+          {% include "misago/errorpages/ban_message.html" %}
+        {% else %}
+          <p class="lead">{{ message }}</p>
+          <p>
+            {% if help_text %}
+              {{ help_text }}
+            {% else %}
+              {% trans "Please try again or use another method to sign in if the problem persists." %}
+            {% endif %}
+          </p>
+        {% endif %}
       </div>
       </div>
 
 
     </div>
     </div>