【问题标题】:Using JWT Access Tokens with bshaffer library将 JWT 访问令牌与 bshaffer 库一起使用
【发布时间】:2017-11-13 09:50:41
【问题描述】:

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

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

使用辅助存储

此库允许您将访问令牌备份到辅助存储。只需将实现 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.

谢谢!

【问题讨论】:

    标签: php oauth oauth-2.0


    【解决方案1】:

    我猜您的实现将是用户凭据 + JWT?如果不是这种情况,请解释您的确切用例,我会尽力帮助您。

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

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

    用户凭据最初使用 access_tokens。这些是不包含任何数据的令牌。它们被用于查找用户,而无需一遍又一遍地传输敏感数据。要使用 JWT 令牌,您可以将“use_jwt_access_tokens”键设置为 true。您可以在示例中看到这一点。

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

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

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

    // 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);
    

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 1970-01-01
      • 2014-02-05
      • 2020-11-27
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多