SOCIAL_AUTH_LOGIN_ERROR_URL 不起作用 Django 2.0.6

V. *_*ldo 5 python django oauth oauth-2.0 google-plus

我正在为我的组织开发一个网络应用程序(比方说“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']
Run Code Online (Sandbox Code Playgroud)

当我登录或注册允许的帐户时,效果很好,但是,当帐户的域不允许时,我想将用户重定向或发送到另一个页面,让他知道发生了什么。

我一直在尝试:

SOCIAL_AUTH_LOGIN_ERROR_URL = 'principal'
Run Code Online (Sandbox Code Playgroud)

但我无法让它工作,因为如果我设置,我会不断收到 AuthForbidden 异常或 500 错误DEBUG = False

关于在不允许域时如何重定向或发送消息的任何建议?非常感谢

一些代码:

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']
Run Code Online (Sandbox Code Playgroud)

尝试使用另一个域帐户登录时出现的页面:

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
Run Code Online (Sandbox Code Playgroud)

V. *_*ldo 5

经过(大量?)研究后,我找到了一种方法。恢复:我只希望来自域列表(谷歌提供)的电子邮件能够在我的网站上注册:example1@domain1.com、example2@another.domain.com 您可以在此处此处找到使用谷歌登录的基本步骤 完成这些步骤后,您将能够使用任何 google、facebook、twitter 帐户登录,无论其域是什么。下一步是在settings.py中定义下一个变量

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['domain1.edu.com', 'domain2.edu.com']
Run Code Online (Sandbox Code Playgroud)

这将过滤您希望能够注册的域。

你也可以设置下一个,尽管这些现在与我无关,我认为最好拥有它们

SOCIAL_AUTH_LOGIN_ERROR_URL = '/user/error/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
Run Code Online (Sandbox Code Playgroud)

错误 url 定义如果登录时发生错误,应用程序应重定向到哪个路径

我的解决方案从这里开始: 当系统检测到您的域允许时,一切都应该顺利,但是当它检测到您的域不在白名单中时,它会抛出AuthForbidden异常(是的,即使我定义它不应该引发上面变量的例外)。由于异常不是错误,因此它不会转到 SOCIAL_AUTH_LOGIN_ERROR_URL,您将看到异常屏幕。

解决方案是创建自定义管道方法。您可以找到管道的介绍您可以在这里

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
Run Code Online (Sandbox Code Playgroud)

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',
)
Run Code Online (Sandbox Code Playgroud)

(modular是我的项目的名称,写下你的项目的名称)(很多帖子都会向你展示仅由“social.pipeline...”调用的方法,我使用“social_core.pipeline...”代替,因为social给了我一个错误)

这应该有效。希望它能帮助某人。