|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\patron\actions\authorization; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\craft\ember\actions\LookupTrait; |
|
13
|
|
|
use flipbox\patron\Patron; |
|
14
|
|
|
use flipbox\patron\queries\ProviderQuery; |
|
15
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
16
|
|
|
use yii\web\Controller; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
* |
|
22
|
|
|
* @property Controller $controller |
|
23
|
|
|
*/ |
|
24
|
|
|
class Authorize extends Action |
|
25
|
|
|
{ |
|
26
|
|
|
use LookupTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param int $id |
|
30
|
|
|
* @return AbstractProvider |
|
31
|
|
|
* @throws \yii\base\InvalidConfigException |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function find(int $id) |
|
34
|
|
|
{ |
|
35
|
|
|
return (new ProviderQuery()) |
|
36
|
|
|
->id($id) |
|
37
|
|
|
->one(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
* @throws \yii\base\InvalidConfigException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function run(int $id) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$object = $this->find($id)) { |
|
47
|
|
|
return $this->handleNotFoundResponse(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Save provider to session |
|
51
|
|
|
Patron::getInstance()->getSession()->setProvider( |
|
52
|
|
|
$id |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
return $this->runInternal($object, $this->getScopes()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param AbstractProvider $provider |
|
60
|
|
|
* @param array $scopes |
|
61
|
|
|
* @return mixed|\yii\web\Response |
|
62
|
|
|
* @throws \yii\web\HttpException |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function runInternal(AbstractProvider $provider, array $scopes) |
|
65
|
|
|
{ |
|
66
|
|
|
$request = Craft::$app->getRequest(); |
|
67
|
|
|
|
|
68
|
|
|
// Set redirect |
|
69
|
|
|
Patron::getInstance()->getSession()->setRedirectUrl( |
|
70
|
|
|
$request->getParam('redirect', $request->getReferrer()) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
// Check access |
|
74
|
|
|
if (($access = $this->checkAccess($provider, $scopes)) !== true) { |
|
75
|
|
|
return $access; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->performAction($provider, $scopes); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getScopes(): array |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$scopes = Craft::$app->getRequest()->getParam('scope')) { |
|
87
|
|
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $scopes; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param AbstractProvider $provider |
|
95
|
|
|
* @param array $scopes |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
* @throws \yii\web\HttpException |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function performAction(AbstractProvider $provider, array $scopes) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->handleExceptions(function () use ($provider, $scopes) { |
|
102
|
|
|
$options = []; |
|
103
|
|
|
|
|
104
|
|
|
if (!empty($scopes)) { |
|
105
|
|
|
$options['scope'] = $scopes; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Get url (this ensure all params ... such as state) |
|
109
|
|
|
$authorizationUrl = $provider->getAuthorizationUrl($options); |
|
110
|
|
|
|
|
111
|
|
|
// Save state to session |
|
112
|
|
|
Patron::getInstance()->getSession()->setState( |
|
113
|
|
|
$provider->getState() |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
return $this->controller->redirect($authorizationUrl); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|