决定何时使用Python Social Auth刷新OAUTH2令牌

Gio*_*lia 8 python django oauth-2.0 python-social-auth

我认为这主要是关于最佳实践的问题.

我有一个OAUTH2提供程序,只要刷新令牌就会发出访问令牌(有效期为10小时).

我在这里发现刷新访问令牌非常容易,但我无法理解如何确定何时刷新.

简单的答案可能是"当它不再起作用时",这意味着当我从后端获得HTTP 401时.这个解决方案的问题在于效率不高,而且我只能假设因为令牌已过期而得到401.

我的django应用程序我发现user social auth有一个Extra data字段包含这样的东西:

{ "scope": "read write", "expires": 36000, "refresh_token": "xxxxxxxxxxxxx", "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "token_type": "Bearer" }

但我不确定如何使用该expires字段.

所以我的问题是:我怎么知道访问令牌是否已经过期而我需要刷新它?

编辑:我刚刚发现此评论似乎相关,但我无法理解如何在管道中插入这个新功能,以便在令牌刷新期间工作.

Gio*_*lia 3

我最终明白了这一点。最初我感到困惑的原因是因为实际上有两种情况:

  1. 当用户登录时,基本上管道就会被执行。
  2. 当刷新令牌时调用用户社交身份验证方法refresh_token

解决第一种情况

我为管道创建了一个新函数:

def set_last_update(details, *args, **kwargs):  # pylint: disable=unused-argument
    """
    Pipeline function to add extra information about when the social auth
    profile has been updated.
    Args:
        details (dict): dictionary of informations about the user
    Returns:
        dict: updated details dictionary
    """
    details['updated_at'] = datetime.utcnow().timestamp()
    return details
Run Code Online (Sandbox Code Playgroud)

在设置中我将其添加到管道中之前load_extra_data

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    # the following custom pipeline func goes before load_extra_data
    'backends.pipeline_api.set_last_update',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'backends.pipeline_api.update_profile_from_edx',
    'backends.pipeline_api.update_from_linkedin',
)
Run Code Online (Sandbox Code Playgroud)

并且,仍然在设置中我在额外数据中添加了新字段。

SOCIAL_AUTH_EDXORG_EXTRA_DATA = ['updated_at']
Run Code Online (Sandbox Code Playgroud)

对于第二种情况:

我重写了refresh_token后端的方法来添加额外的字段。

def refresh_token(self, token, *args, **kwargs):
    """
    Overridden method to add extra info during refresh token.
    Args:
        token (str): valid refresh token
    Returns:
        dict of information about the user
    """
    response = super(EdxOrgOAuth2, self).refresh_token(token, *args, **kwargs)
    response['updated_at'] = datetime.utcnow().timestamp()
    return response
Run Code Online (Sandbox Code Playgroud)

仍然在后端类中,我添加了一个额外的字段来提取expires_in来自服务器的字段。

EXTRA_DATA = [
    ('refresh_token', 'refresh_token', True),
    ('expires_in', 'expires_in'),
    ('token_type', 'token_type', True),
    ('scope', 'scope'),
]
Run Code Online (Sandbox Code Playgroud)

此时,我有了创建访问令牌时的时间戳 ( updated_at) 以及它的有效秒数 ( expires_in)。

注意:这updated_at是一个近似值,因为它是在客户端而不是提供商服务器上创建的。

现在唯一缺少的是检查是否需要刷新访问令牌的函数。

def _send_refresh_request(user_social):
    """
    Private function that refresh an user access token
    """
    strategy = load_strategy()
    try:
        user_social.refresh_token(strategy)
    except HTTPError as exc:
        if exc.response.status_code in (400, 401,):
            raise InvalidCredentialStored(
                message='Received a {} status code from the OAUTH server'.format(
                    exc.response.status_code),
                http_status_code=exc.response.status_code
            )
        raise


def refresh_user_token(user_social):
    """
    Utility function to refresh the access token if is (almost) expired
    Args:
        user_social (UserSocialAuth): a user social auth instance
    """
    try:
        last_update = datetime.fromtimestamp(user_social.extra_data.get('updated_at'))
        expires_in = timedelta(seconds=user_social.extra_data.get('expires_in'))
    except TypeError:
        _send_refresh_request(user_social)
        return
    # small error margin of 5 minutes to be safe
    error_margin = timedelta(minutes=5)
    if datetime.utcnow() - last_update >= expires_in - error_margin:
        _send_refresh_request(user_social)
Run Code Online (Sandbox Code Playgroud)

我希望这对其他人有帮助。