autisid / oidc-client

OpenID Connect客户端

1.0.2 2023-03-13 14:18 UTC

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的完整重构。

支持的规范

需求

  1. PHP 8.1+
  2. JSON扩展
  3. MBString扩展
  4. (可选) 之间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;

请参阅OpenID Connect规范以获取可用用户属性

示例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:访问令牌的内省

参考:https://tools.ietf.org/html/rfc7662

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_jwtprivate_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 文件中。