Django Python Social Auth仅允许某些用户登录

时间:2016-09-13 14:50:24

标签: python django python-social-auth

我只希望来自电子邮件地址列表中的@ companyname.net电子邮件的用户能够通过google +使用Python Social Auth登录。我该如何做到这一点?

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['companyname.net']

是我目前在settings.py中的内容,但只允许@ companyname.net-ers登录。

2 个答案:

答案 0 :(得分:1)

解决此问题的一种方法是覆盖python-social-auth管道。

您可以使用以下内容覆盖create_user:

def create_user(strategy, details, user=None, *args, **kwargs):
    if user:
        return {'is_new': False}

    allowed_emails = get_list_of_emails()

    fields = dict((name, kwargs.get(name, details.get(name)))
                  for name in strategy.setting('USER_FIELDS', USER_FIELDS))
    if not fields:
        return

    if fields[email] in allowed_emails:        
        return {
            'is_new': True,
            'user': strategy.create_user(**fields)
        }

    return

此方法get_list_of_emails()将用作从数据库中加载来自文件的电子邮件的方法。它需要返回一个电子邮件列表。

然后,在设置的SOCIAL_AUTH_PIPELINE中,将create_user替换为自定义方法:

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

通过这种方式,您可以保留domais白名单,然后将您想要的电子邮件存储在可以使用get_list_of_emails()

方法加载它们的地方

更多关于docs

答案 1 :(得分:0)

这是我经过测试的实现:

import os
import psycopg2
from Utils.common import setup_logging

LOG = setup_logging(__name__)
USER_FIELDS = ['username', 'email']


def get_whitelisted_emails():
    whitelisted_domains_emails = []
    try:
        connection = psycopg2.connect(user=os.environ.get('DB_USER'),
                                      password=os.environ.get('DB_PASSWORD'),
                                      host='localhost',
                                      port=os.environ.get('DB_PORT'),
                                      database=os.environ.get('DB_NAME'))
        cursor = connection.cursor()
        get_whitelisted_domains_emails = "SELECT domain, email FROM cortex_emails WHERE is_active=True;"
        cursor.execute(get_whitelisted_domains_emails)
        whitelisted_domains_emails = cursor.fetchall()
        connection.close()
    except(Exception, psycopg2.Error) as error:
        LOG.info('Failed to connect to database...')

    return [email for _, email in whitelisted_domains_emails]


def create_user(strategy, details, user=None, *args, **kwargs):
    if user:
        return {'is_new': False}

    allowed_emails = get_whitelisted_emails()

    fields = dict((name, kwargs.get(name, details.get(name)))
                  for name in strategy.setting('USER_FIELDS', USER_FIELDS))
    if not fields:
        return

    if fields['email'] in allowed_emails:
        return {
            'is_new': True,
            'user': strategy.create_user(**fields)
        }

    return
相关问题