PHP code example of chervand / yii2-oauth2-server

1. Go to this page and download the library: Download chervand/yii2-oauth2-server 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/ */

    

chervand / yii2-oauth2-server example snippets


 
/** 
 * config/main.php 
 */
return [
    // ...
    'components' => [
        // ...
        'user' => [
            'identityClass' => 'app\components\Identity',
            // ...
        ],
        // ...
    ],
    // ...
];

 
/** 
 * config/main.php 
 */
return [
    // ...
    'bootstrap' => [
        'oauth2',
        // ...
    ],
    'modules' => [
        'oauth2' => [
            'class' => \chervand\yii2\oauth2\server\Module::class,
            'privateKey' => __DIR__ . '/../private.key',
            'publicKey' => __DIR__ . '/../public.key',
            'cache' => [
                \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface::class => [
                    'cacheDuration' => 3600,
                    'cacheDependency' => new \yii\caching\FileDependency(['fileName' => 'example.txt']),
                ],
                \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface::class => [
                    'cacheDuration' => 3600,
                    'cacheDependency' => new \yii\caching\FileDependency(['fileName' => 'example.txt']),
                ],
            ],
            'enableGrantTypes' => function (\chervand\yii2\oauth2\server\Module &$module) {
                $server = $module->authorizationServer;
                $server->enableGrantType(new \League\OAuth2\Server\Grant\ImplicitGrant(
                    new \DateInterval('PT1H')
                ));
                $server->enableGrantType(new \League\OAuth2\Server\Grant\PasswordGrant(
                    $module->userRepository,
                    $module->refreshTokenRepository
                ));
                $server->enableGrantType(new \League\OAuth2\Server\Grant\ClientCredentialsGrant());
                $server->enableGrantType(new \League\OAuth2\Server\Grant\RefreshTokenGrant(
                    $module->refreshTokenRepository
                ));
                $server->enableGrantType(new \chervand\yii2\oauth2\server\components\Grant\RevokeGrant(
                    $module->refreshTokenRepository,
                    $module->publicKey
                ));
            },
        ],
        // ...
    ],
    // ...
];



class ActiveController extends \yii\rest\ActiveController
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        unset($behaviors['authenticator']);
        unset($behaviors['rateLimiter']);

        /** @var \chervand\yii2\oauth2\server\Module $auth */
        $auth = \Yii::$app->getModule('oauth2');

        $behaviors['authenticator'] = [
            'class' => \yii\filters\auth\CompositeAuth::class,
            'authMethods' => [
                [
                    'class' => \chervand\yii2\oauth2\server\components\AuthMethods\HttpMacAuth::class,
                    'publicKey' => $auth->publicKey,
                    'cache' => $auth->cache,
                ],
                [
                    'class' => \chervand\yii2\oauth2\server\components\AuthMethods\HttpBearerAuth::class,
                    'publicKey' => $auth->publicKey,
                    'cache' => $auth->cache,
                ],
            ]
        ];

        $behaviors['rateLimiter'] = [
            'class' => \yii\filters\RateLimiter::class,
        ];

        return $behaviors;
    }

}