PHP code example of wearesho-team / yii2-authorization

1. Go to this page and download the library: Download wearesho-team/yii2-authorization library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wearesho-team / yii2-authorization example snippets




use Wearesho\Yii2\Authorization;

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



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',
]);



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




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);



// 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,
            ],
        ],
    ],
];




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

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