autisid / oidc-client
OpenID Connect客户端
Requires
- php: >=8.0
- ext-json: *
- ext-mbstring: *
- cse/helpers-session: ^1
- lcobucci/clock: ^2
- lcobucci/jwt: ~4.1
- web-token/jwt-core: ^3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- phpunit/phpunit: ^9.5.10
- roave/security-advisories: dev-latest
- vlucas/phpdotenv: ^5
Suggests
- ext-bcmath: Faster cipher key operations. Please see https://web-token.spomky-labs.com/introduction/pre-requisite
- ext-gmp: Faster cipher key operations. Please see https://web-token.spomky-labs.com/introduction/pre-requisite
This package is auto-updated.
Last update: 2024-09-13 17:35:39 UTC
README
这是一个简单的库,允许应用程序通过基本的OpenID Connect流程进行用户身份验证。该库旨在通过使它足够简单,以便对OpenID Connect协议知之甚少的开发者也能设置身份验证,来鼓励OpenID Connect的使用。
此包是对JuliusPC/OpenID-Connect-PHP的完整重构。
支持的规范
- OpenID Connect Core 1.0
- OpenID Connect Discovery 1.0 (找不到颁发者)
- OpenID Connect RP-Initiated Logout 1.0 - 草案 01
- OpenID Connect Dynamic Client Registration 1.0
- RFC 6749: OAuth 2.0授权框架
- RFC 7009: OAuth 2.0令牌撤销
- RFC 7636: OAuth公共客户端通过代码交换的证明密钥
- RFC 7662: OAuth 2.0令牌内省
- 草案:授权响应中的OAuth 2.0授权服务器颁发者标识符
需求
- PHP 8.1+
- JSON扩展
- MBString扩展
- (可选) 之间GMP或BCMath扩展,允许更快的加密密钥操作(用于JWT;有关更多信息,请参阅此处)
安装
使用composer安装
composer require autisid/oidc-client-php
示例
示例1:基本客户端
此示例使用授权代码流,并且如果OpenID提供者在其发现文档中宣布它,还将使用PKCE。如果您不确定应该选择哪种流程:这是正确的方法。它是最安全和最通用的。
use Autisid\OpenIDConnect\Client; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') $oidc->authenticate(); $name = $oidc->getUserInfo()->given_name;
示例2:动态注册
use Autisid\OpenIDConnect\Client; $oidc = (new Client()) ->providerUrl('https://id.example.com') $oidc->register(); [$client_id, $client_secret] = $oidc->getClientCredentials(); // Be sure to add logic to store the client id and client secret
示例3:网络和安全
您应该始终为您的应用程序使用HTTPS。如果您使用的是自签名证书,可以通过在客户端上调用verifySsl
方法来禁用SSL验证,并设置自定义证书(如果有的话),使用certPath
方法(这仅在verifySsl设置为false时才有效)。
您还可以通过httpProxy
设置代理。
use Autisid\OpenIDConnect\Client; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') ->httpProxy('http://proxy.example.com:8080') ->certPath('path/to/cert.pem') ->verifySsl(false)
示例4:隐式流
参考:https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth
隐式流应被视为遗留流程,如果可以使用授权代码授予,则不应使用。由于其缺点和安全性差,隐式流将在即将到来的OAuth 2.1标准中过时。请参阅示例1以获取替代方案。
use Autisid\OpenIDConnect\Client; use Autisid\OpenIDConnect\ResponseType; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') ->responseType(ResponseType::ID_TOKEN) ->allowImplicitFlow(true) $oidc->authenticate(); $sub = $oidc->getUserInfo()->sub;
示例5:访问令牌的内省
use Autisid\OpenIDConnect\Client; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') $data = $oidc->introspectToken('an.access-token.as.given'); if (!$data->get('active')) { // the token is no longer usable }
示例6:PKCE客户端
PKCE已在示例1中的大多数场景中配置并使用。此示例向您展示如何在初始配置中显式设置代码挑战方法。这将在您的OpenID提供者在发现文档中未宣布支持它但确实支持的情况下启用PKCE。
use Autisid\OpenIDConnect\Client; use Autisid\OpenIDConnect\CodeChallengeMethod; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') // for some reason we want to set S256 explicitly as Code Challenge Method // maybe your OP doesn’t announce support for PKCE in its discovery document. ->codeChallengeMethod(CodeChallengeMethod::S256) $oidc->authenticate(); $name = $oidc->getUserInfo()->given_name;
示例7:令牌端点身份验证方法
默认情况下,客户端仅启用 client_secret_basic
,这是长时间以来唯一支持的选项。最近增加了 client_secret_jwt
和 private_key_jwt
,但它们默认是禁用的,直到明确启用。
use Autisid\OpenIDConnect\Client; use Autisid\OpenIDConnect\TokenEndpointAuthMethod; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') ->endpoints(options: [ 'token_endpoint_auth_methods_supported' => [ TokenEndpointAuthMethod::CLIENT_SECRET_BASIC, TokenEndpointAuthMethod::CLIENT_SECRET_JWT, TokenEndpointAuthMethod::PRIVATE_KEY_JWT, ], ]);
注意:此库中尚未包含 JWT 生成器。
开发环境
有时您可能需要在您的开发系统上禁用 SSL 安全性。您可以通过使用 false
参数调用 verify
方法来实现。注意:在生产系统上不推荐这样做。
use Autisid\OpenIDConnect\Client; $oidc = (new Client()) ->providerUrl('https://id.example.com') ->clientId('ClientIDHere') ->clientSecret('ClientSecretHere') ->verifySsl(false)
待办事项
- 动态注册不支持注册认证令牌和端点
贡献
- 所有合并的拉取请求都应该添加到 CHANGELOG.md 文件中。