|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Oauth |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\oauth\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use gplcart\core\controllers\frontend\Controller; |
|
14
|
|
|
use gplcart\modules\oauth\models\Oauth as OauthModel; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use OutOfRangeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handles incoming requests and outputs data related to Oauth functionality |
|
20
|
|
|
*/ |
|
21
|
|
|
class Oauth extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Oauth model instance |
|
26
|
|
|
* @var \gplcart\modules\oauth\models\Oauth $oauth |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $oauth; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The current Oauth provider |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $data_provider; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* A code received from a provider |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $data_code; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* A state hash received from a provider |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $data_state; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* An array of data parsed from a received state |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $data_decoded_state; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* An array of token data |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $data_token; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* A processed authorization result |
|
62
|
|
|
* @var mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $data_result; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param OauthModel $oauth |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(OauthModel $oauth) |
|
70
|
|
|
{ |
|
71
|
|
|
parent::__construct(); |
|
72
|
|
|
|
|
73
|
|
|
$this->oauth = $oauth; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Callback for Oauth returning URL |
|
78
|
|
|
*/ |
|
79
|
|
|
public function callbackOauth() |
|
80
|
|
|
{ |
|
81
|
|
|
try { |
|
82
|
|
|
$this->setResponseOauth(); |
|
83
|
|
|
$this->setTokenOauth(); |
|
84
|
|
|
$this->setResultOauth(); |
|
85
|
|
|
$this->redirectOauth(); |
|
86
|
|
|
} catch (Exception $ex) { |
|
87
|
|
|
trigger_error($ex->getMessage()); |
|
88
|
|
|
$this->outputHttpStatus(403); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set and validates received data from Oauth provider |
|
95
|
|
|
* @throws InvalidArgumentException |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function setResponseOauth() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->data_code = $this->getQuery('code', ''); |
|
100
|
|
|
$this->data_state = $this->getQuery('state', ''); |
|
101
|
|
|
$this->data_decoded_state = $this->oauth->decodeState($this->data_state); |
|
102
|
|
|
$this->data_provider = $this->oauth->getProvider($this->data_decoded_state['id']); |
|
103
|
|
|
|
|
104
|
|
|
if (!$this->oauth->isValidState($this->data_state, $this->data_decoded_state['id'])) { |
|
105
|
|
|
throw new InvalidArgumentException('Invalid state code'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$domain = parse_url($this->data_decoded_state['url'], PHP_URL_HOST); |
|
109
|
|
|
|
|
110
|
|
|
if (empty($domain)) { |
|
111
|
|
|
throw new InvalidArgumentException('Unknown redirect domain'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$store = $this->store->get($domain); |
|
115
|
|
|
|
|
116
|
|
|
if (empty($store['status'])) { |
|
117
|
|
|
throw new InvalidArgumentException('Invalid redirect domain'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Does final redirect after authorization |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function redirectOauth() |
|
125
|
|
|
{ |
|
126
|
|
|
if (isset($this->data_result['message'])) { |
|
127
|
|
|
$this->setMessage($this->data_result['message'], $this->data_result['severity'], true); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if (isset($this->data_result['redirect'])) { |
|
131
|
|
|
$this->redirect($this->data_result['redirect']); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->redirect($this->data_decoded_state['url']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set received token data |
|
139
|
|
|
* @throws OutOfRangeException |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function setTokenOauth() |
|
142
|
|
|
{ |
|
143
|
|
|
$query = $this->oauth->getQueryToken($this->data_provider, array('code' => $this->data_code)); |
|
144
|
|
|
$this->data_token = $this->oauth->exchangeToken($this->data_provider, $query); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Set authorization result |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function setResultOauth() |
|
151
|
|
|
{ |
|
152
|
|
|
if (empty($this->data_token['access_token'])) { |
|
153
|
|
|
throw new OutOfRangeException('Empty Oauth access token'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->data_result = $this->oauth->authorize($this->data_provider, array('token' => $this->data_token)); |
|
157
|
|
|
|
|
158
|
|
|
$this->data_result += array( |
|
159
|
|
|
'severity' => 'warning', |
|
160
|
|
|
'message' => $this->text('An error occurred') |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|