Controller.php
12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace Wpo\Graph;
// Prevent public access to this script
defined('ABSPATH') or die();
use \Wpo\Core\Permissions_Helpers;
use \Wpo\Graph\Request;
use \Wpo\Services\Access_Token_Service;
use \Wpo\Services\Authentication_Service;
use \Wpo\Services\Log_Service;
use \Wpo\Services\Options_Service;
if (!class_exists('\Wpo\Graph\Controller')) {
class Controller extends \WP_REST_Controller
{
/**
* Register the routes for the objects of the controller.
*/
public function register_routes()
{
$version = '1';
$namespace = 'wpo365/v' . $version . '/graph';
register_rest_route(
$namespace,
'/user',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => '\Wpo\Graph\Current_User::get_current_user',
'permission_callback' => array($this, 'check_permissions_current_user'),
),
)
);
register_rest_route(
$namespace,
'/users',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/users');
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true);
}
),
)
);
register_rest_route(
$namespace,
'/myorganization',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/myorganization');
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true);
}
),
)
);
register_rest_route(
$namespace,
'/me',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/me');
},
'permission_callback' => array($this, 'check_permissions'),
),
)
);
register_rest_route(
$namespace,
'/groups',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/groups');
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true);
}
),
)
);
register_rest_route(
$namespace,
'/drives',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/drives');
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true);
}
),
)
);
register_rest_route(
$namespace,
'/sites',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::get($request, '/sites');
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true);
}
),
)
);
register_rest_route(
$namespace,
'/proxy',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::proxy($request);
},
'permission_callback' => function ($request) {
return $this->check_permissions($request, true, true);
}
),
)
);
register_rest_route(
$namespace,
'/token',
array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => function ($request) {
return Request::token($request);
},
'permission_callback' => function ($request) {
return $this->check_permissions_get_token($request);
}
),
)
);
}
/**
* Checks if the app is allowed to access the API.
*
* @param string $scope Scope for which the token must be valid.
* @return bool|WP_Error True if user can retrieve an access token for the requested scope otherwise a WP_Error is returned.
*/
public function check_permissions($request, $allow_application = false, $check_proxy = false)
{
Log_Service::write_log('DEBUG', '##### -> ' . __METHOD__);
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
return new \WP_Error('UnauthorizedException', 'The request cannot be validated.', array('status' => 401));
}
$body = $request->get_json_params();
if ($check_proxy && !Options_Service::get_global_boolean_var('enable_graph_proxy')) {
$url = !empty($body['url']) ? $body['url'] : '';
return new \WP_Error('ForbiddenException', sprintf('This type of request is not allowed [proxy request for %s]. Go to WP Admin > WPO365 > Integration and \'Allow Microsoft Graph proxy-type requests\'.', $url), array('status' => 403));
}
// User signed in with Microsoft but the request is malformed
if (empty($body) || !\is_array($body) || empty($body['scope'])) {
return new \WP_Error('InvalidArgumentException', 'No (Microsoft Graph permission) scope has been defined. Possibly the request body is malformed or the request header did not define the Content-type as application/json.', array('status' => 400));
}
$wp_usr_id = \get_current_user_id();
$graph_permission_level = self::get_graph_permission_level($body['scope']);
if ($graph_permission_level == 'signedInWithMicrosoft') {
$wpo_auth_value = get_user_meta($wp_usr_id, Authentication_Service::USR_META_WPO365_AUTH, true);
// To get a delegated oauth token the user must have signed in with Microsoft
if ($wp_usr_id === 0 || empty($wpo_auth_value)) {
return new \WP_Error('UnauthorizedException', 'Please sign in with Microsoft first before using this API.', array('status' => 401));
}
// If administrator configured a session duration then check if wpo_auth is expired
if (Options_Service::get_global_numeric_var('session_duration') > 0) {
$wpo_auth = json_decode($wpo_auth_value);
if (!isset($wpo_auth->expiry) || $wpo_auth->expiry < time()) {
return new \WP_Error('UnauthorizedException', 'Your session is expired. Please sign in with Microsoft again before using this API.', array('status' => 401));
}
}
// User signed in with Microsoft
return true;
} elseif ($graph_permission_level == 'signedIn') {
// User is not logged in
if ($wp_usr_id === 0) {
return new \WP_Error('UnauthorizedException', 'Please sign in first before using this API.', array('status' => 401));
}
// User is logged in
return true;
} elseif ($graph_permission_level == 'anonymous') {
// The app does not require a logged-in user
return true;
}
return new \WP_Error('UnauthorizedException', 'The server received an unauthenticated request.', array('status' => 401));
}
/**
* Checks if the app may retrieve a (delegated) oauth access token.
*
* @since 17.0
*
* @return bool|WP_Error True if user can retrieve an access token for the requested scope otherwise a WP_Error is returned.
*/
public function check_permissions_get_token($request)
{
Log_Service::write_log('DEBUG', '##### -> ' . __METHOD__);
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
return new \WP_Error('UnauthorizedException', 'The request cannot be validated.', array('status' => 401));
}
$body = $request->get_json_params();
$wp_usr_id = \get_current_user_id();
if (
false == Options_Service::get_global_boolean_var('graph_allow_token_retrieval')
&& false == Options_Service::get_global_boolean_var('graph_allow_get_token')
) {
return new \WP_Error('ForbiddenException', 'This type of request is not allowed [token request]. Go to WP Admin > WPO365 > Integration and allow \'Apps may request (delegated) oauth access tokens\'.', array('status' => 403));
}
$wpo_auth_value = get_user_meta($wp_usr_id, Authentication_Service::USR_META_WPO365_AUTH, true);
// To get a delegated oauth token the user must have signed in with Microsoft
if ($wp_usr_id === 0 || empty($wpo_auth_value)) {
return new \WP_Error('UnauthorizedException', 'Please sign in with Microsoft first before using this API.', array('status' => 401));
}
// If administrator configured a session duration then check if wpo_auth is expired
if (Options_Service::get_global_numeric_var('session_duration') > 0) {
$wpo_auth = json_decode($wpo_auth_value);
if (!isset($wpo_auth->expiry) || $wpo_auth->expiry < time()) {
return new \WP_Error('UnauthorizedException', 'Your session is expired. Please sign in with Microsoft again before using this API.', array('status' => 401));
}
}
return true;
}
/**
* Checks if a user is logged in.
*/
public function check_permissions_current_user()
{
$wp_usr_id = \get_current_user_id();
if ($wp_usr_id === 0) {
return new \WP_Error('UnauthorizedException', 'Please sign in first before using this API.', array('status' => 401));
}
return true;
}
/**
* Get the Graph Permission Level defined by the administrator and verifies it against the requested scope.
*
* @since 17.0
*
* @param string $scope The scope requested by the app.
* @return string The verified Graph Permission Level.
*/
private static function get_graph_permission_level($scope)
{
$graph_permission_level = Options_Service::get_global_string_var('graph_permission_level');
if (empty($graph_permission_level) || $graph_permission_level == 'signedInWithMicrosoft') {
return 'signedInWithMicrosoft';
}
// The following scopes require a user who signed in with Microsoft
if (true === Permissions_Helpers::must_use_delegate_access_for_scope($scope)) {
return 'signedInWithMicrosoft';
}
return $graph_permission_level;
}
}
}