簡體   English   中英

使用Python Social Auth過濾或限制Django中的一組用戶使用Google-Oauth2登錄

[英]Filtering or restricting Google-Oauth2 login to a set of users in Django using Python Social Auth

我已經能夠通過域過濾掉它們,但是如果我想使用一些正則表達式進行進一步的過濾。 我可以那樣做嗎? 最近我在做很多研究。 我認為建立部分管道是解決方案。 但是由於文檔不佳; 我無法實現自己的目標。 添加中間件也無濟於事,因為登錄過程會完全覆蓋我添加的功能。 我想做這樣的事情。

中間件

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",{})

問題是它處理每個請求,而不是專門處理登錄請求。 還行不通。 請指出我要去哪里了。

感謝幫助! 謝謝!

為此,您需要一個管道函數,這很簡單,因為這可以解決問題:

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_auth.auth_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'
)

是的 因此它正在按預期方式工作。 我沒有測試正確的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM