-
Notifications
You must be signed in to change notification settings - Fork 0
/
grantedbyme.ajax.php
244 lines (231 loc) · 9.26 KB
/
grantedbyme.ajax.php
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
<?php
// load composer class loader
require_once('vendor/autoload.php');
// load wordpress functions
if (!function_exists('get_user_by')) {
include('../../../wp-config.php');
}
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$ajax = new GrantedByMeWPAjax();
/**
* GrantedByMe WordPress Plug-In AJAX
*
* PHP version 5
*
* @category PlugIn
* @package GBM
* @author GrantedByMe <info@grantedby.me>
* @access private
* @license https://grantedby.me/licenses/php/license.md MIT
* @version Release: <release_id>
* @link https://grantedby.me
*/
class GrantedByMeWPAjax
{
private $log;
/**
* Constructor
*/
function __construct()
{
$this->log = new Logger('GrantedByMeWPAjax');
$this->log->pushHandler(new StreamHandler(__DIR__ . '/data/app.log', Logger::INFO));
$headers = getallheaders();
$csrf_token = false;
if (isset($headers['X-CSRFToken'])) {
$csrf_token = $headers['X-CSRFToken'];
} else if(isset($headers['x-csrftoken'])) {
$csrf_token = $headers['x-csrftoken'];
} else {
$this->gbm_error('Missing CSRF token');
}
if (!wp_verify_nonce($csrf_token, 'csrf-token')) {
$this->gbm_error('Invalid CSRF token: ' . $csrf_token);
}
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$this->gbm_error('Invalid AJAX request');
}
if (!isset($_POST['operation']) || !isset($_POST['challenge_type'])) {
$this->gbm_error('Missing operation or challenge_type parameter');
}
$operation = $_POST['operation'];
$challenge_type = intval($_POST['challenge_type']);
if ($operation == 'getChallengeState' && (!isset($_POST['challenge']) || !is_string($_POST['challenge']) || empty($_POST['challenge']))) {
$this->gbm_error('Invalid parameter');
}
if ($operation == 'getChallenge' && $challenge_type == \GBM\ApiRequest::$CHALLENGE_AUTHORIZE && !is_user_logged_in()) {
$this->gbm_error('Invalid login state for account linking');
}
// call api
if ($operation == 'getChallenge') {
$response = GrantedByMeWP::init_sdk()->getChallenge($challenge_type);
die(json_encode($response));
} else if ($operation == 'getChallengeState') {
if($challenge_type == \GBM\ApiRequest::$CHALLENGE_AUTHORIZE) {
$this->gbm_get_account_state();
} else if($challenge_type == \GBM\ApiRequest::$CHALLENGE_AUTHENTICATE) {
$this->gbm_get_session_state();
} else if($challenge_type == \GBM\ApiRequest::$CHALLENGE_PROFILE) {
$this->gbm_get_register_state();
} else {
$this->gbm_error('Invalid challenge type');
}
} else {
$this->gbm_error('Invalid operation');
}
}
/**
* TBD
*
* @throws \GBM\ApiRequestException
*/
private function gbm_get_account_state()
{
$response = GrantedByMeWP::init_sdk()->getChallengeState($_POST['challenge']);
if (isset($response['status']) && $response['status'] == \GBM\ApiRequest::$STATUS_VALIDATED) {
$authenticator_secret = \GBM\ApiRequest::generateAuthenticatorSecret();
$result = GrantedByMeWP::init_sdk()->linkAccount($_POST['challenge'], $authenticator_secret);
if (isset($result['success']) && $result['success'] == true) {
$user_id = get_current_user_id();
$options = get_option('grantedbyme_option_name');
$options['users'][$user_id] = $authenticator_secret;
$is_saved = update_option('grantedbyme_option_name', $options);
} else {
$this->log->addInfo('User account link error: ' . $result['error_message'] . ' (' . $result['error'] . ')');
$_SESSION['gbm_form_error'] = 'api_error';
$_SESSION['gbm_form_error_code'] = $result['error'];
$_SESSION['gbm_form_error_message'] = $result['error_message'];
}
}
die(json_encode($response));
}
/**
* TBD
*
* @throws \GBM\ApiRequestException
*/
private function gbm_get_session_state()
{
$response = GrantedByMeWP::init_sdk()->getChallengeState($_POST['challenge']);
if (isset($response['status']) && $response['status'] == \GBM\ApiRequest::$STATUS_VALIDATED) {
if (isset($response['authenticator_secret'])) {
$_SESSION['gbm_challenge'] = $_POST['challenge'];
$this->gbm_login($response['authenticator_secret']);
// do not send secret to frontend
unset($response['authenticator_secret']);
} else {
header('HTTP/1.0 401 Unauthorized');
$this->gbm_error('Login error with empty authenticator secret');
}
}
die(json_encode($response));
}
/**
* TBD
*
* @throws \GBM\ApiRequestException
*/
private function gbm_get_register_state()
{
$response = GrantedByMeWP::init_sdk()->getChallengeState($_POST['challenge']);
if (isset($response['status']) && $response['status'] == \GBM\ApiRequest::$STATUS_VALIDATED) {
if (isset($response['data'])) {
$this->gbm_register($response['data']);
// do not send secret to frontend
unset($response['data']);
} else {
$this->gbm_error('Register error with empty data');
}
}
die(json_encode($response));
}
/**
* Register WordPress user
*
* @param $data
*/
private function gbm_register($data)
{
if (!empty($data)) {
$this->log->addInfo('Register', $data);
$count_data = count_users();
$total_users = $count_data['total_users'] + 1;
$user_login = 'gbm_user_' . $total_users;
$user_pass = \GBM\ApiCrypto::randomString(16);
$userdata = array(
'user_email' => $data['email'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'user_login' => $user_login,
'user_pass' => $user_pass
);
$user_id = wp_insert_user($userdata) ;
if (!is_wp_error($user_id)) {
$this->log->addInfo('User created: ' . $user_id);
$authenticator_secret = \GBM\ApiRequest::generateAuthenticatorSecret();
$result = GrantedByMeWP::init_sdk()->linkAccount($_POST['challenge'], $authenticator_secret);
if (isset($result['success']) && $result['success'] == true) {
$options = get_option('grantedbyme_option_name');
$options['users'][$user_id] = $authenticator_secret;
$is_saved = update_option('grantedbyme_option_name', $options);
$this->log->addInfo('User account linked: ' . $user_id);
$this->gbm_login($authenticator_secret);
$_SESSION['gbm_registration_completed'] = true;
} else {
$this->log->addInfo('User account link error: ' . $result['error_message'] . ' (' . $result['error'] . ')');
$_SESSION['gbm_form_error'] = 'api_error';
$_SESSION['gbm_form_error_code'] = $result['error'];
$_SESSION['gbm_form_error_message'] = $result['error_message'];
}
} else {
$this->log->addInfo('User registration error: ' . $user_id->get_error_message());
$_SESSION['gbm_form_error'] = 'registration_error';
$_SESSION['gbm_form_error_message'] = $user_id->get_error_message();
}
} else {
$this->gbm_error('Empty registration data');
}
}
/**
* Log-in WordPress user
*
* @param $authenticator_secret
*/
private function gbm_login($authenticator_secret)
{
if (!empty($authenticator_secret)) {
$options = get_option('grantedbyme_option_name');
if(isset($options['users'])) {
$user_id = array_search($authenticator_secret, $options['users'], true);
$user = get_user_by('id', esc_attr($user_id));
} else {
$user = false;
}
if ($user) {
$this->log->addInfo('Login success with user_id: ' . $user_id);
wp_set_current_user($user->ID, $user->user_login);
wp_set_auth_cookie($user->ID);
do_action('wp_login', $user->user_login);
} else {
header('HTTP/1.0 401 Unauthorized');
$this->gbm_error('Login error: user not found');
}
} else {
$this->gbm_error('Login error: empty authentication secret');
}
}
/**
* Generic error handler
*
* @param $reason
*/
private function gbm_error($reason)
{
$this->log->addInfo($reason);
$response = array();
$response['success'] = false;
$response['error'] = 1;
die(json_encode($response));
}
}