我猜你的實現將是User Credentials + JWT?如果情況並非如此,請解釋您的確切用例,我會盡力幫助您。
我希望以下信息能幫助你。對於每個部分,您可以設置單獨的存儲類型。您還可以在Server對象的storageMap變量中看到選項。有關多種存儲類型的更多信息,請參見BShaffer - Using Multiple Storages。
您需要將相應的存儲類型設置爲您的用例。如果您的用戶正在存儲在數據庫中,請使用PDO存儲。如果它們存儲在內存中,則使用內存存儲。
用戶憑證最初使用access_tokens。這些是不包含任何數據的標記。它們被用來查找用戶而不用一遍又一遍地傳輸敏感數據。要使用JWT標記,您可以將'use_jwt_access_tokens'鍵設置爲true。你可以在例子中看到這個。
JWT令牌通常不存儲在數據庫中(JWT的好處是因爲令牌本身包含所需的用戶信息)。所以在這個例子中,我已經爲PDO設置了access_token存儲。如果您想使用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);