【问题标题】:django social auth limiting user datadjango social auth 限制用户数据
【发布时间】:2013-09-11 12:46:43
【问题描述】:

我已将django social auth's 配置为仅从谷歌接收电子邮件,但谷歌显示此屏幕提醒应用用户性别、出生日期、图片、语言将被收集:

我的 django-social-auth 配置如下:

WHITE_LISTED_DOMAINS = [ 'some_domain', ]
GOOGLE_WHITE_LISTED_DOMAINS = WHITE_LISTED_DOMAINS
SOCIAL_AUTH_EXTRA_DATA = False    
#LOGIN_ERROR_URL    = '/login-error/' Not set
#SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user' Not set
#GOOGLE_CONSUMER_KEY          = '' Not set
#GOOGLE_CONSUMER_SECRET       = '' Not set
#GOOGLE_OAUTH2_CLIENT_ID      = '' Not set
#GOOGLE_OAUTH2_CLIENT_SECRET  = '' Not set
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = False
SOCIAL_AUTH_PROTECTED_USER_FIELDS = ['email',]

INSTALLED_APPS = (
    'django.contrib.auth',
     ...
    'social_auth',
)

我该如何避免这条谷歌消息?

已编辑

我已迁移到 GoogleOauth2 身份验证并继承和更改 google 后端:

from social_auth.backends.google import *

GOOGLE_OAUTH2_SCOPE = ['https://www.googleapis.com/auth/userinfo.email',]

class GoogleOAuth2(BaseOAuth2):
    """Google OAuth2 support"""
    AUTH_BACKEND = GoogleOAuth2Backend
    AUTHORIZATION_URL = 'https://accounts.google.com/o/oauth2/auth'
    ACCESS_TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'
    REVOKE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/revoke'
    REVOKE_TOKEN_METHOD = 'GET'
    SETTINGS_SECRET_NAME = 'GOOGLE_OAUTH2_CLIENT_SECRET'
    SCOPE_VAR_NAME = 'GOOGLE_OAUTH_EXTRA_SCOPE'
    DEFAULT_SCOPE = GOOGLE_OAUTH2_SCOPE
    REDIRECT_STATE = False

    print DEFAULT_SCOPE  #<------ to be sure

    def user_data(self, access_token, *args, **kwargs):
        """Return user data from Google API"""
        return googleapis_profile(GOOGLEAPIS_PROFILE, access_token)

    @classmethod
    def revoke_token_params(cls, token, uid):
        return {'token': token}

    @classmethod
    def revoke_token_headers(cls, token, uid):
        return {'Content-type': 'application/json'}

但谷歌仍然要求提供个人资料数据,个人资料仍在范围内:

https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&redirect_uri=...

如果我手动修改社交身份验证代码而不是继承,则运行良好:

def get_scope(self):
    return ['https://www.googleapis.com/auth/userinfo.email',]

我的代码有什么问题?

【问题讨论】:

  • 你在AUTHENTICATION_BACKENDS中定义了新的后端吗?
  • 是的,print DEFAULT_SCOPE outpùt 是新的范围,但是...我在您的 django-auth 代码中还有另一个打印,打印输出包括 userinfo.profile。
  • 如果在新类上覆盖get_scope() 会发生什么?同时从同一设置中删除另一个后端。
  • 也试过不成功。但从现在开始,这是一个发展问题。感谢您指向要覆盖的确切社交身份验证模块。我检查您的答案作为解决方案,我将测试最终方法,中间时间,如果您找到解决方案,请发布。感谢您的工作并分享这段宝贵的代码。

标签: django django-socialauth


【解决方案1】:

这是因为 google 后端使用的默认范围设置为(电子邮件和个人资料信息),它被定义为 here。为了避免这种情况,您可以创建自己的 google 后端来设置所需的范围,然后使用该后端而不是内置的后端。示例:

from social_auth.backends.google import GoogleOAuth2

class SimplerGoogleOAuth2(GoogleOAuth2):
    DEFAULT_SCOPE = ['https://www.googleapis.com/auth/userinfo.email']

【讨论】:

    【解决方案2】:

    不知道如何在 AUTHENTICATION_BACKENDS 中添加的人,如果使用 Omab 建议的方式,则需要在 setting.py 文件中添加新定义的后端:

    AUTHENTICATION_BACKENDS = (
        'app_name.file_name.class_name',  #ex: google_auth.views.SimplerGoogleOAuth2
        # 'social_core.backends.google.GoogleOAuth2', # comment this as no longer used
        'django.contrib.auth.backends.ModelBackend',
    )
    

    要了解如何创建 类 SimplerGoogleOAuth2,请查看 Omab 的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 2021-07-31
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多