PHP code example of dmstr / yii2-oauth-module

1. Go to this page and download the library: Download dmstr/yii2-oauth-module 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/ */

    

dmstr / yii2-oauth-module example snippets




use dmstr\oauth\Module as OAuthModule;
use dmstr\oauth\modules\admin\Module as OAuthAdminModule;

return [
    'modules' => [
        'oauth' => [
            'class' => OAuthModule::class,
            'tokenPrivateKey' => 'file:///path/to/private.key', // Path to private key file
            'tokenEncryptionKey' => 'your-secret', // optional. Only needed if you have a passphrase for your private key
            'accessTokenIssuer' => 'http://localhost:80', // Issuer of the access token.
            'userIdAttribute' => 'id', // The attribute of the user model that will be added to the access token as the `sub` claim.
            // This is optional but recommended. It will allow you to manage your clients in the admin interface.
            'modules' => [
                'admin' => [
                    'class' => OAuthAdminModule::class
                ]
            ]
        ]
    ],
    // This is only needed if your using codemix/yii2-localeurls (https://github.com/codemix/yii2-localeurls)
    'components' => [
        'urlManager' => [
            'ignoreLanguageUrlPatterns' => [
                '#^oauth/token#' => '#^oauth/token#'
            ]
        ],
        'rules' => [
            // This is only needed if you want to use the admin module. It will create an url alias to the user module
            'oauth/admin/user/index' => 'user/admin/index',
            'oauth/admin/user/view' => 'user/admin/update'
        ]
     
    ]
];

[
    'controllerMap' => [
        'migrate' => [
            'migrationPath' => [
                '@vendor/dmstr/yii2-oauth-module/src/migrations'
            ]
        ]
    ]
]



namespace app\api\controllers;

use Da\User\Model\User;
use bizley\jwt\JwtHttpBearerAuth;
use yii\filters\AccessControl;
use yii\rest\Controller;

class ItemsController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator']['authMethods'] = [
            [
                'class' => JwtHttpBearerAuth::class,
                // We used auth() here to keep the example simple. Implementing findIdentityByAccessToken() in your user model is recommended.
                'auth' => function (Plain $token) {
                    return User::findIdentity($token->claims()->get('sub'));
                }
            ]
        ];
        $behaviors['access'] = [
            'class' => AccessControl::class,
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['@'],
                    'actions' => ['index']
                ]
            ]
        ];
        return $behaviors;
    }

    /**
     * Example action. Replace with your own.
     */
    public function actionIndex(): array
    {
        return [
            [
                'id' => 1,
                'name' => 'Item 1'
            ]
        ];
    }
}