imarc/oauth2-openid-connect-server

一个基于 The PHP League 的 OAuth2 Server 的 OpenID Connect 服务器

3.0.0 2024-08-08 18:46 UTC

This package is auto-updated.

Last update: 2024-09-08 19:15:07 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

此库在 The PHP League 的 OAuth2 Server 上实现了 OpenID Connect 规范。

要求

注意:league/oauth2-server 的版本可能需要更高的 PHP 版本。

使用方法

以下类需要配置并传递给 AuthorizationServer,以提供 OpenID Connect 功能。

  1. IdentityRepository。此接口必须实现 OpenIDConnectServer\Repositories\IdentityProviderInterface,并基于 $accessToken->getUserIdentifier() 的返回值返回用户的身份。
    1. IdentityRepository 必须返回一个实现以下接口的 UserEntity
      1. OpenIDConnectServer\Entities\ClaimSetInterface
      2. League\OAuth2\Server\Entities\UserEntityInterface。
  2. ClaimSet。ClaimSet 是将声明与给定的范围相关联的一种方式。
  3. ClaimExtractor。ClaimExtractor 接受一个 ClaimSets 数组,并提供了 OpenID Connect 指定的范围(profile、email、phone 和 address)的默认声明。
  4. IdTokenResponse。此类必须在构造 AuthorizationServer 时传递,并负责将 id_token 添加到响应中。
  5. ScopeRepository。getScopeEntityByIdentifier($identifier) 方法必须返回一个 ScopeEntity,以支持 openid 范围。请参阅示例。

示例配置

// Init Repositories
$clientRepository       = new ClientRepository();
$scopeRepository        = new ScopeRepository();
$accessTokenRepository  = new AccessTokenRepository();
$authCodeRepository     = new AuthCodeRepository();
$refreshTokenRepository = new RefreshTokenRepository();

$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';

// OpenID Connect Response Type
$responseType = new IdTokenResponse(new IdentityRepository(), new ClaimExtractor());

// Setup the authorization server
$server = new \League\OAuth2\Server\AuthorizationServer(
    $clientRepository,
    $accessTokenRepository,
    $scopeRepository,
    $privateKey,
    $publicKey,
    $responseType
);

$grant = new \League\OAuth2\Server\Grant\AuthCodeGrant(
    $authCodeRepository,
    $refreshTokenRepository,
    new \DateInterval('PT10M') // authorization codes will expire after 10 minutes
);

$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month

// Enable the authentication code grant on the server
$server->enableGrantType(
    $grant,
    new \DateInterval('PT1H') // access tokens will expire after 1 hour
);

return $server;

服务器配置完成后,应按照 OAuth2 Server 文档 中的说明使用。

UserEntity

为了使此库正常工作,您需要将您的 IdentityProvider 添加到 IdTokenResponse 对象中。这将用于内部查找标识符的用户实体。此外,您的 UserEntity 必须实现 ClaimSetInterface,它包括一个名为 getClaims() 的方法。getClaims() 方法应返回一个作为键/值对的属性列表,如果已定义适当的范围,则可以返回。

use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\UserEntityInterface;
use OpenIDConnectServer\Entities\ClaimSetInterface;

class UserEntity implements UserEntityInterface, ClaimSetInterface
{
    use EntityTrait;

    protected $attributes;

    public function getClaims()
    {
        return $this->attributes;
    }
}

ClaimSets

ClaimSet 是一个定义声明列表的范围。

// Example of the profile ClaimSet
$claimSet = new ClaimSetEntity('profile', [
        'name',
        'family_name',
        'given_name',
        'middle_name',
        'nickname',
        'preferred_username',
        'profile',
        'picture',
        'website',
        'gender',
        'birthdate',
        'zoneinfo',
        'locale',
        'updated_at'
    ]);

如上图所示,profile 列出了一组可以从我们的 UserEntity 中提取的声明,如果授权请求中包含 profile 范围。

添加自定义 ClaimSets

在某个时刻,您可能希望包括自己的自定义声明组。为此,您需要创建一个 ClaimSetEntity,给它一个范围(您将在 OAuth2 请求的范围参数中包含的值)以及它支持的声明列表。

$extractor = new ClaimExtractor();
// Create your custom scope
$claimSet = new ClaimSetEntity('company', [
        'company_name',
        'company_phone',
        'company_address'
    ]);
// Add it to the ClaimExtract (this is what you pass to IdTokenResponse, see configuration above)
$extractor->addClaimSet($claimSet);

现在,当您使用请求传递公司范围时,它将尝试从 UserEntity::getClaims() 中定位这些属性。

安装

通过 Composer

$ composer require steverhoades/oauth2-openid-connect-server

测试

要运行单元测试,您需要从源中要求 league/oauth2-server,因为此存储库利用了一些现有的测试基础设施。

$ composer require league/oauth2-server --prefer-source

从根目录运行 PHPUnit

$ vendor/bin/phpunit

许可协议

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