|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\oauth2server\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use roaresearch\yii2\oauth2server\filters\ErrorToExceptionFilter; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\{helpers\ArrayHelper, rest\OptionsAction}; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property roaresearch\yii2\oauth2server\Module $module |
|
11
|
|
|
*/ |
|
12
|
|
|
class RestController extends \yii\rest\Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @inheritdoc |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public function behaviors(): array |
|
18
|
|
|
{ |
|
19
|
5 |
|
return ArrayHelper::merge(parent::behaviors(), [ |
|
20
|
|
|
'exceptionFilter' => [ |
|
21
|
5 |
|
'class' => ErrorToExceptionFilter::class, |
|
22
|
5 |
|
'oauth2Module' => $this->module, |
|
23
|
|
|
], |
|
24
|
|
|
]); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
5 |
|
protected function verbs(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
5 |
|
'token' => ['POST'], |
|
34
|
|
|
'options' => ['OPTIONS'], |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function actions(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
'options' => [ |
|
45
|
|
|
'class' => OptionsAction::class, |
|
46
|
|
|
'collectionOptions' => ['POST', 'OPTIONS'], |
|
47
|
|
|
'resourceOptions' => ['OPTIONS'], |
|
48
|
|
|
], |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Action to generate oauth2 tokens. |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function actionToken() |
|
56
|
|
|
{ |
|
57
|
5 |
|
return $this->module->getServer()->handleTokenRequest() |
|
58
|
5 |
|
->getParameters(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Action to generate an authorization code which will be redirected to a |
|
63
|
|
|
* uri matching the oauth2 client uri. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function actionAuthorize(int $authorized = 0) |
|
66
|
|
|
{ |
|
67
|
|
|
$response = $this->module->handleAuthorizeRequest((bool) $authorized); |
|
68
|
|
|
|
|
69
|
|
|
return $response->isRedirection() |
|
70
|
|
|
? $this->redirect( |
|
71
|
|
|
$response->getHttpHeader('Location'), |
|
72
|
|
|
$response->getStatusCode(), |
|
73
|
|
|
) |
|
74
|
|
|
: $response->send(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|