使用Python Social Auth过滤或限制Google-Oauth2登录到Django中的一组用户

使用Python Social Auth过滤或限制Google-Oauth2登录到Django中的一组用户,python,regex,django,google-oauth,python-social-auth,Python,Regex,Django,Google Oauth,Python Social Auth,我已经能够通过域过滤掉它们,但是如果我想使用一些正则表达式进行进一步过滤的话。我可以这样做吗?我最近做了很多研究;我认为部分管道是解决办法。但是由于糟糕的文档;我没能实现我的目标。 添加中间件也没有帮助,因为登录过程完全覆盖了我添加的功能。 我想做这样的事 中间件.py class MyMiddleware(SocialAuthExceptionMiddleware): def process_exception(self, request, exception): if

我已经能够通过域过滤掉它们,但是如果我想使用一些正则表达式进行进一步过滤的话。我可以这样做吗?我最近做了很多研究;我认为部分管道是解决办法。但是由于糟糕的文档;我没能实现我的目标。 添加中间件也没有帮助,因为登录过程完全覆盖了我添加的功能。 我想做这样的事

中间件.py

class MyMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if type(exception) == AuthForbidden:
            return render(request, "app/needlogin.html", {})

    def process_request(self,request):
        if request.user.is_authenticated():
            name = request.user.username.split('@')[0]
            roll=[]
            for i in name:
                if i.isdigit():
                    roll.append(i)
            if int(roll[1])<3:
                auth_logout(request)
                return render(request,"app/notlogin.html",{})
            # else:
                # return render(request, "app/needlogin.html",{})
类MyMiddleware(SocialAuthExceptionMiddleware):
def过程_异常(自身、请求、异常):
如果类型(异常)=禁止验证:
返回呈现(请求“app/needlogin.html”,{})
def过程_请求(自我,请求):
if request.user.is_经过身份验证():
name=request.user.username.split('@')[0]
滚动=[]
以我的名义:
如果i.isdigit():
滚动追加(一)

如果int(roll[1])您需要一个管道函数来实现该功能,那么像这样简单的方法应该可以实现:

from social.exceptions import AuthForbidden

def email_check(strategy, details, *args, **kwargs):
    if not is_valid_email(details.get(email)):
        raise AuthForbidden(strategy.backend)
然后将此函数粘贴在
“social.pipeline.social\u auth.auth\u allowed”条目之后,如下所示:

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'import.path.to.email_check',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details'
)

嗯。因此,这是工作,因为它应该是。我没有测试正确的条件。

谢谢您的回答!