filsh / yii2-oauth2-server
PHP OAuth2 服务器
Requires
This package is auto-updated.
Last update: 2024-09-19 21:01:07 UTC
README
实现 OAuth2 服务器的包装器(https://github.com/bshaffer/oauth2-server-php)
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist filsh/yii2-oauth2-server "*"
或者
"filsh/yii2-oauth2-server": "^2.0"
将以下内容添加到您的 composer.json 文件的 require 部分。
要使用此扩展,只需在您的应用程序配置中添加以下代码
'bootstrap' => ['oauth2'], 'modules' => [ 'oauth2' => [ 'class' => 'filsh\yii2\oauth2server\Module', 'tokenParamName' => 'accessToken', 'tokenAccessLifetime' => 3600 * 24, 'storageMap' => [ 'user_credentials' => 'common\models\User', ], 'grantTypes' => [ 'user_credentials' => [ 'class' => 'OAuth2\GrantType\UserCredentials', ], 'refresh_token' => [ 'class' => 'OAuth2\GrantType\RefreshToken', 'always_issue_new_refresh_token' => true ] ] ] ]
common\models\User
- 实现接口 \OAuth2\Storage\UserCredentialsInterface
的用户模型,因此 OAuth2 凭据数据存储在用户表中
下一步,您应该运行迁移
yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/src/migrations
此迁移创建 OAuth2 数据库模式并在 http://fake/
为 testclient:testpass
插入测试用户凭据
将 URL 规则添加到 urlManager
'urlManager' => [ 'rules' => [ 'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>', ... ] ]
配置
您可以通过在模块上设置 options
属性来传递额外的 OAuth2 服务器选项。这些选项配置了底层的 OAuth2 服务器以及 bshaffer/oauth2-server-php 的各个部分/组件。例如,您可以通过设置 auth_code_lifetime
选项来配置响应中的授权代码有效期。其中一些作为模块上的独立属性实现:tokenParamName
=> use_jwt_access_tokens
、tokenAccessLifetime
=> token_param_name
、useJwtToken
=> access_lifetime
。完整选项列表由底层 OAuth2 服务器主要组件支持 - 源代码。各种组件的选项分散在 bshaffer/oauth2-server-php 源代码中。
用法
要使用此扩展,只需为您的基控制器添加行为
use yii\helpers\ArrayHelper; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter; use filsh\yii2\oauth2server\filters\auth\CompositeAuth; class Controller extends \yii\rest\Controller { /** * @inheritdoc */ public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => [ 'class' => CompositeAuth::className(), 'authMethods' => [ ['class' => HttpBearerAuth::className()], ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'], ] ], 'exceptionFilter' => [ 'class' => ErrorToExceptionFilter::className() ], ]); } }
在站点控制器中创建授权码的授权操作
https://api.mysite.com/authorize?response_type=code&client_id=TestClient&redirect_uri=https://fake/
/** * SiteController */ class SiteController extends Controller { /** * @return mixed */ public function actionAuthorize() { if (Yii::$app->getUser()->getIsGuest()) return $this->redirect('login'); /** @var $module \filsh\yii2\oauth2server\Module */ $module = Yii::$app->getModule('oauth2'); $response = $module->getServer()->handleAuthorizeRequest(null, null, !Yii::$app->getUser()->getIsGuest(), Yii::$app->getUser()->getId()); /** @var object $response \OAuth2\Response */ Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON; return $response->getParameters(); } }
此外,如果您在模块的 options
属性中设置 allow_implicit => true
,则可以使用隐式授权类型 - 更多信息
请求示例
https://api.mysite.com/authorize?response_type=token&client_id=TestClient&redirect_uri=https://fake/cb
带有重定向响应
https://fake/cb#access_token=2YotnFZFEjr1zCsicMWpAA&state=xyz&token_type=bearer&expires_in=3600
JWT 令牌
如果您想获取 JSON Web 令牌 (JWT) 而不是传统令牌,您需要在模块中设置 'useJwtToken' => true
,然后定义两个其他配置:'public_key' => 'app\storage\PublicKeyStorage'
是实现 PublickKeyInterface 的类,以及 'access_token' => 'OAuth2\Storage\JwtAccessToken'
实现 JwtAccessTokenInterface.php
对于 Oauth2 基础库提供的默认 access_token,它工作得很好。只需使用它,一切都会顺利进行。
以及 public_key
<?php namespace app\storage; class PublicKeyStorage implements \OAuth2\Storage\PublicKeyInterface{ private $pbk = null; private $pvk = null; public function __construct() { $this->pvk = file_get_contents('privkey.pem', true); $this->pbk = file_get_contents('pubkey.pem', true); } public function getPublicKey($client_id = null){ return $this->pbk; } public function getPrivateKey($client_id = null){ return $this->pvk; } public function getEncryptionAlgorithm($client_id = null){ return 'RS256'; } }
更多信息,请参阅 https://github.com/bshaffer/oauth2-server-php
作者 & 贡献者
本包的原始作者是 Igor Maliy 。在项目维护阶段,维护者是 Vardan Pogosian。