bnviking/yii2-oauth2

OAuth2.0 Yii2 extension

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.1.1 2022-04-09 14:57 UTC

This package is auto-updated.

Last update: 2024-05-09 20:24:14 UTC


README

12669446?s=400&u=d883b62c0adcae00380135155c820b5d928224dc&v=4

OAuth2.0

Extension for Yii2 framework

Widget OAuth2.0

The following clients are currently supported for authorization:

Installation

The preferred way to install this extension is through [composer] (http://getcomposer.org/download/).

Either run

php composer.phar require --prefer-dist bnviking/yii2-oauth2 "~v1.1.1"

or add

"bnviking/yii2-oauth2": "~v1.1.1"

to the require section of your composer.json file.

Config

'components' => [
    ...
    'bnvOAuth2'=> [
        'class' => \bnviking\oauth2\OAuth2Config::class,
        'clientUrlName' => 'client',
        'authUrl' => 'auth2/authorize', 
        'clients' => [
            'discord'=>[
                'class'=> \bnviking\oauth2\clients\Discord::class,
                'clientID' => 'discord_client_id',
                'clientSecret' => 'discord_client_secret',
            ],
            'vkontakte'=>[
                'class'=> \bnviking\oauth2\clients\VKontakte::class,
                'clientID' => 'vkontakte_client_id',
                'clientSecret' => 'vkontakte_client_secret',
            ],
            'yandex'=>[
                'class'=> \bnviking\oauth2\clients\Yandex::class,
                'clientID' => 'yandex_client_id',
                'clientSecret' => 'yandex_client_secret',
            ],
            'mailru'=>[
                'class'=> \bnviking\oauth2\clients\MailRu::class,
                'clientID' => 'mailru_client_id',
                'clientSecret' => 'mailru_client_secret',
            ],
            'trovo'=>[
                'class'=> \bnviking\oauth2\clients\Trovo::class,
                'clientID' => 'trovo_client_id',
                'clientSecret' => 'trovo_client_secret',
            ],
            'twitch'=>[
                'class'=> \bnviking\oauth2\clients\Twitch::class,
                'clientID' => 'twitch_client_id',
                'clientSecret' => 'twitch_client_secret',
            ],
        ]
    ]
    ...
]

Create link to OAuth2

  • clientUrlName - name param Client ID for create URL
  • authUrl - <controller>/<action>

Redirect URL for your APP: https://my.site/controller/action

Use in action

<?php

namespace app\controllers;

use bnviking\oauth2\OAuth2;
use yii\web\Controller;
use Yii;

class Auth2Controller extends Controller
{
    public function actionAuthorize(): string
    {
        /** @var \bnviking\oauth2\components\AuthResult $authResult */
        $authResult = OAuth2::init();

        if ($authResult->hasError()) {
            $errors = $authResult->getErrors();
            Yii::$app->session->setFlash('error', implode('<br>', $errors));
            $this->goHome();
            return '';
        }

        if ($authResult->getAction() === AuthResult::ACTION_ENTERS_SITE) {
            /** @var \bnviking\oauth2\components\OAuth2BaseClient $clientData Auth client data */
            $clientData = $authResult->getClient();
            /** @var \bnviking\oauth2\components\UserResult $userData User data */
            $userData = $authResult->getUser();
            /*
             * property $userData:
             *   id - User ID
             *   email - User email
             *   username - User name
             *   token - Auth token
             *   tokenReset - Reset token
             *   tokenType - Token Type
             *   tokenExpires - Token lifetime [seconds]
            */

            /*
             * Here you can register or enter the site
             */

        }
        ...
    }
}

Widget

<?=\bnviking\oauth2\widgets\OAuth2Buttons::widget()?>

Widget OAuth2.0 example

Additional html options for widget
'components' => [
    ...
     'bnvOAuth2'=> [
        'clients' => [
            ...
            'htmlOptions' => ['class'=>'my-css-class']
        ]
     ]
]

Custom widget example

All clients can be retrieved using the ClientManager

widget.php

<?php
use bnviking\oauth2\components\ClientsManager;
use bnviking\oauth2\exception\OAuth2Exception;
use yii\base\Widget;

class OAuth2Buttons extends Widget
{
    public function run(): string
    {
        $error = '';
        $clients = [];
        try {
            $clientManager = new ClientsManager();
            /** @var \bnviking\oauth2\components\OAuth2BaseClient[] $clients Auth client data */
            $clients = $clientManager->getClients();
        } catch (OAuth2Exception $e) {
            $error = $e->getMessage();
        }

        return $this->render('view-widget',['clients' => $clients,'error' => $error]);
    }

}

view-widget.php

<?php
/* @var yii\web\View $this */
/* @var OAuth2BaseClient[] $clients */
/* @var string $error */

use bnviking\oauth2\components\OAuth2BaseClient;
use bnviking\oauth2\exception\OAuth2Exception;
use yii\helpers\Html;

\bnviking\oauth2\OAuth2Bundle::register($this);
?>
<div class="row">
    <div class="col-lg-12 text-center">
        <?php
            if ($error!== '') {
                echo Html::tag('div',$error,['class'=>'alert alert-danger','role'=>'alert']);
            }
            echo Html::beginTag('div',['class'=>'btn-group']);
                foreach ($clients as $clientId => $client) {
                    echo Html::a('', $client->url, $client->htmlOptions);
                }
            echo Html::endTag('div');
        ?>
    </div>
</div>