steverhoades / oauth2-openid-connect-server
一个基于 The PHP League's OAuth2 Server 的 OpenID Connect 服务器
Requires
- lcobucci/jwt: 4.1.5|^4.2|^4.3|^5.0
- league/oauth2-server: ^5.1|^6.0|^7.0|^8.0
Requires (Dev)
- laminas/laminas-diactoros: ^1.3.2
- phpunit/phpunit: ^5.0|^9.5
This package is auto-updated.
Last update: 2024-09-08 18:22:54 UTC
README
此库在 The PHP League's OAuth2 Server 的基础上实现了 OpenID Connect 规范。
要求
- 需要 PHP 版本 5.5 或更高。
- league/oauth2-server 5.1 或更高。
注意:league/oauth2-server 版本的 PHP 要求可能更高。
使用方法
以下类需要配置并传递给 AuthorizationServer,以提供 OpenID Connect 功能。
- IdentityRepository。这个类必须实现 OpenIDConnectServer\Repositories\IdentityProviderInterface,并根据 $accessToken->getUserIdentifier() 的返回值返回用户的身份。
- IdentityRepository 必须返回一个实现以下接口的 UserEntity
- OpenIDConnectServer\Entities\ClaimSetInterface
- League\OAuth2\Server\Entities\UserEntityInterface.
- IdentityRepository 必须返回一个实现以下接口的 UserEntity
- ClaimSet。ClaimSet 是将声明与特定范围关联的一种方式。
- ClaimExtractor。ClaimExtractor 接受一个 ClaimSets 数组,并提供了 OpenID Connect 指定的默认声明,包括:profile、email、phone 和 address。
- IdTokenResponse。这个类必须在构建 AuthorizationServer 时传递,并负责将 id_token 添加到响应中。
- ScopeRepository。getScopeEntityByIdentifier($identifier) 方法必须返回一个用于
openid
范围的 ScopeEntity,以启用支持。请参阅示例。
示例配置
// Init Repositories $clientRepository = new ClientRepository(); $scopeRepository = new ScopeRepository(); $accessTokenRepository = new AccessTokenRepository(); $authCodeRepository = new AuthCodeRepository(); $refreshTokenRepository = new RefreshTokenRepository(); $privateKeyPath = 'file://' . __DIR__ . '/../private.key'; $publicKeyPath = 'file://' . __DIR__ . '/../public.key'; // OpenID Connect Response Type $responseType = new IdTokenResponse(new IdentityRepository(), new ClaimExtractor()); // Setup the authorization server $server = new \League\OAuth2\Server\AuthorizationServer( $clientRepository, $accessTokenRepository, $scopeRepository, $privateKey, $publicKey, $responseType ); $grant = new \League\OAuth2\Server\Grant\AuthCodeGrant( $authCodeRepository, $refreshTokenRepository, new \DateInterval('PT10M') // authorization codes will expire after 10 minutes ); $grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month // Enable the authentication code grant on the server $server->enableGrantType( $grant, new \DateInterval('PT1H') // access tokens will expire after 1 hour ); return $server;
配置服务器后,应按照 OAuth2 Server 文档 中所述使用它。
UserEntity
为了使此库正常工作,您需要将 IdentityProvider 添加到 IdTokenResponse 对象中。这将在内部用于通过其标识符查找 UserEntity。此外,您的 UserEntity 必须实现 ClaimSetInterface,它包含一个 getClaims() 方法。getClaims() 方法应返回一个键/值对列表,该列表可以是如果已定义适当的范围则返回的属性。
use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\UserEntityInterface;
use OpenIDConnectServer\Entities\ClaimSetInterface;
class UserEntity implements UserEntityInterface, ClaimSetInterface
{
use EntityTrait;
protected $attributes;
public function getClaims()
{
return $this->attributes;
}
}
ClaimSets
ClaimSet 是定义声明列表的范围。
// Example of the profile ClaimSet
$claimSet = new ClaimSetEntity('profile', [
'name',
'family_name',
'given_name',
'middle_name',
'nickname',
'preferred_username',
'profile',
'picture',
'website',
'gender',
'birthdate',
'zoneinfo',
'locale',
'updated_at'
]);
如上所示,profile 列出了如果将 profile 范围包含在授权请求中,则可以从我们的 UserEntity 中提取的声明集合。
添加自定义 ClaimSets
在某个时候,您可能希望包含自己的自定义声明组。为此,您需要创建一个 ClaimSetEntity,为其提供一个范围(您将在 OAuth2 请求的范围参数中包含的值)以及它支持的声明列表。
$extractor = new ClaimExtractor();
// Create your custom scope
$claimSet = new ClaimSetEntity('company', [
'company_name',
'company_phone',
'company_address'
]);
// Add it to the ClaimExtract (this is what you pass to IdTokenResponse, see configuration above)
$extractor->addClaimSet($claimSet);
现在,当您请求公司范围时,它将尝试从您的 UserEntity::getClaims() 中定位这些属性。
安装
通过 Composer
$ composer require steverhoades/oauth2-openid-connect-server
测试
要运行单元测试,您需要从源代码中需要 league/oauth2-server,因为此存储库使用了一些现有的测试基础设施。
$ composer require league/oauth2-server --prefer-source
从根目录运行 PHPUnit
$ vendor/bin/phpunit
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。