Authorizing users using redis tokens for Yii2
composer require wearesho-team/yii2-authorization:^2.0To operate with access and refresh token pair you should use Token entity.
<?php
use Wearesho\Yii2\Authorization;
$token = new Authorization\Token("accessValue", "refreshValue");
$token->getAccess(); // accessValue
$token->getRefresh(); // refreshValueFor configuration you have to use ConfigInterface. Few implementations available out-of-box:
Simple Yii2 base object: 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',
]);To use environment to configure authorization you should use [EnvironmentConfig](./src / EnvironmentConfig . php).
Environment keys(with default prefix):
- **AUTHORIZATION_EXPIRE_INTERVAL ** -(default: null), seconds before access token will be expired
- **AUTHORIZATION_REFRESH_EXPIRE_INTERVAL ** -(default: null), seconds before refresh token will be expired
<?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 environmentTo store tokens you should use Repository. It will store tokens in specified redis connection.
<?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);To automatically configure ConfigInterface definition you should use 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,
],
],
],
];For refresh token storage configuration you need to pass definition of RefreshTokenStorage implementation.
Available implementation:
- wearesho-team/yii2-authorization-refresh-storage-db for storing refresh token using SQL database.
To implement part of yii`s web\Identity interface you should use HasToken trait, which implement findIdentityByAccessToken method and will allow to use something like HttpBearerAuth behaviors.
<?php
use Wearesho\Yii2\Authorization;
use yii\web;
class User implements web\IdentityInterface
{
use Authorization\HasToken;
// then, implement other interface methods
}