如何从 Laravel Passport 中的刷新令牌中获取 id?

Posted

技术标签:

【中文标题】如何从 Laravel Passport 中的刷新令牌中获取 id?【英文标题】:How to get id from refresh token in Laravel Passport? 【发布时间】:2020-12-15 06:24:49 【问题描述】:

我正在编写一个控制器,我想在其中获取刷新令牌 ID,但我无法弄清楚如何在 laravel 中进行。

解密访问令牌没有问题。我正在使用标准协议并使用护照的公钥接收数据。

我尝试了不同的方法。

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class RefreshTokenController extends Controller

    public static $public_key;

    public static function refresh_token(Request $request)
    
        self::$public_key  = file_get_contents(storage_path() . DIRECTORY_SEPARATOR . 'oauth-public.key');
        $refresh_token = $request->get('refresh_token');
        $data = base64_decode($refresh_token);
        $public_key = openssl_get_publickey(self::$public_key);
        openssl_public_decrypt($data, $decrypted, $public_key);
        
        dd($decrypted);
    

我也尝试了标准decrypt();。但它也给出了一个错误。

传入数据示例。

def50200dc11ab81fdb16530806530f85fe41f7facc934447589b3a0daa202d56f5b4a8265db94b418e09280bcc8a05c88c3817612200d3c28de15c9adfd4a213f7cb136fca6f78099d686fce8d5ddf93f9bb29c1df081d7f7b75394875b5287a7ca5d9ca5eb513ea13a26195aeda41c897832467ec1b0348cdb3e5ed9476e5e7d8712b8628c18ee8ffb525de965faeb3f25b1a8eb4ee904a15341650fe6d3ed901600beda0f80c71b9b607f3e549ba8fbd9eda0e02fc7647ab8b4ea9161f7eb65833bfe46fb7f3e116a207282f947660ead392dc78ad2b9501331b4d28433a421a4193484e113baf8050d3878bd94b6ac860a1a766e79877f1af8d3ab5aee3779bf33c2a7e347552e51bb8b5448585435ef7b79d06853bcd775ce781b8d2e56289dbc8d4a10ec507aecde19bb496e90f88a687ee9bb33755046d50f860f7723d87bd4c079cd4d1242903bdb3ca447ce6ab03f2fd800182d0d8c5a3b7de5402ba2ecb4058d458cbee94da433635877e63120d6418e51eafdfd763191fceef399c16c088e27da9ffbf9

根据 oauth_refresh_tokens 表。保留所有刷新令牌 ID。调用refresh token方法时,根据refresh token,在数据库中匹配到对应的access token,并进行更新。

我将不胜感激。

【问题讨论】:

【参考方案1】:
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Defuse\Crypto\Crypto;
use Exception;
use Illuminate\Http\Request;

class RefreshTokenController extends Controller

    public static function refresh_token(Request $request)
    
        $refresh_token = $request->get('refresh_token');
        $app_key = env('APP_KEY');
        $enc_key = base64_decode(substr($app_key, 7));
        try 
            $crypto = Crypto::decryptWithPassword($refresh_token, $enc_key);
         catch (Exception $exception)
            return $exception;
        
        return  json_decode($crypto, true);
    

Laravel Passport 使用位于 ENV 文件中的 APP_ 加密密码。 使用库Defuse\Crypto\Crypto;

【讨论】:

以上是关于如何从 Laravel Passport 中的刷新令牌中获取 id?的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Passport 密码授予刷新令牌

我应该还是不应该在 Laravel Passport 中使用带有密码授予授权的刷新令牌?

如何卸载 Laravel Passport

从 Laravel Passport 的 oauth_access_tokens 表中的现有令牌创建访问令牌

在护照 laravel 中刷新令牌时出现 TokenMismatchException

Laravel API 身份验证(Passport),从 post application/json 获取用户 ID