iwink / php-openid-connect-client
Requires
- php: >=5.3.3
- laminas/laminas-eventmanager: >=2.2.1
- laminas/laminas-filter: >=2.2.1
- laminas/laminas-http: >=2.2.1
- laminas/laminas-json: >=2.2.1
- laminas/laminas-session: >=2.2.1
- laminas/laminas-stdlib: >=2.2.1
Requires (Dev)
- phpunit/phpunit: ~4.4
This package is not auto-updated.
Last update: 2021-02-23 09:52:40 UTC
README
该库的目的是提供工具和构建块,用于创建基于OAuth2协议的客户端,并侧重于OAuth2协议和OpenID Connect规范的委托认证/授权。
功能
- 灵活且可扩展
- 依赖注入方法
- 带有单元测试
- 基于Zend Framework 2的部分
兼容性
该库已成功与以下身份提供者进行了测试
要求
- Zend Framework >= 2.2.1 (在composer.json中解决)
安装
使用composer
将以下要求添加到您的composer.json
文件中
"require": {
"ivan-novakov/php-openid-connect-client": "dev-master"
}
不使用composer
只需克隆仓库或下载并解压缩最新版本,然后相应地配置自动加载器。
基本用法
您需要一个在身份提供者处注册的client_id
和client_secret
,并且您必须知道提供者端点的URL。
最常用的流程是
- 生成授权请求URL
- 将用户重定向到授权URL或让他点击“登录”按钮
- 处理回调请求并检索授权码
- 使用授权码发出令牌请求并检索访问令牌
- (可选) 使用访问令牌发出用户信息请求并检索有关用户的信息
该库引入了一个“flow”对象,该对象将上述操作集成到仅两次调用中
getAuthorizationRequestUri
- 生成用户授权的URL,然后由开发者决定如何将用户重定向到该URLprocess
- 一次性执行上述列表中的3、4和5项操作
简单示例
use InoOicClient\Flow\Basic;
$config = array(
'client_info' => array(
'client_id' => '<client ID>',
'redirect_uri' => '<redirect URI>',
'authorization_endpoint' => 'https://#/o/oauth2/auth',
'token_endpoint' => 'https://#/o/oauth2/token',
'user_info_endpoint' => 'https://www.googleapis.com/oauth2/v1/userinfo',
'authentication_info' => array(
'method' => 'client_secret_post',
'params' => array(
'client_secret' => '<client secret>'
)
)
)
);
$flow = new Basic($config);
if (! isset($_GET['redirect'])) {
try {
$uri = $flow->getAuthorizationRequestUri('openid email profile');
printf("<a href=\"%s\">Login</a>", $uri);
} catch (\Exception $e) {
printf("Exception during authorization URI creation: [%s] %s", get_class($e), $e->getMessage());
}
} else {
try {
$userInfo = $flow->process();
} catch (\Exception $e) {
printf("Exception during user authentication: [%s] %s", get_class($e), $e->getMessage());
}
}
分发器
“flow”对象只是一个外观。真正的“工作”是由所谓的“分发器”完成的
InoOicClient\Oic\Authorization\Dispatcher
- 生成授权请求URI并处理回调请求InoOicClient\Oic\Token\Dispatcher
- 发送令牌请求InoOicClient\Oic\UserInfo\Dispatcher
- 发送用户信息请求
HTTP客户端
该库使用带有cURL连接适配器的Zend Framework 2 HTTP客户端,这提供了关于安全HTTPS连接的最佳安全性。HTTP客户端通过工厂创建,默认配置为验证服务器证书。客户端还执行CN匹配验证。您可以在这篇文章中找到有关Zend Framework 2中安全HTTPS连接的更多信息。
然而,您可以选择注入自己的HTTP客户端实例,并对其进行不同的配置。
客户端认证
根据OpenID Connect规范(另请参阅OAuth2规范),该库支持以下客户端认证方法
client_secret_basic
- 客户端密钥通过Authorization
HTTP头发送client_secret_post
- 客户端密钥作为POST参数发送
状态持久化
规范建议在请求授权时使用state
参数。然后服务器有义务在回调中返回相同的值。这可能会防止跨站请求伪造攻击。
库自动处理状态
- 在创建授权URI期间生成一个不透明的状态值
- 将状态保存在用户会话中
- 检查从服务器发送的状态值与保存的值是否一致
默认情况下,生成的状态值保存在用户会话中(来自Zend Framework的会话容器)。您可以通过实现InoOicClient\Oic\Authorization\State\Storage\StorageInterface
来使用其他存储方式。
高级用法
如果您需要构建自定义流程或扩展/修改某些功能,您可以实现自己的流程对象(有关详细信息,请参阅InoOicClient\Flow\Basic
),或者可以直接使用分发器。然后,您可以根据您的用例构建和配置涉及的对象(分发器、请求、响应等)。
创建客户端信息对象
use InoOicClient\Client\ClientInfo;
$clientOptions = array(
'client_id' => '<client ID>',
'redirect_uri' => '<redirect URI>',
'authorization_endpoint' => 'https://#/o/oauth2/auth',
'token_endpoint' => 'https://#/o/oauth2/token',
'user_info_endpoint' => 'https://www.googleapis.com/oauth2/v1/userinfo',
'authentication_info' => array(
'method' => 'client_secret_post',
'params' => array(
'client_secret' => '<client secret>'
)
)
);
$clientInfo = new ClientInfo();
$clientInfo->fromArray($clientOptions);
准备授权请求URI
use InoOicClient\Oic\Authorization;
$stateManager = new Manager();
$dispatcher = new Authorization\Dispatcher();
$dispatcher->setStateManager($stateManager);
$request = new Authorization\Request($clientInfo, 'code', 'openid profile email');
$uri = $dispatcher->createAuthorizationRequestUri($request);
从回调中检索授权代码
$stateManager = new Manager();
$dispatcher = new Authorization\Dispatcher();
$dispatcher->setStateManager($stateManager);
$response = $dispatcher->getAuthorizationResponse();
printf("OK<br>Code: %s<br>State: %s<br>", $response->getCode(), $response->getState());
执行令牌请求
$httpClientFactory = new Http\ClientFactory();
$httpClient = $httpClientFactory->createHttpClient();
$tokenDispatcher = new Token\Dispatcher($httpClient);
$tokenRequest = new Token\Request();
$tokenRequest->setClientInfo($clientInfo);
$tokenRequest->setCode($authorizationCode);
$tokenRequest->setGrantType('authorization_code');
$tokenResponse = $tokenDispatcher->sendTokenRequest($tokenRequest);
printf("Access token: %s<br>", $tokenResponse->getAccessToken());
运行单元测试
请确保通过composer("require-dev")安装了phpunit,并在根目录中运行
$ ./vendor/bin/phpunit -c tests/
待办事项
- 为不同的提供者提供用户友好的演示
- 添加对JWT和ID令牌验证的支持
规范
OpenID Connect
OAuth2
提供者文档
许可
- BSD 3-Clause