Issues (1358)

modules/OAuth2/token.php (2 issues)

1
<?php
2
/**
3
 * @package  OAuth2
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs\modules\OAuth2;
9
10
use
11
	cs\Config,
12
	cs\ExitException,
13
	cs\Page,
14
	cs\Response;
15
16
Response::instance()
17
	->header('cache-control', 'no-store')
0 ignored issues
show
The method header() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
	->/** @scrutinizer ignore-call */ header('cache-control', 'no-store')
Loading history...
18
	->header('pragma', 'no-cache');
19
/**
20
 * Errors processing
21
 */
22
if (!isset($_POST['grant_type'])) {
23
	$e = new ExitException(
24
		[
25
			'invalid_request',
26
			'grant_type parameter required'
27
		],
28
		400
29
	);
30
	$e->setJson();
31
	throw $e;
32
}
33
if (!isset($_POST['client_id'])) {
34
	$e = new ExitException(
35
		[
36
			'invalid_request',
37
			'client_id parameter required'
38
		],
39
		400
40
	);
41
	$e->setJson();
42
	throw $e;
43
}
44
if (!isset($_POST['client_secret'])) {
45
	$e = new ExitException(
46
		[
47
			'invalid_request',
48
			'client_secret parameter required'
49
		],
50
		400
51
	);
52
	$e->setJson();
53
	throw $e;
54
}
55
$OAuth2 = OAuth2::instance();
56
$client = $OAuth2->get_client($_POST['client_id']);
57
if (!$client) {
0 ignored issues
show
The condition $client is always false.
Loading history...
58
	$e = new ExitException(
59
		[
60
			'access_denied',
61
			'Invalid client id'
62
		],
63
		400
64
	);
65
	$e->setJson();
66
	throw $e;
67
}
68
if (!$client['active']) {
69
	$e = new ExitException(
70
		[
71
			'access_denied',
72
			'Inactive client id'
73
		],
74
		400
75
	);
76
	$e->setJson();
77
	throw $e;
78
}
79
if ($_POST['client_secret'] != $client['secret']) {
80
	$e = new ExitException(
81
		[
82
			'access_denied',
83
			'client_secret do not corresponds client_id'
84
		],
85
		400
86
	);
87
	$e->setJson();
88
	throw $e;
89
}
90
if (!$client['domain']) {
91
	$e = new ExitException(
92
		[
93
			'unauthorized_client',
94
			'Request method is not authored'
95
		],
96
		400
97
	);
98
	$e->setJson();
99
	throw $e;
100
}
101
$Config = Config::instance();
102
$Page   = Page::instance();
103
/**
104
 * Tokens operations processing
105
 */
106
switch ($_POST['grant_type']) {
107
	case 'authorization_code':
108
		if (!isset($_POST['redirect_uri'])) {
109
			$e = new ExitException(
110
				[
111
					'invalid_request',
112
					'redirect_uri parameter required'
113
				],
114
				400
115
			);
116
			$e->setJson();
117
			throw $e;
118
		} elseif (
119
			urldecode($_POST['redirect_uri']) != $Config->base_url().'/OAuth2/blank/' &&
120
			!preg_match("#^[^/]+://$client[domain]#", urldecode($_POST['redirect_uri']))
121
		) {
122
			$e = new ExitException(
123
				[
124
					'invalid_request',
125
					'Invalid redirect_uri parameter'
126
				],
127
				400
128
			);
129
			$e->setJson();
130
			throw $e;
131
		}
132
		if (!isset($_POST['code'])) {
133
			$e = new ExitException(
134
				[
135
					'invalid_request',
136
					'code parameter required'
137
				],
138
				400
139
			);
140
			$e->setJson();
141
			throw $e;
142
		}
143
		$token_data = $OAuth2->get_code($_POST['code'], $client['id'], $client['secret'], urldecode($_POST['redirect_uri']));
144
		if (!$token_data) {
145
			$e = new ExitException(
146
				[
147
					'access_denied',
148
					"Server can't get token data, check parameters and try again"
149
				],
150
				403
151
			);
152
			$e->setJson();
153
			throw $e;
154
		}
155
		if ($token_data['expires_in'] < 0) {
156
			$e = new ExitException(
157
				[
158
					'access_denied',
159
					'access_token expired'
160
				],
161
				403
162
			);
163
			$e->setJson();
164
			throw $e;
165
		}
166
		$Page->json($token_data);
167
		return;
168
	case 'refresh_token':
169
		if (!isset($_POST['refresh_token'])) {
170
			$e = new ExitException(
171
				[
172
					'invalid_request',
173
					'refresh_token parameter required'
174
				],
175
				400
176
			);
177
			$e->setJson();
178
			throw $e;
179
		}
180
		$token_data = $OAuth2->refresh_token($_POST['refresh_token'], $client['id'], $client['secret']);
181
		if (!$token_data) {
182
			$e = new ExitException(
183
				[
184
					'access_denied',
185
					'User session invalid'
186
				],
187
				403
188
			);
189
			$e->setJson();
190
			throw $e;
191
		}
192
		$Page->json($token_data);
193
		return;
194
	default:
195
		$e = new ExitException(
196
			[
197
				'unsupported_grant_type',
198
				'Specified grant type is not supported, only "authorization_code" or "refresh_token" types available'
199
			],
200
			400
201
		);
202
		$e->setJson();
203
		throw $e;
204
}
205