Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/librecmscom/yuncms-oauth2
The OAuth2 module for the yuncms.
https://github.com/librecmscom/yuncms-oauth2
Last synced: about 1 month ago
JSON representation
The OAuth2 module for the yuncms.
- Host: GitHub
- URL: https://github.com/librecmscom/yuncms-oauth2
- Owner: librecmscom
- Created: 2017-11-14T11:28:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-31T10:11:25.000Z (almost 7 years ago)
- Last Synced: 2023-07-26T15:58:54.685Z (over 1 year ago)
- Language: PHP
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yuncms-oauth2
The OAuth2 module for the yuncms.
[![Latest Stable Version](https://poser.pugx.org/yuncms/yuncms-oauth2/v/stable.png)](https://packagist.org/packages/yuncms/yuncms-oauth2)
[![Total Downloads](https://poser.pugx.org/yuncms/yuncms-oauth2/downloads.png)](https://packagist.org/packages/yuncms/yuncms-oauth2)
[![Build Status](https://img.shields.io/travis/yuncms/yuncms-oauth2.svg)](http://travis-ci.org/yuncms/yuncms-oauth2)
[![License](https://poser.pugx.org/yuncms/yuncms-oauth2/license.svg)](https://packagist.org/packages/yuncms/yuncms-oauth2)## Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
To install, either run
```
$ php composer.phar require yuncms/yuncms-oauth2 "~2.0.0"
```
or add```
"yuncms/yuncms-oauth2": "~2.0.0"
```to the ```require``` section of your `composer.json` file.
To create database tables run migration command
```
$ yii migrate --migrationNamespace=@yuncms/oauth2/migrations
```## Usage
url:
Application
```bash
http://yourname.com/oauth2/auth/authorizehttp://yourname.com/oauth2/auth/token
refresh_token
http://yourname.com/oauth2/auth/token
```OAuth 2.0 Authorization usage
```php
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 controller sample
```php
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,
];
}
}
```
Sample client config
```php
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',
],
],
],
];
```## Thanks to
* [Yii framework](https://github.com/yiisoft/yii2)
* [yii2-oauth2-server](https://github.com/borodulin/yii2-oauth2-server)## License
Yii2-oauth2 is released under the MIT License. See the bundled [LICENSE.md](LICENSE.md)
for details.