将JWT访问令牌与bshaffer库

时间:2017-06-12 02:31:54

标签: php oauth oauth-2.0

我正在尝试实施此选项,但不起作用。

有人可以指导我如何实施它吗?

使用辅助存储

此库允许您将访问令牌备份到辅助存储。只需将实现OAuth2 \ Storage \ AccessTokenInterface的对象传递给JwtAccessToken对象,即可将访问权限存储在其他位置:

    $pdoStorage = new OAuth2\Storage\Pdo($pdo); 
    $keyStorage = new OAuth2\Storage\Memory(array('keys' => array(
            'public_key'  => $publicKey,
            'private_key' => $privateKey,
        )));
This example pulls the public/private keys from Memory storage, and saves the granted access tokens to Pdo storage once they are signed.

谢谢!

1 个答案:

答案 0 :(得分:0)

我猜你的实施将是User Credentials + JWT?如果不是这种情况,请说明您的具体用例,我会尽力帮助您。

我希望以下信息可以帮到你。对于每个部件,您可以设置单独的存储类型。您还可以在Server对象中看到其storageMap变量中的选项。有关多种存储类型的更多信息,请访问BShaffer - Using Multiple Storages

您需要将相应的存储类型设置为您的用例。如果您的用户存储在数据库中,请使用PDO存储。如果它们存储在内存中,请使用内存存储。

用户凭据最初使用access_tokens。这些是不包含任何数据的令牌。它们用于查找用户而不会一次又一次地传输敏感数据。要使用JWT令牌,您可以设置' use_jwt_access_tokens'关键是真的。您可以在示例中看到这一点。

JWT令牌通常不存储在数据库中(JWT的好处,因为令牌本身包含所需的用户信息)。因此,在示例中,我将access_token存储设置为PDO。如果您想使用access_tokens而不是JWT令牌,则需要将其存储在数据库中以便以后查找用户。

之后,我已经为我的用例添加了所需的授权类型。请记住,User Credentials授权类型也需要客户端凭据。您必须设置它们的位置。在示例中,我设置了内存存储。

如果您仍然不清楚,请随时提问!

// create storages
$pdoStorage = new \Apps\Source\Plugins\Oauth2\PDO([
    'dsn' => $dsn, // example: 'mysql:dbname=oauth2;host=localhost'
    'username' => $username, 
    'password' => $password,
]);
$memStorage = new \OAuth2\Storage\Memory([
    'keys' => array(
        'public_key'  => $publicKey,
        'private_key' => $privateKey,
    ),
    // client_credentials & client_secret are the key names, don't edit this. 
    'client_credentials' => array(
        'client_id_here' => array('client_secret' => 'secret_here')
    )
]);

// Set the required storage objects
$this->server = new \OAuth2\Server(
    [
        'access_token' => $memStorage, // Where you want to store your access tokens 
        'public_key' => $memStorage, // Where you have stored your keys
        'client_credentials' => $memStorage, // Depends on your keysclient_credentials storage location, mine is in memory, but can be stored in different storage types.
        'user_credentials' => $pdoStorage, // Depend on your where your users are being stored
        'refresh_token' => $pdoStorage // Refresh tokens are being stored in the db
    ],
    [
        'use_jwt_access_tokens' => true,
    ]
);

// Set the grant types
$grantType = new \OAuth2\GrantType\UserCredentials($pdoStorage);
$this->server->addGrantType($grantType);

$grantType = new \OAuth2\GrantType\RefreshToken($pdoStorage, [
    'always_issue_new_refresh_token' => true,
    'refresh_token_lifetime'         => 2419200 // the refresh tokens now last 28 days
]);
$this->server->addGrantType($grantType);
相关问题