b2pweb / parroauth2-client
B2P OAuth 2 客户端实现
Requires
- php: ~7.1 | ~8.0.0 | ~8.1.0 | ~8.2.0 | ~8.3.0
- ext-json: *
- b2pweb/jwt: ~1.0
- php-http/client-common: ~2.4
- php-http/discovery: ~1.14
- psr/http-client: ~1.0
- psr/http-factory: ~1.0
- psr/http-factory-implementation: ~1.0
- psr/http-message: ~1.0
- psr/http-message-implementation: ~1.0
- psr/simple-cache: ~1.0|~2.0|~3.0
- spomky-labs/base64url: ~2.0
- web-token/jwt-checker: ~1.3|~2.0|~3.0
- web-token/jwt-key-mgmt: ~1.3|~2.0|~3.0
- web-token/jwt-signature: ~1.3|~2.0|~3.0
- web-token/jwt-signature-algorithm-ecdsa: ~1.3|~2.0|~3.0
- web-token/jwt-signature-algorithm-eddsa: ~1.3|~2.0|~3.0
- web-token/jwt-signature-algorithm-hmac: ~1.3|~2.0|~3.0
- web-token/jwt-signature-algorithm-none: ~1.3|~2.0|~3.0
- web-token/jwt-signature-algorithm-rsa: ~1.3|~2.0|~3.0
Requires (Dev)
- bshaffer/oauth2-server-php: ~1.11
- cache/array-adapter: ~1.1
- nyholm/psr7: ~1.4
- php-http/curl-client: ~2.2
- php-http/mock-client: ~1.3
- phpunit/phpunit: ~7.0 | ~8.5
- squizlabs/php_codesniffer: ~3.6
- vimeo/psalm: ~4.9
README
PHP 的 OAuth 2.0 和 OpenID Connect 客户端库。
安装
使用 composer 安装
composer require b2pweb/parroauth2-client
简单用法
对于简单用法,使用 授权服务器元数据 RFC 8414 或 OpenID 连接发现,您可以在 示例 目录中查看。
密码认证
使用 密码 授权类型(参照:RFC 6749#4.3)对提供者进行认证。
此示例仅配置 OAuth 2.0 客户端,并使用所有者的凭据(即用户名和密码)调用提供者的令牌端点。
标准认证流程
使用 授权码 授权类型(参照:RFC 6749#4.1)实现客户端端认证,这是推荐的授权流程。
- 首先配置会话存储
- 然后加载提供者和客户端
- 注册扩展
JwtAccessToken
启用对访问令牌的本地 introspectionPkce
启用 PKCE RFC 7636 以减轻授权码截获攻击IdTokenValidator
(仅适用于 OpenID)以启用 ID Token 的验证TokenStorage
将访问令牌存储到会话中,并将其提供给 OAuth 端点RequiredScopeValidator
断言访问令牌中提供了给定的作用域。
- 如果令牌不存在或已过期,则通过使用
AuthorizationCodeFlow
执行认证过程 - 一旦认证成功,执行用户信息和 introspection
- 还实现了 注销 动作,使用撤销端点并将用户重定向到 OP 以停止会话
服务器端访问令牌检查
使用本地 introspection 检查作为 Authorization: Bearer 标头的传递的访问令牌。
高级用法
手动配置提供者
如果认证提供者未实现自动发现,或者您想手动配置,则可以使用 ProviderBuilder
$loader = new \Parroauth2\Client\Provider\ProviderLoader(); // Configure and create the provider $provider = $loader->builder('http://my-op.example.com') ->openid() // Enable openid connection on the endpoint // Configure endpoints ->tokenEndPoint('/token') ->authorizationEndPoint('/auth') ->introspectionEndPoint('/introspect') // Configure public key for local introspection ->addKeyFile('./keys/provider.pub') ->create() ; // Create the client $client = $provider->client((new \Parroauth2\Client\ClientConfig('client_id'))->setSecret('secret'));
延迟提供者
在某些情况下,您应该延迟加载提供者,并且仅在需要时才加载它。这在使用将客户端或提供者注入到服务中的依赖注入容器时是必要的。
在这种情况下,您可以使用 ProviderLoader::lazy()
,它允许仅在调用 OP 端点时加载提供者。
设计考虑
端点
端点是不可变的,对设置器的任何调用都将返回端点的新实例。
因此以下代码是无效的
/** @var $client \Parroauth2\Client\ClientInterface */ $token = $client->endPoints()->token(); $token->refresh('MyRefreshToken'); // This instruction has no effect : the return value is ignored $token->call(); // This call will fail : no token has been provided
要保存状态,如提供令牌,您应使用具有 EndPointTransformerInterface
的扩展,或在每个端点调用时手动注入参数。
扩展
扩展由一个具有单个方法 configure()
的类组成,该方法接受客户端作为参数。它们允许修改或配置客户端的任何可变元素,例如
- 更改客户端配置
- 注册或替换端点
- 注册
EndPointTransformerInterface
要简单地应用端点转换器,可以继承 AbstractEndPointTransformerExtension
,实现所需的端点转换方法,并使用 CallableEndPointInterface::onResponse()
来拦截响应。
注意:因为端点是不可变的,端点转换器必须返回配置的端点实例