jakub-onderka/openid-connect-php

简约的OpenID Connect客户端

v1.1.0 2022-10-19 07:21 UTC

README

Latest Stable Version Latest Unstable Version PHP Version Require

这是一个简单的库,允许应用程序通过基本的OpenID Connect流程进行用户身份验证。这个库希望通过使其足够简单,让对OpenID Connect协议了解不多的开发者也能轻松设置身份验证,从而鼓励使用OpenID Connect。

这是jumbojett/OpenID-Connect-PHP的分支

Jumbojett的库非常棒,但缺少一些功能,测试不够完善,并且尚未准备好支持新的PHP版本。因此,我创建了此分支。此分支需要PHP 7.0或更高版本;如果您需要使用旧版本的PHP,请使用原始版本。

最重要的变更

  • 添加了对椭圆曲线(EC)JWT令牌签名算法的支持,这些算法比RSA签名更快
  • 添加了对client_secret_jwtprivate_key_jwt身份验证方法的支持,这些方法比传统方法更安全
  • JWT ID Token验证符合OpenID Connect标准
  • 单元测试覆盖率大大提高
  • 许多小的优化和修复

特别感谢Michael Jett,该库的原始作者,以及Justin Richer和Amanda Anganes对协议的帮助和支持。

要求

  1. PHP 7.0或更高版本
  2. CURL扩展
  3. JSON扩展
  4. APCu用于缓存(可选)

安装

  1. 使用composer安装库
composer require jakub-onderka/openid-connect-php
  1. 包含composer自动加载器
require __DIR__ . '/vendor/autoload.php';

示例 1: 基本客户端

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere', 'ClientSecretHere');
$oidc->authenticate();
$name = $oidc->requestUserInfo('given_name');

查看openid规范以获取可用的用户属性

示例 2: 动态注册

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient("https://id.provider.com");

$response = $oidc->register("Client Name");
$clientID = $response->client_id;
$clientSecret = $response->client_secret;

// Be sure to add logic to store the client id and client secret

示例 3: 网络和安全

// Configure a proxy
$oidc->setHttpProxy("http://my.proxy.com:80/");

// Configure a cert
$oidc->setCertPath("/path/to/my.cert");

示例 4: 请求客户端凭证令牌

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere', 'ClientSecretHere');
$oidc->providerConfigParam(['token_endpoint' => 'https://id.provider.com/connect/token']);
$oidc->addScope('my_scope');

// This assumes success (to validate check if the access_token property is there and a valid JWT):
$clientCredentialsToken = $oidc->requestClientCredentialsToken()->access_token;

示例 5: 请求资源所有者令牌(带有客户端认证)

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere','ClientSecretHere');
$oidc->providerConfigParam(['token_endpoint' => 'https://id.provider.com/connect/token']);
$oidc->addScope('my_scope');

// Add username and password
$oidc->addAuthParam([
  'username' => '<Username>',
  'password' => '<Password>',
]);

// Perform the auth and return the token (to validate check if the access_token property is there and a valid JWT):
$token = $oidc->requestResourceOwnerToken(true)->access_token;

示例 6: 基本客户端,适用于隐式流程,例如Azure AD B2C

查看https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere', 'ClientSecretHere');
$oidc->setResponseTypes(['id_token']);
$oidc->addScope(['openid']);
$oidc->setAllowImplicitFlow(true);
$oidc->addAuthParam(['response_mode' => 'form_post']);
$oidc->setCertPath('/path/to/my.cert');
$oidc->authenticate();
$sub = $oidc->getVerifiedClaims('sub');

示例 7: 访问令牌的检查

查看https://tools.ietf.org/html/rfc7662

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere', 'ClientSecretHere');
$data = $oidc->introspectToken('an.access-token.as.given');
if (!$data->active) {
    // the token is no longer usable
}

示例 8: PKCE客户端

use JakubOnderka\OpenIDConnectClient;

$oidc = new OpenIDConnectClient('https://id.provider.com', 'ClientIDHere');
$oidc->setCodeChallengeMethod('S256');
$oidc->authenticate();
$name = $oidc->requestUserInfo('given_name');

开发环境

在某些情况下,您可能需要在开发系统中禁用SSL安全。注意:在生产系统中不推荐这样做。

$oidc->setVerifyHost(false);
$oidc->setVerifyPeer(false);

此外,您的本地系统可能不支持HTTPS,因此您可能需要禁用升级到HTTPS

$oidc->httpUpgradeInsecureRequests(false);

待办事项

  • 动态注册不支持注册身份验证令牌和端点

贡献

  • 所有合并的pull请求应添加到CHANGELOG.md文件中。