我正在为我的组织使用网络应用程序(让我们说“ students.uni”和“ academics.uni”) 我的组织使用Google服务,因此它们是Google域,我想仅对使用“ social_django”的域限制对应用程序的访问权限 我正在使用Django 2.0.6和Python 3.6.5
我正在使用它来限制对应用程序的访问权限,效果很好:
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['student.uni', 'academics.uni']
当我登录或使用允许的帐户注册时,效果很好,但是,当不允许使用该帐户的域时,我想重定向或将用户发送到另一个页面,以使他知道发生了什么事。
我一直在尝试:
SOCIAL_AUTH_LOGIN_ERROR_URL = 'principal'
但是我无法使其正常工作,因为如果设置DEBUG = False
,我会不断收到AuthForbidden异常或500错误
关于不允许该域时如何进行重定向或发送消息的任何建议?非常感谢
某些代码:
Settings.py(不是全部,而是相关代码)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.user',
'apps.inv',
'apps.sol',
'social_django',
]
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth', # for Google authentication
'social_core.backends.google.GoogleOpenId', # for Google authentication
'social_core.backends.google.GoogleOAuth2', # for Google authentication
'django.contrib.auth.backends.ModelBackend',
)
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'principal'
#DEBUG = False
SOCIAL_AUTH_LOGIN_ERROR_URL = 'principal'
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY ='****.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '******'
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['student.uni', 'academics.uni']
尝试使用另一个域帐户登录时获得的页面:
AuthForbidden at /auth/complete/google-oauth2/
Your credentials aren't allowed
Request Method: GET
Request URL: http://127.0.0.1:8000/auth/complete/google-oauth2/?state=8*********&code=4/***********-***********************************&authuser=0&session_state=*****************************..e40c&prompt=none
Django Version: 2.0.6
Exception Type: AuthForbidden
Exception Value:
Your credentials aren't allowed
Exception Location: C:\ambientes\prueba\lib\site-packages\social_core\pipeline\social_auth.py in auth_allowed, line 14
Python Executable: C:\ambientes\prueba\Scripts\python.exe
Python Version: 3.6.5
Python Path:
['C:\\proyectosDjango\\Modular',
'C:\\ambientes\\prueba\\Scripts\\python36.zip',
'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\espar\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\ambientes\\prueba',
'C:\\ambientes\\prueba\\lib\\site-packages']
Server time: Dom, 1 Jul 2018 15:45:32 -0500
答案 0 :(得分:2)
经过(大量?)研究,我找到了一种方法。 继续:我只希望来自域列表(提供Google)的电子邮件能够在我的网站上注册:example1 @ domain1.com,example2 @ another.domain.com 您可以找到登录Google here和here的基本步骤 完成这些步骤后,无论其域是什么,您都可以使用任何google,facebook,twitter帐户登录。 下一步是在 settings.py
中定义下一个变量SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['domain1.edu.com', 'domain2.edu.com']
这将过滤您希望能够注册的域。
您也可以设置下一个,尽管这些与我现在无关,我认为拥有它们会更好
SOCIAL_AUTH_LOGIN_ERROR_URL = '/user/error/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
错误网址定义了登录时发生错误时应用程序应重定向到的路径
我的解决方案从这里开始: 当系统检测到您的域被允许一切正常时,但是当系统检测到您的域不在白名单中时,则会引发 AuthForbidden 异常(是的,即使当我定义它不应引发上述变量的异常时)。由于该异常不是错误,因此不会转到SOCIAL_AUTH_LOGIN_ERROR_URL,您将看到异常屏幕。
解决方案是创建自定义管道方法。您可以找到有关管道here
的介绍1-在与 settings.py 相同的文件夹中创建 pipeline.py 文件 2-定义方法auth_allowed,以便它不会引发异常,而是重定向到其他地方(或执行您想要的任何操作)。我是这样做的:
from django.shortcuts import redirect
def auth_allowed(backend, details, response, *args, **kwargs):
if not backend.auth_allowed(response, details):
return redirect('user:error')#<-here goes your url as defined on your urls.py
3-在 settings.py 中定义管道,用您的自定义方法替换auth_allowed
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'modular.pipeline.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)
(模块化是我的proyect的名字,写上您的proyect的名字) (许多帖子将向您展示仅由“ social.pipeline ...”调用的方法,我改用了“ social_core.pipeline ...”,因为社交给了我一个错误)
这应该有效。希望对别人有帮助。