2017 © Pedro Peláez
 

yii2-extension yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

image

yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  • Saturday, November 11, 2017
  • by xutl
  • Repository
  • 0 Watchers
  • 1 Stars
  • 280 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 24 Versions
  • 4 % Grown

The README.md

Yii2 OAuth 2.0 Server

Description

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

Installation

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

To install, either run, (*3)

$ php composer.phar require yuncms/oauth2 "~2.0.0"

or add, (*4)

"yuncms/oauth2": "~2.0.0"

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

To create database tables run migration command, (*6)

$ yii migrate --migrationNamespace=@yuncms/oauth2/migrations

Usage

url: Application, (*7)

http://yourname.com/oauth2/auth/authorize

http://yourname.com/oauth2/auth/token

refresh_token
http://yourname.com/oauth2/auth/token

OAuth 2.0 Authorization usage, (*8)

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' => \yuncms\oauth2\AuthorizeFilter::className(),
                'only' => ['index'],
            ],
        ];
    }
    public function actions()
    {
        return [
            /**
             * Returns an access token.
             */
            'token' => [
                'class' => \yuncms\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, (*9)

class ApiController extends \yii\rest\Controller
{
    public function behaviors()
    {
        return [
            /** 
             * Performs authorization by token
             */
            'tokenAuth' => [
                'class' => \yuncms\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, (*10)

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',
                ],
            ],
        ],
];

Thanks to

License

Yii2-oauth2 is released under the MIT License. See the bundled LICENSE.md for details., (*11)

The Versions

11/11 2017

dev-master

9999999-dev https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

11/11 2017

2.0.3.1

2.0.3.1 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

26/10 2017

2.0.3

2.0.3.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

14/10 2017

2.0.2

2.0.2.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/09 2017

2.0.1.5

2.0.1.5 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/09 2017

2.0.1.4

2.0.1.4 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/09 2017

2.0.1.3

2.0.1.3 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/09 2017

2.0.1.2

2.0.1.2 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/09 2017

2.0.1.1

2.0.1.1 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

04/09 2017

2.0.1

2.0.1.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

04/09 2017

2.0.0

2.0.0.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

30/08 2017

2.0.4.1

2.0.4.1 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

25/08 2017

2.0.4

2.0.4.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

21/07 2017

1.1.0

1.1.0.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

20/07 2017

1.0.9

1.0.9.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

20/07 2017

1.0.8

1.0.8.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

28/06 2017

1.0.7

1.0.7.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

26/06 2017

1.0.6

1.0.6.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

26/06 2017

1.0.5

1.0.5.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

26/06 2017

1.0.4

1.0.4.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

26/06 2017

1.0.3

1.0.3.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

23/06 2017

1.0.2

1.0.2.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

22/06 2017

1.0.1

1.0.1.0 https://github.com/yuncms/yii2-oauth2

The Oauth2 Server extension for the Yii2 framework

  Sources   Download

MIT

The Requires

 

yii2 oauth2 oauth2-server

13/01 2017

1.0.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