10

我正在使用 django social-auth (http://django-social-auth.readthedocs.org/en/latest/index.html)并尝试创建一个用户配置文件,用户可以在其中关联多个帐户(如 Stackoverflow 上的此处) .

目前,我处于单个用户可以使用不同的身份验证提供程序登录的地步,但每次登录都会创建一个新用户。如何将所有用户帐户关联到一个帐户(如用户个人资料)?

另外,使用 django social-auth 时创建用户个人资料页面的最佳做法是什么?

4

2 回答 2

11

DSA 已经支持多个帐户关联,诀窍是用户必须登录,否则 DSA 不知道它应该与已经存在的帐户关联。

关于您的个人资料,向 DSA 添加功能的推荐方法是扩展管道,您可以创建如下条目:

def create_profile(user, is_new=False, *args, **kwargs):
    if is_new:
        # create a profile instance for the given user
        create_user_profile(user)

然后在这样的设置中注册它:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.user.get_username',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'myapp.pipeline.create_profile'
)

其中条目是获取该功能的导入路径。

编辑:链接到文档和设置说明。

于 2013-04-15T17:46:50.047 回答
1

I see 2 alterntive when you want to handle with multiple account association:

    1. Find a unique attribute that all identity provider has.
    1. Keep a base authentication and link all the rest identity providers to its.

The first approach has 2 disadvantages

  • Is possible that does not exist any attibute that is present in all the identity provider. One common implementatios that people did in the past is to use the email as this attribute to identity the user, but for example Twitter does not share the email anymore.

  • Is posible to find a non consistent problem. For example people can set differents values for an username or for a email in the differents providers.

I recommend you to implement the second approach. Set a primary identity provider (ldap, database or an authentication closed and secure for you). And each time a user want link a "X identity provider to your account", look for the attribute that this provider use and save in a table this relation between your primary identity and that new identity.

Keep also aware about the data colision. Set what provider will have preference when setting the data for your local account, in order to not turn your data wrong or expired. (Read somethig about "Level of assurance)

This is an old debate so first of implement something, start to read. There are many interesting documentation about this issues

Related to the specific problem in django. Read this comparation between the differents djago social-auth apps. Also check django-socialprofile and this old thread

于 2013-04-15T14:29:25.310 回答