2017 © Pedro Peláez
 

yii2-extension yii2-access-router

Simple access control router for Yii2 Framework

image

yidas/yii2-access-router

Simple access control router for Yii2 Framework

  • Tuesday, June 5, 2018
  • by yidas
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

   , (*1)

Yii 2 Access Router


Yii 2 user authentication & authorization router, (*2)

Latest Stable Version Latest Unstable Version License, (*3)

FEATURES

  • Yii 2 User Authentication/Authorization for route level Integration, (*4)

  • RESTful API Authentication by Access Token support, (*5)

  • HTTP Request Login by Access Token support, (*6)

Access Router is a simple user access filtered on route level which supports authentication and authorization. Different from Yii2 Access Control Filter (ACF), this User Authorization can specify routes but not only in controller-actions level., (*7)


OUTLINE


REQUIREMENTS

This library requires the following:, (*8)

  • PHP 5.4.0+
  • Yii 2.0.0+

INSTALLATION

Install via Composer in your Yii2 project:, (*9)

composer require yidas/yii2-access-router

CONFIGURATION

Setup a Access Router component and then add it into bootstrap for your application configuration:, (*10)

return [
    'bootstrap' => ['log', 'access'],
    'components' => [
        'access' => [
            'class' => 'yidas\filters\AccessRouter',
            'except' => ['site/login', 'site/register'],
            'denyCallback' => function() {
                return Yii::$app->response->redirect(['/site/login']);
            },
        ],
        // ...
    ],
    // ...
];
  1. Create a component called access which uses yidas\filters\AccessRouter as class with configuration., (*11)

  2. Add this access component into bootstrap list., (*12)

Options

Key Type Default Description
except array ['*'] Excepted routes for identity verification check. ['{controller}/{action}', '{d}/{c}/{a}']
denyCallback callable null DenyCallback for HTTP authentication
httpAuth array HTTP authentication framework feature
httpLogin array HTTP request method login feature
exceptErrorAction boolean true Error action would be excepted through filter while turning on

USAGE

Except

Access Router implements Access Control Filter (ACF) for routes that the user is must in login status to pass through the filter from any routes except specified ones., (*13)

You can setup excepted routes that skip the user authorization. The except setting with [*] value means that the user authorization is disabled:, (*14)

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login'], //`site/login` is the login page which can not bypass user authorization
],

HTTP Authentication

Access Router supports automatically authenticating client's request by HTTP Authentication with bearer schemes (RFC 6750), you can enable it by setting up httpAuth configuration:, (*15)

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login', 'site/register'],
    'httpAuth' => [
        'enable' => true,
        'denyCallback' => function() {
            $response = Yii::$app->response;
            $response->statusCode = 401;
            $response->format = \yii\web\Response::FORMAT_JSON;
            $response->data = ['message' => 'Access Denied'];
            return $response->send();
        },
    ],
],

HTTP Authentication login will disable session for one time access uasge, which equals to \Yii::$app->user->enableSession = false;, (*16)

Options

Key Type Default Description
enable boolean false Enable HTTP authentication
denyCallback callable null DenyCallback for HTTP authentication
forced boolean true Force to authorize by HTTP authentication
key string 'AUTHORIZATION' The header key

Request Method Login

Access Router also supports automatically login client's request by HTTP GET/POST parameter by giving access token, you can enable it by setting up httpLogin configuration:, (*17)

'access' => [
    'class' => 'yidas\filters\AccessRouter',
    'except' => ['site/login', 'site/register'],
    'httpLogin' => [
        'enable' => true,
        'method' => 'post'
        'only' => ['site/login'],
        // 'key' => 'access_token',
    ],
],

For above configuration, you could login by accessing route site/login with correct access_token body value (Content-Type: application/x-www-form-urlencoded)., (*18)

Request Method Login is same as form login that the session is enable, and the duration time could be customized., (*19)

For GET method, If you setup 'method' => 'get' with 'only' => ['*'], then you can login by any routes with correct access_token parameter. For example: //example.com/?access_token={valid-user-access-token}, (*20)

For security reasons, it's not recommended to use GET method that passes access token in parameter., (*21)

Options

Key Type Default Description
enable boolean false Enable HTTP request method login
method string 'post' Parameter's Methods of get/post
only array ['*'] Allowed routes for login. ['{controller}/{action}', '{d}/{c}/{a}']
duration integer 3600 * 24 * 30 Seconds of login duration
key string 'access_token' Parameter's key
forced boolean true Force to authorize by HTTP authentication

POST Method without CSRF

If you uses post method and want to disable global CSRF validatiob, you can set enableCsrfValidation to false for request configuration:, (*22)

'components' => [
    'request' => [
        'csrfParam' => '_csrf-backend',
        'enableCsrfValidation' => false,
    ],

If you just want to disable CSRF for some controllers/actions, dynamically setting enableCsrfValidation for controller., (*23)


ADDITIONS

ACF for Global

If you want to use original Yii 2 Access Control Filter (ACF) for global route instead of Access Router's User Authorization, just comment out the except of Access Router and add ACF rules into 'as beforeRequest' in config:, (*24)

'bootstrap' => ['log', 'access'],
'components' => [
    'access' => [
        'class' => 'yidas\filters\AccessRouter',
        'except' => ['*'], // Equal to comment out
    ],
    // ...
],
'as beforeRequest' => [
    'class' => 'yii\filters\AccessControl',
    'rules' => [
        [
            'allow' => true,
            'actions' => ['login'],
        ],
        [
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
    'denyCallback' => function () {
        return Yii::$app->response->redirect(['site/login']);
    },
],

Warning: ACF could only defines actions but not routes, which the actions could be applied by every controllers., (*25)

For above setting example, login excepted action could be matched by any controller such as site/login, controller/login., (*26)


REFERENCE

Yii 2 - Application Structure > Application Events, (*27)

RFC7617 - The 'Basic' HTTP Authentication Scheme, (*28)

The Versions

05/06 2018

dev-master

9999999-dev

Simple access control router for Yii2 Framework

  Sources   Download

MIT

The Requires

 

framework yii2 access access router access contorl

05/06 2018

1.0.0

1.0.0.0

Simple access control router for Yii2 Framework

  Sources   Download

MIT

The Requires

 

framework yii2 access access router access contorl