tecnocen / yii2-oauth2-server
PHP OAuth2 服务器
Requires
- php: >=5.6
- bshaffer/oauth2-server-php: ~1.3
- tecnocen/yii2-migrate: *
- yiisoft/yii2: ~2.0.12
Requires (Dev)
- ext-curl: *
- codeception/base: ^2.2.1
- codeception/verify: ~0.3.1|~1.0.0
- flow/jsonpath: ~0.3
- phpunit/php-code-coverage: 5.3.*
- yiisoft/yii2-debug: *
README
实现 OAuth2 服务器的一个包装器。
该项目是从 Filsh 原始项目 分支出来的,但更改并不透明,请阅读 [UPGRADE.md] 以升级到最新版本。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist tecnocen/yii2-oauth2-server "*"
或者在 composer.json 的 require 部分添加
"tecnocen/yii2-oauth2-server": "~4.1"
。
使用方法
要使用此扩展,只需将以下代码添加到您的应用程序配置中作为新模块
'bootstrap' => ['oauth2'], 'modules'=>[ // other modules ... 'oauth2' => [ 'class' => 'tecnocen\oauth2server\Module', 'tokenParamName' => 'accessToken', 'tokenAccessLifetime' => 3600 * 24, 'storageMap' => [ 'user_credentials' => 'app\models\User', ], 'grantTypes' => [ 'user_credentials' => [ 'class' => 'OAuth2\GrantType\UserCredentials', ], 'refresh_token' => [ 'class' => 'OAuth2\GrantType\RefreshToken', 'always_issue_new_refresh_token' => true ] ] ] ],
引导程序将初始化翻译并将所需的 URL 规则添加到 Yii::$app->urlManager
。
JWT 令牌
此分支不支持 JWT 令牌,请随时提交一个 (pull request)[https://github.com/tecnocen-com/yii2-oauth2-server/pulls] 以启用此功能。
UserCredentialsInterface
传递给 Yii::$app->user->identityClass
的类必须实现接口 \OAuth2\Storage\UserCredentialsInterface
,以在用户表中存储 OAuth2 凭据。
use Yii; class User extends common\models\User implements \OAuth2\Storage\UserCredentialsInterface { /** * Implemented for Oauth2 Interface */ public static function findIdentityByAccessToken($token, $type = null) { /** @var \tecnocen\oauth2server\Module $module */ $module = Yii::$app->getModule('oauth2'); $token = $module->getServer()->getResourceController()->getToken(); return !empty($token['user_id']) ? static::findIdentity($token['user_id']) : null; } /** * Implemented for Oauth2 Interface */ public function checkUserCredentials($username, $password) { $user = static::findByUsername($username); if (empty($user)) { return false; } return $user->validatePassword($password); } /** * Implemented for Oauth2 Interface */ public function getUserDetails($username) { $user = static::findByUsername($username); return ['user_id' => $user->getId()]; } }
迁移
下一步是运行迁移
yii migrate all -p=@tecnocen/oauth2server/migrations/tables
yii fixture "*" -n=tecnocen/oauth2server/fixtures
第一个命令创建 OAuth2 数据库模式。第二个命令为 http://fake/
插入测试客户端凭据 testclient:testpass
。
控制器
为了支持通过访问令牌进行认证。只需为您的控制器或模块添加行为即可。
use yii\helpers\ArrayHelper; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; use tecnocen\oauth2server\filters\auth\CompositeAuth; class Controller extends \yii\rest\Controller { /** * @inheritdoc */ public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => [ 'class' => CompositeAuth::class, 'authMethods' => [ ['class' => HttpBearerAuth::class], [ 'class' => QueryParamAuth::class, 'tokenParam' => 'accessToken', ], ] ], ]); } }
上面的代码与默认实现相同,可以简化为
use yii\helpers\ArrayHelper; use tecnocen\oauth2server\filters\auth\CompositeAuth; class Controller extends \yii\rest\Controller { /** * @inheritdoc */ public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => CompositeAuth::class, ]); } }
作用域
属性 tecnocen\oauth2server\filters\auth\CompositeAuth::$actionScopes
设置了哪些操作需要特定的作用域。如果这些作用域不满足,则操作不会执行,服务器将回复 HTTP 状态码 403。
public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'authenticator' => [ 'class' => CompositeAuth::class, 'actionScopes' => [ 'create' => 'default create', 'update' => 'default edit', '*' => 'default', // wildcards are allowed ] ],, ]); }
自动吊销令牌
有时需要在每个请求上吊销令牌,以防止请求被触发两次。
为了启用此功能,您需要在用于识别已认证用户的类中实现 tecnocen\oauth2server\RevokeAccessTokenInterface
。
use OAuth2\Storage\UserCredentialsInterface; use tecnocen\oauth2server\RevokeAccessTokenInterface; use tecnocen\oauth2server\RevokeAccessTokenTrait; class User extend \yii\db\ActiveRecord implement UserCredentialsInterface, RevokeAccessTokenInterface { use RevokeAccessTokenTrait; // optional, trait with default implementation. // rest of the class. }
然后使用此类作为 Yii::$app->user->identityClass
的配置
附加动作过滤器 tecnocen\oauth2server\filters\RevokeAccessToken
允许配置自动吊销访问令牌的操作。
public function behaviors() { return [ 'revokeToken' => [ 'class' => \tecnocen\oauth2server\filters\RevokeAccessToken::class, // optional only revoke the token if it has any of the following // scopes. if not defined it will always revoke the token. 'scopes' => ['author', 'seller'], // optional whether or not revoke all tokens or just the active one 'revokeAll' => true, // optional if non authenticated users are permited. 'allowGuests' => true, // which actions this behavior applies to. 'only' => ['create', 'update'], ] ]; }
使用 JS 生成令牌
获取访问令牌(js 示例)
var url = window.location.host + "/oauth2/token"; var data = { 'grant_type':'password', 'username':'<some login from your user table>', 'password':'<real pass>', 'client_id':'testclient', 'client_secret':'testpass' }; //ajax POST `data` to `url` here //
使用
- Yii 2: 快速、安全且专业的 PHP 框架 https://yiiframework.cn
行为准则
请阅读 CODE_OF_CONDUCT.md 了解我们的行为准则。
贡献
请阅读 CONTRIBUTING.md 以了解提交拉取请求给我们的流程细节。
版本控制
我们使用 SemVer 进行版本控制。有关可用的版本,请参阅 此存储库的标签。
考虑到 SemVer 的版本控制规则9、10和11提到了预发布版本,它们在Tecnocen-com中将不会使用。
作者
- Angel Guevara - 初始工作 - Tecnocen.com
- Carlos Llamosas - 初始工作 - Tecnocen.com
还可以查看参与此项目的 贡献者列表。
许可协议
本项目遵循MIT许可协议 - 详细内容请参阅 LICENSE.md 文件。
致谢
- 待办事项 - 感谢任何使用了其代码的人
- 待办事项 - 灵感来源
- 待办事项 - 等等