Skip to content

Commit 23e1595

Browse files
author
Maliy Igor
committed
Oauth2 wrapper integration
1 parent c7f3c05 commit 23e1595

File tree

5 files changed

+64
-44
lines changed

5 files changed

+64
-44
lines changed

Module.php

+47
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ class Module extends \yii\base\Module
1212

1313
public $storageDefault = 'filsh\yii2\oauth2server\storage\Pdo';
1414

15+
public $modelClasses = [];
16+
1517
private $_server;
1618

19+
private $_models = [];
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
public function init()
25+
{
26+
parent::init();
27+
$this->modelClasses = array_merge($this->getDefaultModelClasses(), $this->modelClasses);
28+
}
29+
1730
public function getServer($force = false)
1831
{
1932
if($this->_server === null || $force === true) {
@@ -68,4 +81,38 @@ protected function createStorage()
6881

6982
return $storages;
7083
}
84+
85+
/**
86+
* Get object instance of model
87+
* @param string $name
88+
* @param array $config
89+
* @return ActiveRecord
90+
*/
91+
public function model($name, $config = [])
92+
{
93+
// return object if already created
94+
if(!empty($this->_models[$name])) {
95+
return $this->_models[$name];
96+
}
97+
98+
// create object
99+
$className = $this->modelClasses[ucfirst($name)];
100+
$this->_models[$name] = Yii::createObject(array_merge(["class" => $className], $config));
101+
return $this->_models[$name];
102+
}
103+
104+
/**
105+
* Get default model classes
106+
*/
107+
protected function getDefaultModelClasses()
108+
{
109+
return [
110+
'Clients' => 'filsh\yii2\oauth2server\models\OauthClients',
111+
'AccessTokens' => 'filsh\yii2\oauth2server\models\OauthAccessTokens',
112+
'AuthorizationCodes' => 'filsh\yii2\oauth2server\models\OauthAuthorizationCodes',
113+
'RefreshTokens' => 'filsh\yii2\oauth2server\models\OauthRefreshTokens',
114+
'Scopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
115+
];
116+
}
117+
71118
}

controllers/DefaultController.php

+3-27
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
namespace filsh\yii2\oauth2server\controllers;
44

5-
use \Yii;
5+
use Yii;
66
use yii\helpers\ArrayHelper;
7-
use yii\filters\auth\CompositeAuth;
8-
use yii\filters\auth\HttpBearerAuth;
9-
10-
use filsh\yii2\oauth2server\filters\ExceptionFilter;
11-
use filsh\yii2\oauth2server\filters\auth\QueryParamAuth;
7+
use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter;
128

139
class DefaultController extends \yii\rest\Controller
1410
{
@@ -18,16 +14,8 @@ class DefaultController extends \yii\rest\Controller
1814
public function behaviors()
1915
{
2016
return ArrayHelper::merge(parent::behaviors(), [
21-
'authenticator' => [
22-
'class' => CompositeAuth::className(),
23-
'authMethods' => [
24-
HttpBearerAuth::className(),
25-
QueryParamAuth::className(),
26-
],
27-
'except' => ['token']
28-
],
2917
'exceptionFilter' => [
30-
'class' => ExceptionFilter::className()
18+
'class' => ErrorToExceptionFilter::className()
3119
],
3220
]);
3321
}
@@ -40,16 +28,4 @@ public function actionToken()
4028

4129
return $response->getParameters();
4230
}
43-
44-
public function actionResource()
45-
{
46-
$server = Yii::$app->getModule('oauth2')->getServer();
47-
$request = Yii::$app->getModule('oauth2')->getRequest();
48-
49-
if (!$server->verifyResourceRequest($request)) {
50-
return $server->getResponse()->getParameters();
51-
}
52-
53-
return ['success' => true, 'message' => 'You accessed my APIs!'];
54-
}
5531
}

filters/ExceptionFilter.php renamed to filters/ErrorToExceptionFilter.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Yii;
66
use yii\base\Controller;
77

8-
class ExceptionFilter extends yii\base\Behavior
8+
class ErrorToExceptionFilter extends yii\base\Behavior
99
{
1010
public function events()
1111
{
@@ -20,12 +20,17 @@ public function events()
2020
public function afterAction($event)
2121
{
2222
$response = Yii::$app->getModule('oauth2')->getServer()->getResponse();
23-
if($response !== null && !$response->isSuccessful()) {
23+
24+
$isValid = true;
25+
if($response !== null) {
26+
$isValid = $response->isInformational() || $response->isSuccessful() || $response->isRedirection();
27+
}
28+
if(!$isValid) {
2429
$status = $response->getStatusCode();
2530
// TODO: необходимо также пробрасывать error_uri
2631
$message = $response->getParameter('error_description');
2732
if($message === null) {
28-
$message = P::t('yii', 'An internal server error occurred.');
33+
$message = Yii::t('yii', 'An internal server error occurred.');
2934
}
3035
throw new \yii\web\HttpException($status, $message);
3136
}

filters/auth/QueryParamAuth.php renamed to filters/auth/CompositeAuth.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44

55
use \Yii;
66

7-
class QueryParamAuth extends \yii\filters\auth\QueryParamAuth
7+
class CompositeAuth extends \yii\filters\auth\CompositeAuth
88
{
9-
/**
10-
* @inheritdoc
11-
*/
12-
public $tokenParam = 'access_token';
13-
14-
/**
15-
* @inheritdoc
16-
*/
179
public function authenticate($user, $request, $response)
1810
{
1911
$oauthsServer = Yii::$app->getModule('oauth2')->getServer();
2012
$oauthRequest = Yii::$app->getModule('oauth2')->getRequest();
21-
if (!$oauthsServer->verifyResourceRequest($oauthRequest)) {
22-
$this->handleFailure($response);
23-
} else {
13+
if ($oauthsServer->verifyResourceRequest($oauthRequest)) {
2414
return parent::authenticate($user, $request, $response);
2515
}
16+
17+
return null;
2618
}
2719
}

migrations/m140501_075311_add_oauth2_server.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
try {
1616
$this->createTable('{{%oauth_clients}}', [
1717
'client_id' => Schema::TYPE_STRING . '(32) NOT NULL',
18-
'client_secret' => Schema::TYPE_STRING . '(32) NOT NULL',
18+
'client_secret' => Schema::TYPE_STRING . '(32) DEFAULT NULL',
1919
'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL',
2020
'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL',
2121
'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL',
@@ -76,7 +76,7 @@ public function up()
7676

7777
// insert client data
7878
$this->batchInsert('{{%oauth_clients}}', ['client_id', 'client_secret', 'redirect_uri', 'grant_types'], [
79-
['testclient', 'testpass', 'http://fake/', 'client_credentials password'],
79+
['testclient', 'testpass', 'http://fake/', 'client_credentials authorization_code password implicit'],
8080
]);
8181

8282
$transaction->commit();

0 commit comments

Comments
 (0)