john-peterson-g17 / oauth-token-management
一个与框架无关的抽象层,用于管理OAuth 2.0流程中的令牌/授权,如RFC 6749所述。此包将是您授权服务器中的一个模块,用于管理令牌
Requires
- php: >=8.2
- firebase/php-jwt: ^6.10
- predis/predis: ^2.2
- ramsey/uuid: ^4.7
Requires (Dev)
- larapack/dd: ^1.1
- phpunit/phpunit: ^10.5
- vlucas/phpdotenv: ^5.6
README
本项目旨在成为一个无偏见的与框架无关的模块,用于管理OAuth 2.0令牌,如RFC 6749所述。更具体地说,如果您正在创建授权服务器,则此包将是授权服务器中的模块,负责
- 颁发令牌
- 撤销令牌
- 刷新访问令牌
- 等等。
参考OAuth 2.0中定义的授权服务器,如RFC 6749第1.1节所述
概念
授权网关
这是您与该包交互的主要类。它负责颁发令牌(通过授权)、授权访问、刷新访问令牌等。将其视为OAuth2授权服务器中的核心模块。
授权
授权是一组颁发给客户端的令牌和一些元数据。它是成功认证和授予系统访问权限的声明。授权是不可变的。
您可以通过各种方法检索授权的数据。
$grant->userId(); // <-- The ID of the user that the grant was issued for (access was granted for) $grant->accessToken(); // <-- The access token associated with this grant $grant->refreshToken(); // <-- The refresh token associated with this grant $grant->expiresIn(); // <-- The number of seconds until the access token associated with this grant expires $grant->tokenType(); // <-- The token type for the grant. "Bearer" as stated in RFC 6750
目的是使用授权对象的从数据构建您的“成功”API响应,如RFC 6749第5.1节所示
参考
安装
要安装,只需通过composer命令要求包即可
composer require john-peterson-g17/oauth-token-management
设置
使用该包的第一步是设置授权网关(这是负责颁发、撤销、授权和刷新令牌等的模块)。这可以通过定义配置并使用该配置创建授权网关对象来完成
use JohnPetersonG17\OAuthTokenManagement\HashingAlgorithm; use JohnPetersonG17\OAuthTokenManagement\Persistance\Driver; use JohnPetersonG17\OAuthTokenManagement\Config; use JohnPetersonG17\OAuthTokenManagement\AuthorizationGate; // Expects an array of key value objects for configuring the authorization gate. Default values are provided if none are passed in via the array. $config = new Config( [ 'issuer' => 'https://myserver.com', 'key' => 'someSuperSecretKey1234', 'hashing_algorithm' => HashingAlgorithm::HS256, 'access_token_expiration' => 30, 'refresh_token_expiration' => 60, 'persistance_driver' => Driver::None ] ); $gate = new AuthorizationGate($config); // <-- Pass the configuration when creating the AuthorizationGate
如果传递了无效类型或配置选项的无效值,则将抛出 \InvalidArgumentException
。
$config = new Config( [ 'persistance_driver' => 'some_incorrect_driver_type' ] ); // <-- Throws \InvalidArgumentException
配置选项
授权网关有许多可用的配置选项。以下表格中给出了可用配置选项的列表。
信息:请记住,配置选项应作为使用表格中列出的键作为键值的数组提供
持久性
默认情况下,没有使用持久性驱动程序。这对于您想要处理令牌的持久性并希望包只处理令牌生成的情况非常有用。
警告:如果未设置持久性驱动程序,则授权网关的许多功能将抛出
\JohnPetersonG17\OAuthTokenManagement\Exceptions\PersistanceDriverNotSetException
。例如:如果令牌没有在任何地方持久化,则无法检索令牌。
如果要让包负责将令牌持久化到数据存储中,则可以设置以下可用的持久性驱动程序之一。
Redis持久性驱动程序
您可以通过在配置中设置redis持久性驱动程序来设置包使用redis来持久化您的令牌。然后,您可以传递一个额外的键 redis
,其中包含配置连接到redis服务器的选项数组。
在底层,使用predis客户端与redis进行连接/通信,因此传递给 redis
键的任何选项将直接传递给predis。因此,所有predis配置选项都受支持。
Predis 参考文档: https://github.com/predis/predis
use JohnPetersonG17\OAuthTokenManagement\Persistance\Driver; use JohnPetersonG17\OAuthTokenManagement\Config; $config = new Config( [ 'persistance_driver' => Driver::Redis, 'redis' => [ // <-- Any options supported by predis can be passed in this array to configure the underlying predis client 'parameters' => [ 'host' => $this->host, 'port' => $this->port, ] ] ] );
使用方法
配置并创建一个身份验证网关后,您就可以调用网关上的所有可用函数来创建和检查令牌。
警告:如果未设置持久性驱动程序,则授权网关的许多功能将抛出
\JohnPetersonG17\OAuthTokenManagement\Exceptions\PersistanceDriverNotSetException
。例如:如果令牌没有在任何地方持久化,则无法检索令牌。
授予用户访问权限(颁发授权)
使用此方法来授予用户访问和刷新令牌。
$userId = 1234; // ... Your application code authenticating the user $grant = $gate->grant($userId); // <-- Authentication successful so lets grant the user some tokens via this method // ... Your application code sharing the tokens/grant with the client (API Response, etc...)
令牌存储在Grant
对象中,该对象包含有关向用户授权的令牌和其他信息。
授权用户(验证用户的访问令牌)
使用此方法来验证用户的访问令牌是否有效,并且他们可以访问系统。
$accessToken = $grant->accessToken(); // <-- The user must have been issued a grant with an access token previously. $gate->authorize($accessToken); // <-- Authentication successful so lets grant the user some tokens via this method // ... Your application code now that the user is authorized access
如果访问令牌无效或已过期,则将抛出异常。
$accessToken = $grant->accessToken(); // <-- The user must have been issued a grant with an access token previously. try { $gate->authorize($accessToken); } catch (\JohnPetersonG17\OAuthTokenManagement\Exceptions\TokenExpiredException) { // ... Your application code informing the client that the access token has expired } catch (\JohnPetersonG17\OAuthTokenManagement\Exceptions\NotFoundException) { // ... Handle the case where the token does not exist or cannot be found }
撤销用户令牌(用户登出)
使用此方法来撤销用户的令牌/授权。通常,当用户明确从系统中登出时,会执行此操作。
$userId = 1234; // ... Your application code logging out the user $grant = $gate->revoke($userId);
刷新用户的访问令牌
使用此方法来刷新用户的访问令牌,这允许用户“保持登录”到您的系统,直到刷新令牌过期。
$refreshToken = $grant->refreshToken(); // <-- The user must have been issued a grant with an refresh token previously. $grant = $gate->refresh($refreshToken); // <-- A new grant is issued with a new access token. The refresh token is the same
检索用户的令牌
此方法在需要获取用户现有令牌/授权的情况下更为实用。
$userId = 1234; $grant = $gate->retrieve($userId); // <-- Throws a \JohnPetersonG17\OAuthTokenManagement\Exceptions\NotFoundException if a grant does not exist for the user
贡献
有关如何为此项目做出贡献的说明,请参阅我们的CONTRIBUTING文档。
欢迎所有人做出贡献!