steverhoades/oauth2-openid-connect-server

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

v2.6.1 2023-09-08 16:15 UTC

README

Build Status Code Coverage Scrutinizer Code Quality

此库在 The PHP League's 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) 方法必须返回一个用于 openid 范围的 ScopeEntity,以启用支持。请参阅示例。

示例配置

// 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。此外,您的 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 列出了如果将 profile 范围包含在授权请求中,则可以从我们的 UserEntity 中提取的声明集合。

添加自定义 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)。有关更多信息,请参阅 许可证文件