【问题标题】:django-ouath-toolkit with social-authdjango-oauth-toolkit with social-auth
【发布时间】:2017-10-23 23:23:03
【问题描述】:

我正在尝试设置 social-auth (python-social-auth) 以使用 django-oauth-toolkit 与另一个 Django 项目通信。

我希望 Django 项目 A 使用来自 Django 项目 B 的 OAuth2 登录。Django 项目 A 使用 social-auth 而项目 B 使用 django-oauth-toolkit 作为 OAuth2 提供者。

项目 A 可以成功登录到其他 OAuth2 提供程序,例如 Google,但在尝试使用我的自定义后端登录时出现以下错误:

AuthStateMissing at /sso/complete/custom-backend/
Session value state missing.

这就是在项目 A 中实现自定义后端的方式:

class CustomBackend(BaseOAuth2):
    name = 'custom-backend'
    EXTRA_DATA = [
        ('profile', 'org')
    ]

    def authorization_url(self):
        return "%s/oauth2/authorize" % self.setting("URL")

    def access_token_url(self):
        return "%s/oauth2/token" % self.setting("URL")

    def user_data(self, access_token, *args, **kwargs):
        resp = requests.get("%s/me/" % self.setting("URL"), headers={
            'Authorization': "Bearer %s" % access_token
        })
        if resp.status_code != 200:
            raise Exception("Error while getting user details from auth source.")
        data = resp.json()

        return {
            'username': data['email'],
            'first_name': data['first_name'],
            'last_name': data['last_name'],
            'user_id': data['id'],
            'email': data['email']
        }

有一个名为SOCIAL_AUTH_CUSTOM_BACKEND_URL 的设置等于项目B 的基本URL(http://localhost:8001 用于测试)。

项目B重定向到项目A时(报错时)的URL为:http://localhost:8000/sso/complete/custom-backend/?redirect_state=6kg4S1eitCMqTTzFGrm9uerG37UNkUPl&code=e64by72AkD2unMVVsGZCz0V2byuUyu&state=6kg4S1eitCMqTTzFGrm9uerG37UNkUPl

不胜感激,谢谢。

【问题讨论】:

    标签: python django oauth django-authentication


    【解决方案1】:

    我发现,HTTP cookie 不是由同一主机上的端口分隔。所以我让项目 A(OAuth2 客户端)在localhost:8000 上运行,项目 B(OAuth2 服务器)在localhost:8001 上运行。 Django 的sessionid Cookie 在认证过程中被覆盖,导致了这个错误。

    解决方案是在不同的主机上运行 OAuth2 服务器(我刚刚在 macOS 上的 /private/etc/hosts 中做了一个条目,如下所示:

    127.0.0.1    id.localhost
    

    并将SOCIAL_AUTH_CUSTOM_BACKEND_URL 更新为http://id.localhost:8001

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多