【问题标题】:django-allauth: Only allow users from a specific google apps domaindjango-allauth:只允许来自特定谷歌应用域的用户
【发布时间】:2013-10-07 11:04:45
【问题描述】:

我是 Django 的新手。使用 django-allauth 我设置了单击登录。我从 google api 控制台获得了我的域凭据(client_id 和 secret_key)。但问题是 django-allauth 允许我从任何 google 帐户登录,而我希望将电子邮件地址限制在我的域中(@example.com 而不是@gmail.com)

django-social-auth 对此有白名单域参数,我如何在 allauth 中包含此信息? 在 django-social-auth 上花费数小时后,我发现 django-allauth 更容易设置

任何帮助将不胜感激。

【问题讨论】:

    标签: django django-allauth


    【解决方案1】:

    回答我自己的问题-

    您想要做的是在用户通过社交帐户提供商的身份验证之后并在他们可以继续访问他们的个人资料页面之前停止登录。您可以使用

    allauth/socialaccount/adaptor.pyDefaultSocialAccountAdapter类的

    pre_social_login方法

    在用户通过 社交提供者,但在实际处理登录之前 (在发出 pre_social_login 信号之前)。 您可以使用此钩子进行干预,例如中止登录 引发 ImmediateHttpResponse 为什么要同时使用适配器挂钩和信号?介入 例如来自信号处理程序的流程很糟糕——多个 处理程序可能处于活动状态并以未确定的顺序执行。

    做类似的事情

    from allauth.socialaccount.adaptor import DefaultSocialAccountAdapter
    
    class MySocialAccount(DefaultSocialAccountAdapter):
        def pre_social_login(self, request, sociallogin):
            u = sociallogin.account.user
            if not u.email.split('@')[1] == "example.com"
                raise ImmediateHttpResponse(render_to_response('error.html'))
    

    这不是一个精确的实现,但类似这样的工作。

    【讨论】:

    • 并在创建自定义适配器后,按照插件文档中的配置部分将其注册到settings.SOCIALACCOUNT_ADAPTER
    • 为了将来参考,我发现我必须使用u = sociallogin.user,否则如果用户以前不存在,我会收到错误。
    【解决方案2】:

    这是一个替代解决方案:

    from allauth.account.adapter import DefaultAccountAdapter
    from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
    
    class CustomAccountAdapter(DefaultAccountAdapter):
        def is_open_for_signup(self, request):
            return False # No email/password signups allowed
    
    class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
        def is_open_for_signup(self, request, sociallogin):
            u = sociallogin.user
            # Optionally, set as staff now as well.
            # This is useful if you are using this for the Django Admin login.
            # Be careful with the staff setting, as some providers don't verify
            # email address, so that could be considered a security flaw.
            #u.is_staff = u.email.split('@')[1] == "customdomain.com"
            return u.email.split('@')[1] == "customdomain.com"
    

    此代码可以存在于任何地方,但假设它位于 mysite/adapters.py 中,您还需要在 settings.py 中提供以下内容:

    ACCOUNT_ADAPTER = 'mysite.adapters.CustomAccountAdapter'
    SOCIALACCOUNT_ADAPTER = 'mysite.adapters.CustomSocialAccountAdapter'
    

    【讨论】:

    • 我尝试了这个解决方案,但我不断收到来自社交帐户登录的“注册已关闭”。已设置 SOCIALACCOUNT_LOGIN_ON_GET = TrueSOCIALACCOUNT_AUTO_SIGN_UP = True
    【解决方案3】:

    您可以在注册过程中覆盖 allauth 的 allauth.socialaccount.forms.SignupForm 并检查域。 Discalmer:这一切都是在没有测试的情况下编写的,但其中的一些东西应该可以工作。

    # settings.py
    # not necesarry, but it would be a smart way to go instead of hardcoding it
    ALLOWED_DOMAIN = 'example.com'
    

    .

    # forms.py
    from django.conf import settings
    from allauth.socialaccount.forms import SignupForm
    
    
    class MySignupForm(SignupForm):
    
        def clean_email(self):
            data = self.cleaned_data['email']
            if data.split('@')[1].lower() == settings.ALLOWED_DOMAIN:
                raise forms.ValidationError(_(u'domena!'))
            return data
    

    在您的网址中覆盖 allauth 默认值(将其放在 django-allauth 的包含之前)

    # urls.py
    
    from allauth.socialaccount.views import SignupView
    from .forms import MySignupForm
    
    
    urlpatterns = patterns('',
        # ...
        url(r"^social/signup/$", SignupView.as_view(form_class=MySignupForm), name="account_signup"),
        # ...
    )
    

    我不确定“^social/signup/$”,请重新检查。

    【讨论】:

    • 正确的网址是accounts/social/signup。但这不起作用。我不想在使用 google 登录后显示 allauth 注册表单。我想要的是,在谷歌登录后立即将他们定向到他们的个人资料页面,或者他们收到一条错误消息,说我们只接受来自 example.com 域的用户。
    • 我应该使用 django-allauth 的 pre_social_login 信号来进行域匹配吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多