【问题标题】:How can I return the refresh token using python-social-auth and django-graphql-auth?如何使用 python-social-auth 和 django-graphql-auth 返回刷新令牌?
【发布时间】:2021-12-29 22:23:50
【问题描述】:

使用社交身份验证时如何返回刷新令牌?我试过这个解决方案here我不知道如何使它与graphql兼容。

这是我的尝试:

import graphql_social_auth
from graphql_jwt.shortcuts import get_token


class SocialAuth(graphql_social_auth.SocialAuthJWT):
    refresh_token = graphene.String()
    token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        return cls(
            user=social.user,
            token=get_token(social.user),
            refresh_token=social.extra_data['refresh_token']
        )

当我注释掉刷新令牌时,用户和令牌字段都很好。我只是不知道从哪里获得刷新令牌,它似乎不在extra_data中。

我已经处理这个问题几个小时了,所有的搜索结果都没有帮助。请,非常感谢任何指导。

【问题讨论】:

    标签: python django graphql graphene-django


    【解决方案1】:

    扩展SocialAuthJWT 时不需要再次声明token 字段,因为它已经有来自JSONWebTokenMixin mixin 的token 字段,除非您需要用不同的类型覆盖它。

    至于刷新令牌,你可以这样做来获取它:

    import graphql_social_auth
    from graphql_jwt.shortcuts import get_token
    from graphql_jwt.shortcuts import create_refresh_token
    
    
    class SocialAuth(graphql_social_auth.SocialAuthJWT):
        refresh_token = graphene.String()
    
        @classmethod
        def resolve(cls, root, info, social, **kwargs):
            if social.user.refresh_tokens.count() >= 1:
                refresh_token = social.user.refresh_tokens.last()
            else:
                refresh_token = create_refresh_token(social.user)
    
            return cls(
                user=social.user,
                token=get_token(social.user),
                refresh_token=refresh_token
            )
    

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 2018-07-18
      • 2017-05-18
      • 2016-09-04
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      相关资源
      最近更新 更多