wearesho-team/yii2-authorization

Redis 授权令牌用于 Yii2

2.2.0 2023-12-07 20:07 UTC

README

Test & Lint codecov

使用 Redis 令牌为 Yii2 授权用户

安装

composer require wearesho-team/yii2-authorization:^2.0

使用

令牌实体

要操作访问令牌和刷新令牌对,您应该使用 Token 实体。

<?php

use Wearesho\Yii2\Authorization;

$token = new Authorization\Token("accessValue", "refreshValue");
$token->getAccess(); // accessValue
$token->getRefresh(); // refreshValue

配置

对于配置,您必须使用 ConfigInterface。提供了一些开箱即用的实现

配置

简单的 Yii2 基础对象:Config

<?php

use Wearesho\Yii2\Authorization;

$config = new Authorization\Config([
    'expireInterval' => 'PT1M', // as \DateInterval value format
    'refreshExpireInterval' => 'PT1M', // same as expireInterval, used for refresh token
]);

$config = new Authorization\Config([
    'expireInterval' => new \DateInterval("PT1M"), // as \DateInterval instance,
    'refreshExpireInterval' => 'PT1M',
]);

$config = new Authorization\Config([
    'expireInterval' => function(): \DateInterval {
        return new \DateInterval("PT1M");
    }, // as \Closure that returns \DateInterval
    'refreshExpireInterval' => 'PT1M',
]);

环境配置

要使用环境配置授权,您应该使用 [EnvironmentConfig](./src / EnvironmentConfig . php)。
环境键(带有默认前缀)

  • **AUTHORIZATION_EXPIRE_INTERVAL ** -(默认: null),访问令牌过期前的秒数
  • **AUTHORIZATION_REFRESH_EXPIRE_INTERVAL ** -(默认: null),刷新令牌过期前的秒数
<?php

use Wearesho\Yii2\Authorization;

$config = new Authorization\EnvironmentConfig();
$config->getExpireInterval(0); // AUTHORIZATION_EXPIRE_INTERVAL will be loaded from environment
$config->getRefreshExpireInterval(0); // AUTHORIZATION_REFRESH_EXPIRE_INTERVAL will be loaded from environment

存储库

要存储令牌,您应该使用 Repository。它将在指定的 Redis 连接中存储令牌。

<?php

use yii\redis;
use Wearesho\Yii2\Authorization;
use Ramsey\Uuid\UuidFactoryInterface;

$repository = new Authorization\Repository([
    'config' => Authorization\ConfigInterface::class,
    'redis' => redis\Connection::class, // your connection
    'factory' => UuidFactoryInterface::class, // some implementation 
]);

$userId = 1;

// Creating new token pair
$token = $repository->create($userId); // Token entity

// Getting user ID using access token
$repository->get($token->getAccess()); // will return 1

// Removing token pair
$userId = $repository->delete($token->getRefresh());

// Then you can create new token pair (for refreshing)
$newToken = $repository->create($userId);

引导

要自动配置 ConfigInterface 定义,您应该使用 Bootstrap

<?php

// config.php

use Wearesho\Yii2\Authorization;

return [
    'bootstrap' => [
        'authorization' => [
            'class' => Authorization\Bootstrap::class,
            'config' => [
                'class' => Authorization\Config::class,
                'expireInterval' => 'PT30M', // 30 minutes
                'refreshExpireInterval' => 'PT90M', // 90 minutes
            ],
            // optional: you can configure refresh token storage
            'refreshTokenStorage' => [
                // default implementation, use your own if you want or choose one from list below
                'class' => Authorization\Repository\RefreshTokenStorageRedis::class,
            ],
        ],
    ],
];

对于刷新令牌存储配置,您需要传递 RefreshTokenStorage 实现的定义。

可用实现

HasToken

要实现 yii 的 web\Identity 接口的一部分,您应该使用 HasToken 特性,该特性实现了 findIdentityByAccessToken 方法,并允许使用类似 HttpBearerAuth 行为的东西。

<?php

use Wearesho\Yii2\Authorization;
use yii\web;

class User implements web\IdentityInterface
{
    use Authorization\HasToken;
    
    // then, implement other interface methods
}

许可

MIT