django-allauth:仅允许特定Google应用程式网域的使用者 [英] django-allauth: Only allow users from a specific google apps domain

查看:139
本文介绍了django-allauth:仅允许特定Google应用程式网域的使用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



django-social-auth为此列出了白名单域名参数,如何在allauth中包含此信息?
我发现django-allauth在django-social-auth上花了几个小时更容易设置



任何帮助将不胜感激。

解决方案

回答我自己的问题 -



这其实很简单。您想要做的是在用户通过社交帐户提供商进行身份验证之后,在他进入他的个人资料页面之前停止登录。您可以使用 allauth / socialaccount / adapter中的 DefaultSocialAccountAdapter 类的



pre_social_login 方法.py


 在用户通过
成功验证后调用社会供应商,但在登录实际处理
之前(和pre_social_login信号发出之前)。
您可以使用此挂钩进行干预,例如中止登录
提高ImmediateHttpResponse
为什么一个适配器钩子和信号?介入
,例如来自信号处理程序中的流程是错误的 - 多个
处理程序可能处于活动状态,并以未确定的顺序执行。


执行类似


$ b来自allauth.socialaccount.adaptor导入的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $::::::::::::::::::::::::::::::::::::
u = sociallogin.account.user
如果不是u.email.split('@')[1] ==example.com
raise ImmediateHttpResponse(render_to_response('error.html' ))

这不是一个确切的实现,但这样的工作。


I am a newbie at Django. Using django-allauth I have set up single click sign in. I obtained my domain credentials ( client_id and secret_key) from google api console. But the problem is django-allauth is letting me login from any google account while I want the email addresses to be restricted to my domain ( @example.com instead of @gmail.com)

django-social-auth has the white listed domains parameter for this, how do I include this information in allauth? I found django-allauth much easier to set up after spending hours on django-social-auth

Any help would be much appreciated.

解决方案

Answering my own question-

This is actually pretty simple. What you want to do is stall the login after a user has been authenticated by a social account provider and before he can proceed to his profile page. You can do this with the

pre_social_login method of the DefaultSocialAccountAdapter class in allauth/socialaccount/adaptor.py

    Invoked just after a user successfully authenticates via a
    social provider, but before the login is actually processed
    (and before the pre_social_login signal is emitted).
    You can use this hook to intervene, e.g. abort the login by
    raising an ImmediateHttpResponse
    Why both an adapter hook and the signal? Intervening in
    e.g. the flow from within a signal handler is bad -- multiple
    handlers may be active and are executed in undetermined order.

Do something like

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'))

This is not an exact implementation but something like this works.

这篇关于django-allauth:仅允许特定Google应用程式网域的使用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆