OAuth 2.0 Yii2 Extension

Overview

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 - /

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

Use in action



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('
'
, $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


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

'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'); ?>
">

/* @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">
        
            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>
You might also like...
OAuth client integration for Symfony. Supports both OAuth1.0a and OAuth2.

HWIOAuthBundle The HWIOAuthBundle adds support for authenticating users via OAuth1.0a or OAuth2 in Symfony. Note: this bundle adds easy way to impleme

An OAuth 2.0 bridge for Laravel and Lumen [DEPRECATED FOR LARAVEL 5.3+]
An OAuth 2.0 bridge for Laravel and Lumen [DEPRECATED FOR LARAVEL 5.3+]

OAuth 2.0 Server for Laravel (deprecated for Laravel 5.3+) Note: This package is no longer maintaned for Laravel 5.3+ since Laravel now features the P

Kaiju is an open source verification bot based on Discord's OAuth written in C# and PHP, with the functionality of being able to integrate the user to a new server in case yours is suspended.
Kaiju is an open source verification bot based on Discord's OAuth written in C# and PHP, with the functionality of being able to integrate the user to a new server in case yours is suspended.

What is Kaiju? Kaiju is an open source verification bot for Discord servers, based on OAuth and with permission for the server owner, to be able to mi

The most popular PHP library for use with the Twitter OAuth REST API.
The most popular PHP library for use with the Twitter OAuth REST API.

TwitterOAuth The most popular PHP library for Twitter's OAuth REST API. See documentation at https://twitteroauth.com. PHP versions listed as "active

This module is intended to provide oauth authentication to freescout.

OAuth FreeScout This module is intended to provide oauth authentication to freescout. Module was tested on keycloak oauth provider with confidential o

A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package

laravel-social A Laravel 5 package for OAuth Social Login/Register implementation using Laravel socialite and (optionally) AdminLTE Laravel package. I

OAuth Service Provider for Laravel 4

OAuth wrapper for Laravel 4 oauth-4-laravel is a simple laravel 4 service provider (wrapper) for Lusitanian/PHPoAuthLib which provides oAuth support i

OAuth Service Provider for Laravel 5

OAuth wrapper for Laravel 5 oauth-5-laravel is a simple laravel 5 service provider (wrapper) for Lusitanian/PHPoAuthLib which provides oAuth support i

The Salla OAuth Client library is designed to provide client applications with secure delegated access to Salla Merchant stores.
The Salla OAuth Client library is designed to provide client applications with secure delegated access to Salla Merchant stores.

Salla Provider for OAuth 2.0 Client This package provides Salla OAuth 2.0 support for the PHP League's OAuth 2.0 Client. To use this package, it will

Releases(v1.1.1)
Laravel wrapper around OAuth 1 & OAuth 2 libraries.

Introduction Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitL

The Laravel Framework 5.2k Dec 27, 2022
PHPoAuthLib provides oAuth support in PHP 7.2+ and is very easy to integrate with any project which requires an oAuth client.

PHPoAuthLib NOTE: I'm looking for someone who could help to maintain this package alongside me, just because I don't have a ton of time to devote to i

David Desberg 1.1k Dec 27, 2022
OAuth 1/2 Provider implementations for chillerlan/php-oauth-core. PHP 7.4+

chillerlan/php-oauth-providers Documentation See the wiki for advanced documentation. Requirements PHP 7.4+ a PSR-18 compatible HTTP client library of

chillerlan 4 Dec 2, 2022
GUI manager for RBAC (Role Base Access Control) Yii2. Easy to manage authorization of user

RBAC Manager for Yii 2 GUI manager for RBAC (Role Base Access Control) Yii2. Easy to manage authorization of user ?? . Documentation Important: If you

MDMunir Software 1.2k Jan 7, 2023
This extension expands WSOAuth extension and provide a EveOnline SSO login method

This extension expands WSOAuth extension and provide a EveOnline SSO login method

Raze Soldier 1 Nov 15, 2021
A spec compliant, secure by default PHP OAuth 2.0 Server

PHP OAuth 2.0 Server league/oauth2-server is a standards compliant implementation of an OAuth 2.0 authorization server written in PHP which makes work

The League of Extraordinary Packages 6.2k Jan 4, 2023
Easy integration with OAuth 2.0 service providers.

OAuth 2.0 Client This package provides a base for integrating with OAuth 2.0 service providers. The OAuth 2.0 login flow, seen commonly around the web

The League of Extraordinary Packages 3.4k Dec 31, 2022
PHP 5.3+ oAuth 1/2 Client Library

PHPoAuthLib NOTE: I'm looking for someone who could help to maintain this package alongside me, just because I don't have a ton of time to devote to i

David Desberg 1.1k Dec 27, 2022
OAuth 1 Client

OAuth 1.0 Client OAuth 1 Client is an OAuth RFC 5849 standards-compliant library for authenticating against OAuth 1 servers. It has built in support f

The League of Extraordinary Packages 907 Dec 16, 2022
The first PHP Library to support OAuth for Twitter's REST API.

THIS IS AN MODIFIED VERSION OF ABRAHAMS TWITTER OAUTH CLASS The directories are structured and the class uses PHP5.3 namespaces. Api.php has a new

Ruud Kamphuis 51 Feb 11, 2021