kdoyen/openid-connect-php

基础 OpenID Connect 客户端

v0.1.1 2017-08-14 15:40 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:53:25 UTC


README

(此包是基于 rask/openid-connect-php 的分支。)

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

特别感谢 Justin Richer 和 Amanda Anganes 对协议的帮助和支持。

此包最初由 Michael Jett 创建,并由 Otto Rask 进行了大量修改。

要求

  1. PHP 5.4 或更高版本
  2. CURL 扩展
  3. JSON 扩展

安装

使用 composer 安装库

composer require kdoyen/openid-connect-php

然后包含 composer 自动加载器

<?php

require '/vendor/autoload.php';

示例 1: 基本客户端

<?php

use OpenIdConnectClient\OpenIdConnectClient;

$oidc = new OpenIDConnectClient([
        'provider_url' => 'https://id.provider.com/',
        'client_id' => 'ClientIDHere',
        'client_secret' => 'ClientSecretHere'
    ]);

$oidc->authenticate();
$name = $oidc->requestUserInfo('given_name');

请参阅 openid 规范以获取可用用户属性.

示例 2: 动态注册

<?php

use OpenIdConnectClient\OpenIdConnectClient;

$oidc = new OpenIDConnectClient([
        'provider_url' => 'https://id.provider.com/'
    ]);

$oidc->register();
$client_id = $oidc->getClientID();
$client_secret = $oidc->getClientSecret();

请确保在您的应用程序中添加逻辑以存储客户端 ID 和客户端密钥。

示例 3: 网络和安全

<?php

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

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

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

<?php

use OpenIdConnectClient\OpenIdConnectClient;

$oidc = new OpenIDConnectClient([
        'provider_url' => 'https://id.provider.com/',
        'client_id' => 'ClientIDHere',
        'client_secret' => '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: 令牌探查

<?php

use OpenIdConnectClient\OpenIdConnectClient;

$oidc = new OpenIDConnectClient([
        'provider_url' => 'https://id.provider.com/',
        'client_id' => 'ClientIDHere',
        'client_secret' => 'ClientSecretHere'
    ]);

// Provide access token to introspect.
// Can take an optional second parameter to set the token_type_hint.
$introspectionResponse = $oidc->introspectToken('provided_access_token');

// Check if the response/token is active and valid (based on exp and nbf).
$introspectionResponse->isActive();

// Get a list of allowed scopes.
$scopeArray = $introspectionResponse->getScopes();

// Simple boolean response if response has scope provided.
$introspectionResponse->hasScope('profile');

待办事项

  • 动态注册不支持注册身份验证令牌和端点。
  • 重构/替换 $_SESSION 使用。
  • 重构/完成测试覆盖率。

许可及作者信息

此包采用 Apache License 2.0 许可。