bnviking/yii2-oauth2
Composer 安装命令:
composer require bnviking/yii2-oauth2
包简介
OAuth2.0 Yii2 extension
README 文档
README
OAuth2.0
Extension for Yii2 framework
The following clients are currently supported for authorization:
- vk.com [register your application]
- mail.ru [register your application]
- yandex.ru [register your application]
- discord.com [register your application]
- trovo.live [register your application]
- twitch.tv [register your application]
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()?>
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>
bnviking/yii2-oauth2 适用场景与选型建议
bnviking/yii2-oauth2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 207 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「oauth2」 「extension」 「vkontakte」 「vk」 「yandex」 「twitch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bnviking/yii2-oauth2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnviking/yii2-oauth2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bnviking/yii2-oauth2 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Vkontakte PHP SDK
A custom URL rule class for Yii 2 which allows to create translated URL rules
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 207
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-03
