Basic installation - Patroklo/yii2-oauth2-server GitHub Wiki

1. Module installation

The preferred and officially supported way of installing this extension is through composer. First install Composer asset plugin with command

composer global require "fxp/composer-asset-plugin:~1.0.3"

Then either run

composer require --prefer-dist filsh/yii2-oauth2-server "*"

or add

"filsh/yii2-oauth2-server": "*"

Into the require section of your composer.json file.

2. Configuration in Yii2 App

2.1. Module configuration

To be able to use this extension, you simply have to add the following code in your application configuration:

    'modules' => [
           'oauth2' => [
                'class' => 'filsh\yii2\oauth2server\Module',
           ],
            ...
       ]

This is the minimum code necessary to execute the OAuth2 server; it will use the default configuration for every Token grant type, storage and access permission.

2.2. Token URL configuration

Since the URL to obtain a Token can only be accessed via POST request (the reason for this will be explained later on), it's considered a good practice to define manually a rule in Yii2 Url Manager to filter all the incoming calls to only accept Post requests.

This is an example of how this can be achieved. This excerpt of code can change depending of your server and urlManager configuration.

    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => TRUE,
            'enableStrictParsing' => TRUE,
            'showScriptName' => FALSE,
            'rules' => [
                'POST oauth2/<action:\w+>' => 'oauth2/default/<action>'
            ]
        ],
        ....
    ]

Config file locations:

In both cases, you'll have to tweak with the configuration files of your Yii2 App installation:

  • If you have a Basic Yii2 App installation the configuration file should be stored at config/web.php
  • If you have an Advanced Yii2 App installation, the configuration file will depend of which tier you want to adapt as an API server, but should be stored in (frontend|backend|common)/config/main.php file.

3. Database configuration

From console mode enter into the main directory of your Yii2 installation and write down:

./yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations

This migration will create a generic OAuth2 database scheme that will be used by the module default stores. It will also insert one test Client credentials testclient:testpass for http://fake/ url redirection.

⚠️ **GitHub.com Fallback** ⚠️