ronvanderheijden / openid-connect
为PHP League的OAuth2 Server提供OpenID Connect支持。与Laravel Passport兼容。
Requires
- php: ^7.4|^8.0
- lcobucci/jwt: ^4.0
- league/oauth2-server: ^8.2.0
Requires (Dev)
- guzzlehttp/psr7: ^1.7
- http-interop/http-factory-guzzle: ^1.0
- league/oauth2-client: ^2.6
- overtrue/phplint: ^2.3
- phpunit/phpunit: ^9.4.2
- slevomat/coding-standard: ^6.4.1
- slim/slim: 4.*
- symplify/easy-coding-standard: ^9.2
README
为PHP League的OAuth2 Server提供OpenID Connect支持。
与 Laravel Passport 兼容!
要求
- 需要PHP版本
^7.4|^8.0
。 - lcobucci/jwt 版本
^4.0
。 - league/oauth2-server 版本
^8.2
。
安装
composer require ronvanderheijden/openid-connect
密钥
要签名和加密令牌,我们需要一对公钥和私钥。
mkdir -m 700 -p tmp openssl genrsa -out tmp/private.key 2048 openssl rsa -in tmp/private.key -pubout -out tmp/public.key chmod 600 tmp/private.key chmod 644 tmp/public.key
示例
我建议首先阅读这个。
要启用OpenID Connect,请按照以下简单步骤操作
$privateKeyPath = 'tmp/private.key'; $currentRequestService = new CurrentRequestService(); $currentRequestService->setRequest(ServerRequestFactory::fromGlobals()); // create the response_type $responseType = new IdTokenResponse( new IdentityRepository(), new ClaimExtractor(), Configuration::forSymmetricSigner( new Sha256(), InMemory::file($privateKeyPath), ), $currentRequestService, $encryptionKey, ); $server = new \League\OAuth2\Server\AuthorizationServer( $clientRepository, $accessTokenRepository, $scopeRepository, $privateKeyPath, $encryptionKey, // add the response_type $responseType, );
现在当调用 /authorize
端点时,提供 openid
范围以获取一个 id_token
。
提供更多范围(例如 openid profile email
)以在 id_token
中接收更多声明。
对于完整的实现,请访问 OAuth2 Server 示例。
一次性令牌支持
为了防止重放攻击,一些客户端可以在授权请求中提供一个“一次性令牌”。如果客户端这样做,服务器必须在 id_token
中返回一个 nonce
声明。
要启用此功能,当注册 AuthCodeGrant 时,您需要使用 \OpenIDConnect\Grant\AuthCodeGrant
而不是 \League\OAuth2\Server\Grant\AuthCodeGrant
。
![注意] 如果您正在使用Laravel,服务提供商已经为您注册了
AuthCodeGrant
。
Laravel Passport
您可以通过以下两个简单步骤使用此包与Laravel Passport一起使用。
1.) 添加服务提供商
# config/app.php 'providers' => [ /* * Package Service Providers... */ OpenIDConnect\Laravel\PassportServiceProvider::class, ],
2.) 创建实体
在 app/Entities/
中创建一个名为 IdentityEntity
或 UserEntity
的实体类。该实体用于收集声明。
# app/Entities/IdentityEntity.php namespace App\Entities; use League\OAuth2\Server\Entities\Traits\EntityTrait; use OpenIDConnect\Claims\Traits\WithClaims; use OpenIDConnect\Interfaces\IdentityEntityInterface; class IdentityEntity implements IdentityEntityInterface { use EntityTrait; use WithClaims; /** * The user to collect the additional information for */ protected User $user; /** * The identity repository creates this entity and provides the user id * @param mixed $identifier */ public function setIdentifier($identifier): void { $this->identifier = $identifier; $this->user = User::findOrFail($identifier); } /** * When building the id_token, this entity's claims are collected */ public function getClaims(): array { return [ 'email' => $this->user->email, ]; } }
发布配置
如果您想更改默认范围、添加自定义声明集或更改存储库,您可以使用以下命令发布OpenID配置:
php artisan vendor:publish --tag=openid
发现和JWKS
Laravel Passport集成还提供以下内容
- 在
/.well-known/openid-configuration
上的发现端点。 - 在
/oauth/jwks
上的JWKS端点。
这两个端点将自动添加到Laravel路由中,并且可以从配置中禁用(使用 openid.routes.discovery
和 openid.routes.jwks
键)。
Laravel Passport默认不提供 userinfo
端点。如果您提供一个,您可以通过将路由命名为 openid.userinfo
来将其添加到发现文档中。
Route::get('/oauth/userinfo', 'YourController@userinfo')->middleware('xxx')->name('openid.userinfo');
支持
发现了错误?有功能请求? 创建问题。
许可
OpenID Connect是开源的,并使用 MIT许可证 许可。