RestController::actionToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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