cloudcogsio/ oauth2-openid-connect-discovery

此库扩展了 'League OAuth2 Client' 库,为暴露 .well-known 配置端点的支持提供者提供 OpenID Connect Discovery 支持。

dev-master 2022-10-06 08:50 UTC

This package is auto-updated.

Last update: 2024-09-26 18:08:50 UTC


README

此库扩展了 League OAuth2 Client 库,为暴露 .well-known 配置端点的提供者提供 OpenID Connect Discovery 支持。

安装

在现有的(或新的)Oauth2 客户端提供者库中安装

  1. 使用 composer
composer require cloudcogsio/oauth2-openid-connect-discovery
  1. 将客户端更改为扩展 \Cloudcogs\OAuth2\Client\OpenIDConnect\AbstractOIDCProvider 而不是 \League\OAuth2\Client\Provider\AbstractProvider

  2. 删除以下方法

getResourceOwnerDetailsUrl getBaseAuthorizationUrl getBaseAccessTokenUrl

现有 OAuth2 客户端
class MyCustomClient extends \League\OAuth2\Client\Provider\AbstractProvider
{
    public function getResourceOwnerDetailsUrl(AccessToken $token)
    {
        ...
    }

    public function getBaseAuthorizationUrl()
    {
        ...
    }

    public function getBaseAccessTokenUrl(array $params)
    {
        ...
    }
}
具有 OpenID Connect Discovery 支持的更新 OAuth2 客户端
class MyCustomClient extends \Cloudcogs\OAuth2\Client\OpenIDConnect\AbstractOIDCProvider
{
    ...
}

现有客户端现在可以利用此库实现的 OIDC 机制。

有关实现新客户端提供者的更多信息,请参阅 https://oauth2-client.thephpleague.com/providers/implementing

使用方法

使用方法与 The League 的 OAuth 客户端相同。需要更改配置选项。

可以删除 URL 选项

'urlAuthorize' 'urlAccessToken' 'urlResourceOwnerDetails'

现有配置
$provider = new MyCustomClient([
   'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
   'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
   'redirectUri'             => 'https://my.example.com/your-redirect-url/',
   'urlAuthorize'            => 'https://service.example.com/authorize',
   'urlAccessToken'          => 'https://service.example.com/token',
   'urlResourceOwnerDetails' => 'https://service.example.com/resource'
]);
新配置
$provider = new MyCustomClient([
    'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
    'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
    'redirectUri'             => 'https://my.example.com/your-redirect-url/',
    'well_known_endpoint'     => 'https://identity.provider.com/.well-known/openid-configuration',
    'publickey_cache_provider'=> '',
]);
  • well_known_endpoint - IDP 的 .well-known/openid-configuration 端点的 URL。
  • publickey_cache_provider - 一个空字符串 OR 一个 \Laminas\Cache\Storage\Adapter\* 存储适配器实例。请参阅 https://github.com/laminas/laminas-cache

其他注意事项和使用方法

您的客户端提供者实例现在将添加一些功能,例如令牌检查(如果您的 IDP 支持)以及从提供者获取更多配置详情的能力。

配置数据通过代理从客户端提供者的 Discovery 对象访问。

// Get the discovered configurations from the provider instance
$discovered = $provider->Discovery();

// Access standard OpenID Connect configuration via supported methods
$issuer = $discovered->getIssuer();
$supported_grants = $discovered->getGrantTypesSupported();
$authorization_endpoint = $discovered->getAuthorizationEndpoint();

// Or overloading for provider specific configuration
$custom_config = $discovered->custom_config;

// Cast to string to obtain the raw JSON discovery response
// All available properties for overloading can be seen in the JSON object.
$json_string = (string) $discovered;

IDP 公钥

在端点发现期间,IDP 的公钥将被检索并本地缓存。这是解码访问令牌(如果需要)所必需的。

公钥缓存

JWK 的缓存由一个 \Laminas\Cache\Storage\Adapter\* 存储适配器实例处理。如果没有提供,则使用 \Laminas\Cache\Storage\Adapter\FileSystem

您可以为公钥存储提供自己的 \Laminas\Cache\Storage\Adapter\* 实例。

示例
$storageAdapter = new \Laminas\Cache\Storage\Adapter\MongoDB($mdbOptions);

$provider = new MyCustomClient([
    'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
    'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
    'redirectUri'             => 'https://my.example.com/your-redirect-url/',
    'well_known_endpoint'     => 'https://identity.provider.com/.well-known/openid-configuration',
    'publickey_cache_provider'=> $storageAdapter,
]);

令牌检查

IDP 发行的 AccessToken 可以在本地解码以获取更多信息。

// Decode the access token
$access_token = $AccessToken->getToken();
$data = $provider->introspectToken($access_token);

通过 IDP 进行令牌检查(可选)

如果 IDP 提供了令牌检查端点,则可以使用令牌检查端点检查 IDP 发行的所有令牌(accessToken、refreshToken 等)。

// Decode the refresh token
$refresh_token = $AccessToken->getRefreshToken();
$data = $provider->introspectToken($refresh_token);

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件