出于某种原因,谷歌oauth2可以使用不同的Gmail电子邮件注册同一用户



有时当用户使用他的gmail帐户注册我的服务,然后他使用他的G Suite帐户注册时,两封电子邮件都会在UserSocialAuth模型中创建记录,但要创建相同的django用户。有人可以帮助我了解为什么会发生这种情况以及如何避免它吗?我需要两个Gmail帐户都有单独的django帐户。

我正在使用社交身份验证应用程序 django https://github.com/python-social-auth/social-app-django

我的管道

SOCIAL_AUTH_PIPELINE = [
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
# request consent if no refresh_token
'contrib.pipelines.redirect_if_no_refresh_token',
'social_core.pipeline.user.get_username',
# http://python-social-auth.readthedocs.io/en/latest/use_cases.html#associate-users-by-email
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
'contrib.pipelines.get_avatar',
# create default data for user
'contrib.pipelines.dummy_data.create',
]

这是它在我的数据库中的样子

In [7]: for uu in UserSocialAuth.objects.filter(user__email='me@mydomain.com').values():
...:     print(uu)
...:
{'user_id': 133, 'uid': 'me@mydomain.com', 'provider': 'google-oauth2', 'id': 125, 'extra_data': {'auth_time': 1523347209, 'access_token': '...', 'expires': 3600, 'token_type': 'Bearer', 'refresh_token': '...'}}
{'user_id': 133, 'uid': 'me@gmail.com', 'provider': 'google-oauth2', 'id': 401, 'extra_data': {'auth_time': 1522379769, 'access_token': '...', 'expires': 3598, 'token_type': 'Bearer'}}

问题似乎出在管道中的associate_by_email配置上。 删除该配置将为所有新的社交登录创建新用户。

根据文档:

如果用户使用他的Facebook帐户注册,然后注销并 下次尝试使用谷歌OAuth2登录,可能会很好(如果 两个社交网站都配置了相同的电子邮件地址( 用户进入由Facebook后端创建的初始帐户。

在此处阅读更多内容: http://python-social-auth.readthedocs.io/en/latest/use_cases.html#associate-users-by-email

从@omab自己那里得到答案 https://github.com/python-social-auth/social-core/issues/232

如果用户未从您的应用注销,然后继续使用第二个 GSuit 帐户登录,则新的社交帐户将与当前登录的用户相关联。如果要强制执行单独的帐户,则需要强制当前没有用户登录您的站点。

最新更新