2017 © Pedro Peláez
 

yii2-extension oauth2

The Oauth2 Server extension for the Yii2 framework

image

conquer/oauth2

The Oauth2 Server extension for the Yii2 framework

  • Friday, April 20, 2018
  • by borodulin
  • Repository
  • 11 Watchers
  • 52 Stars
  • 5,421 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 32 Forks
  • 4 Open issues
  • 13 Versions
  • 17 % Grown

The README.md

Yii2 OAuth 2.0 Server

Build Status, (*1)

Description

This extension provides simple implementation of Oauth 2.0 specification using Yii2 framework., (*2)

Installation

The preferred way to install this extension is through composer., (*3)

To install, either run, (*4)

$ php composer.phar require conquer/oauth2 "*"

or add, (*5)

"conquer/oauth2": "*"

to the require section of your composer.json file., (*6)

Migrations are available from migrations folder., (*7)

To add migrations to your application, edit the console config file to configure a namespaced migration:, (*8)

'controllerMap' => [
    // ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => null,
        'migrationNamespaces' => [
            // ...
            'conquer\oauth2\migrations',
        ],
    ],
],

Then issue the migrate/up command:, (*9)

yii migrate/up

You also need to specify message translation source for this package:, (*10)

'components' => [
    'i18n' => [
        'translations' => [
            'conquer/oauth2' => [
                'class' => \yii\i18n\PhpMessageSource::class,
                'basePath' => '@conquer/oauth2/messages',
            ],
        ],
    ]
],

Usage

OAuth 2.0 Authorization usage, (*11)

namespace app\controllers;

use app\models\LoginForm;

class AuthController extends \yii\web\Controller
{
    public function behaviors()
    {
        return [
            /** 
             * Checks oauth2 credentions and try to perform OAuth2 authorization on logged user.
             * AuthorizeFilter uses session to store incoming oauth2 request, so 
             * you can do additional steps, such as third party oauth authorization (Facebook, Google ...)  
             */
            'oauth2Auth' => [
                'class' => \conquer\oauth2\AuthorizeFilter::className(),
                'only' => ['index'],
            ],
        ];
    }
    public function actions()
    {
        return [
            /**
             * Returns an access token.
             */
            'token' => [
                'class' => \conquer\oauth2\TokenAction::classname(),
            ],
            /**
             * OPTIONAL
             * Third party oauth providers also can be used.
             */
            'back' => [
                'class' => \yii\authclient\AuthAction::className(),
                'successCallback' => [$this, 'successCallback'],
            ],
        ];
    }
    /**
     * Display login form, signup or something else.
     * AuthClients such as Google also may be used
     */
    public function actionIndex()
    {
        $model = new LoginForm();
        if ($model->load(\Yii::$app->request->post()) && $model->login()) {
            if ($this->isOauthRequest) {
                $this->finishAuthorization();
            } else {
                return $this->goBack();
            }
        } else {
            return $this->render('index', [
                'model' => $model,
            ]);
        }
    }
    /**
     * OPTIONAL
     * Third party oauth callback sample
     * @param OAuth2 $client
     */
    public function successCallback($client)
    {
        switch ($client::className()) {
            case GoogleOAuth::className():
                // Do login with automatic signup                
                break;
            ...
            default:
                break;
        }
        /**
         * If user is logged on, redirects to oauth client with success,
         * or redirects error with Access Denied
         */
        if ($this->isOauthRequest) {
            $this->finishAuthorization();
        }
    }

}

Api controller sample, (*12)

class ApiController extends \yii\rest\Controller
{
    public function behaviors()
    {
        return [
            /** 
             * Performs authorization by token
             */
            'tokenAuth' => [
                'class' => \conquer\oauth2\TokenAuth::className(),
            ],
        ];
    }
    /**
     * Returns username and email
     */
    public function actionIndex()
    {
        $user = \Yii::$app->user->identity;
        return [
            'username' => $user->username,
            'email' =>  $user->email,
        ];
    }
}

Sample client config, (*13)

return [
...
   'components' => [
       'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'myserver' => [
                    'class' => 'yii\authclient\OAuth2',
                    'clientId' => 'unique client_id',
                    'clientSecret' => 'client_secret',
                    'tokenUrl' => 'http://myserver.local/auth/token',
                    'authUrl' => 'http://myserver.local/auth/index',
                    'apiBaseUrl' => 'http://myserver.local/api',
                ],
            ],
        ],
];

If you want to use Resource Owner Password Credentials Grant, implement \conquer\oauth2\OAuth2IdentityInterface., (*14)

use conquer\oauth2\OAuth2IdentityInterface;

class User extends ActiveRecord implements IdentityInterface, OAuth2IdentityInterface
{
    ...

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findIdentityByUsername($username)
    {
        return static::findOne(['username' => $username]);
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    ...
}

Warning

As official documentation says:, (*15)

Since this access token request utilizes the resource owner's password, the authorization server MUST protect the endpoint against brute force attacks (e.g., using rate-limitation or generating alerts)., (*16)

It's strongly recommended to rate limits on token endpoint. Fortunately, Yii2 have instruments to do this., (*17)

For further information see Yii2 Ratelimiter, (*18)

License

conquer/oauth2 is released under the MIT License. See the bundled LICENSE for details., (*19)

The Versions

22/03 2018

dev-master

9999999-dev https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 oauth2 oauth2-server

22/03 2018

1.6

1.6.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

The Development Requires

yii2 oauth2 oauth2-server

21/03 2017

1.5.4

1.5.4.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

14/01 2016

1.5.3

1.5.3.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

18/12 2015

1.5.2

1.5.2.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

17/12 2015

1.5.1

1.5.1.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

22/10 2015

1.5

1.5.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

21/10 2015

1.4

1.4.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

27/07 2015

1.3

1.3.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

15/07 2015

1.2

1.2.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

14/07 2015

1.1

1.1.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

13/07 2015

1.0

1.0.0.0 https://github.com/borodulin/yii2-oauth2-server

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server