Yii2 Rest Api 用户承载认证过期时间

Yii2 Rest Api User bearer Authentication expiration time

我目前正在研究基于 yii2 的 Rest api。我为用户 authentication.let 使用不记名令牌,我解释一下这个要求。

1) 第一个用户使用其凭据从外部 php 应用程序进行身份验证。

2)he/she 获得了访问令牌。

3) 每个后续请求都是使用此访问令牌发出的。

public static function findIdentityByAccessToken($token, $type = null)
{
    return static::findOne(['auth_key' => $token]);
}

这是我开始思考的地方。我没有发现访问令牌的任何到期时间。真的需要吗?如果是,我该如何存档?提前致谢。

您的问题有点宽泛,但我会尝试帮助您理解思路。

i do not found any expiration time for the access token. is that really needed?

这取决于您的要求。您是否希望您的用户在首次验证后能够无限期地访问您的 API?您是否希望您的用户经常更新他们的令牌?

我会推荐后者,因为它限制了潜在攻击者使用受损访问令牌的时间。

if yes how can i archive that?

一个选项是将包含到期日期的日期时间的字段添加到数据库 table 对应于您的身份 class 并检查这在 class 的实施中是否仍然有效=11=]

public static function findIdentityByAccessToken($token, $type = null)
{
    return static::findOne([
        'AND', 
        ['auth_key' => $token], 
        ['>=', 'token_expire', new \yii\db\Expression('NOW()')]
    ]);
}