imarc / oauth2-openid-connect-server
一个基于 The PHP League 的 OAuth2 Server 的 OpenID Connect 服务器
Requires
- lcobucci/jwt: ^5.3.0
- league/oauth2-server: ^8.4
Requires (Dev)
- laminas/laminas-diactoros: ^1.3.2
- phpunit/phpunit: ^5.0|^9.5
This package is auto-updated.
Last update: 2024-09-08 19:15:07 UTC
README
此库在 The PHP League 的 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) 方法必须返回一个 ScopeEntity,以支持
openid
范围。请参阅示例。
示例配置
// 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 必须实现 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 列出了一组可以从我们的 UserEntity 中提取的声明,如果授权请求中包含 profile 范围。
添加自定义 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)。有关更多信息,请参阅 许可文件。