I am using Django 1.10 with python 2.7 and social-auth-app-django (1.2.0). It is part of the Python Social Auth library.
I wish to restrict login to only the domain ID of my company I've therefore used this setting from the library.
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
Now if you try to login with any other domain as expected it throws an error.
My goal is to catch this exception and show a custom page to the user. But for the life of me I am unable to do so.
If I set Debug to False it redirects the user to my
LOGIN_ERROR_URL='/'
page but am unable to pass my custom message to the user
This is part of my setting.py
DEBUG = False
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
]
#social auth
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY= "9******6.apps.googleusercontent.com"
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET="W*****x"
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'upload_file'
LOGOUT_URL = 'logout'
LOGIN_ERROR_URL='logout
In my view I've this code to handle the exception
from social_django.middleware import SocialAuthExceptionMiddleware
from social_core.exceptions import AuthForbidden
class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
print "exception occured"
if hasattr(social_exceptions, 'AuthForbidden'):
print "hello two"
return HttpResponse("I'm a exception %s" % exception)
else:
print "other exception"
raise exception
I've even tried with
def process_template_response(self):
print "response"'
def get_redirect_uri(request, exception):
print "URL"
But to no avail.
I've followed these link python-social-auth AuthCanceled exception
and
This is the output when debug is set to False:
"AuthForbidden at /app/oauth/complete/google-oauth2/ Your credentials aren't allowed"