wannabe-pro/openid-connect

基础 OpenID Connect 客户端

0.0.4 2019-11-07 08:11 UTC

This package is auto-updated.

Last update: 2024-09-07 19:30:25 UTC


README

这是 jumbojett/openid-connect-php 的分支

PHP OpenID Connect 基本客户端

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

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

要求

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

安装

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

示例 1: 基本客户端

use WannaBaPro\OpenIDConnect;

$oidc = new CLient('https://id.provider.com',
                                'ClientIDHere',
                                'ClientSecretHere');
$oidc->setCertPath('/path/to/my.cert');
$oidc->authenticate();
$name = $oidc->requestUserInfo('given_name');

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

示例 2: 动态注册

use WannaBaPro\OpenIDConnect;

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

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

// 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 WannaBaPro\OpenIDConnect;

$oidc = new Client('https://id.provider.com',
                                'ClientIDHere',
                                'ClientSecretHere');
$oidc->providerConfigParam(array('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 WannaBaPro\OpenIDConnect;

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

//Add username and password
$oidc->addAuthParam(array('username'=>'<Username>'));
$oidc->addAuthParam(array('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(请参阅 http://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth

use WannaBaPro\OpenIDConnect;

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

示例 7: 访问令牌的 introspection(请参阅 https://tools.ietf.org/html/rfc7662

use WannaBaPro\OpenIDConnect;

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

开发环境

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

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

待办事项

  • 动态注册不支持注册认证令牌和端点

贡献

  • 所有合并后的拉取请求都应添加到 changelog.md 文件中。