yuncms / yuncms-oauth2
该包已被弃用且不再维护。未建议替代包。
yuncms 的 OAuth2 模块。
2.0.1.5
2018-01-31 10:10 UTC
Requires
- jakeasmith/http_build_url: ~1.0
- yiisoft/yii2: ~2.0.6
- yuncms/yuncms-user: ~2.0
README
yuncms 的 OAuth2 模块。
安装
推荐通过 composer 安装此扩展。
安装方法,运行
$ php composer.phar require yuncms/yuncms-oauth2 "~2.0.0"
或添加
"yuncms/yuncms-oauth2": "~2.0.0"
到你的 composer.json
文件的 require
部分。
要创建数据库表,运行迁移命令
$ yii migrate --migrationNamespace=@yuncms/oauth2/migrations
使用方法
url: 应用程序
http://yourname.com/oauth2/auth/authorize http://yourname.com/oauth2/auth/token refresh_token http://yourname.com/oauth2/auth/token
OAuth 2.0 授权使用
namespace app\controllers; use app\models\LoginForm; class AuthController extends \yii\web\Controller { public function behaviors() { return [ /** * Checks oauth2 credentions and try to perform OAuth2 authorization on logged user. * AuthorizeFilter uses session to store incoming oauth2 request, so * you can do additional steps, such as third party oauth authorization (Facebook, Google ...) */ 'oauth2Auth' => [ 'class' => \yuncms\oauth2\AuthorizeFilter::className(), 'only' => ['index'], ], ]; } public function actions() { return [ /** * Returns an access token. */ 'token' => [ 'class' => \yuncms\oauth2\TokenAction::classname(), ], /** * OPTIONAL * Third party oauth providers also can be used. */ 'back' => [ 'class' => \yii\authclient\AuthAction::className(), 'successCallback' => [$this, 'successCallback'], ], ]; } /** * Display login form, signup or something else. * AuthClients such as Google also may be used */ public function actionIndex() { $model = new LoginForm(); if ($model->load(\Yii::$app->request->post()) && $model->login()) { if ($this->isOauthRequest) { $this->finishAuthorization(); } else { return $this->goBack(); } } else { return $this->render('index', [ 'model' => $model, ]); } } /** * OPTIONAL * Third party oauth callback sample * @param OAuth2 $client */ public function successCallback($client) { switch ($client::className()) { case GoogleOAuth::className(): // Do login with automatic signup break; ... default: break; } /** * If user is logged on, redirects to oauth client with success, * or redirects error with Access Denied */ if ($this->isOauthRequest) { $this->finishAuthorization(); } } }
API 控制器示例
class ApiController extends \yii\rest\Controller { public function behaviors() { return [ /** * Performs authorization by token */ 'tokenAuth' => [ 'class' => \yuncms\oauth2\TokenAuth::className(), ], ]; } /** * Returns username and email */ public function actionIndex() { $user = \Yii::$app->user->identity; return [ 'username' => $user->username, 'email' => $user->email, ]; } }
客户端配置示例
return [ ... 'components' => [ 'authClientCollection' => [ 'class' => 'yii\authclient\Collection', 'clients' => [ 'myserver' => [ 'class' => 'yii\authclient\OAuth2', 'clientId' => 'unique client_id', 'clientSecret' => 'client_secret', 'tokenUrl' => 'http://myserver.local/auth/token', 'authUrl' => 'http://myserver.local/auth/index', 'apiBaseUrl' => 'http://myserver.local/api', ], ], ], ];
感谢
许可证
Yii2-oauth2 采用 MIT 许可证发布。有关详细信息,请参阅附带的 LICENSE.md。