使用 Django rest-framework-jwt 撤销令牌

Revoking tokens using Django rest-framework-jwt

我正在考虑允许用户撤销之前颁发的令牌(是的,即使它们设置为在 15 分钟后过期),但没有找到任何方法来使用 DRF-jwt

目前,我正在考虑几个选项:

以上任何一种方法都可以吗?

我们在项目中是这样做的:

jwt_issue_dt 添加到用户模型。

original_iat 添加到负载。所以令牌刷新不会修改这个字段。

比较来自负载的 original_iatuser.jwt_issue_dt:

from calendar import timegm
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

class CustomJSONWebTokenAuthentication(JSONWebTokenAuthentication):

    def authenticate_credentials(self, payload):
        user = super(CustomJSONWebTokenAuthentication, self).authenticate_credentials(payload)
        iat_timestamp = timegm(user.jwt_issue_dt.utctimetuple())
        if iat_timestamp != payload['iat']:
            raise exceptions.AuthenticationFailed('Invalid payload')
        return user

要撤销令牌,您只需更新字段 user.jwt_issue_dt