forked from usefulteam/jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-devices.php
489 lines (406 loc) · 12.4 KB
/
class-devices.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* Devices JWT-Auth.
*
* @package jwt-auth
*/
namespace JWTAuth;
/**
* Display the devices connected with token and let remove them in user profile page.
* Developed by Rodrigo M. Souza https://github.com/pesseba
*/
class Devices {
/**
* Setup action & filter hooks.
*/
public function __construct() {
add_action( 'show_user_profile', array( $this, 'custom_user_profile_fields' ), 10, 1 );
add_action( 'edit_user_profile', array( $this, 'custom_user_profile_fields' ), 10, 1 );
add_action( 'wp_ajax_remove_device', array( $this, 'remove_device' ) );
add_shortcode( 'jwt_auth_devices', array( $this, 'shortcode_jwt_auth_devices' ) );
add_action( 'profile_update', array( $this, 'profile_update' ), 10, 2 );
add_action( 'after_password_reset', array( $this, 'after_password_reset' ), 10, 2 );
add_action( 'user_register', array( $this, 'after_user_creation' ), 10, 1 );
add_filter( 'jwt_auth_payload', array( $this, 'jwt_auth_payload' ), 10, 2 );
add_filter( 'jwt_auth_extra_token_check', array( $this, 'check_device_and_pass' ), 10, 4 );
}
/**
* Filter payload to add device and pass.
*
* @param array $payload The token's payload.
* @param WP_User $user The user who owns the token.
*
* @return array $payload The modified token's payload.
*/
public function jwt_auth_payload( $payload, $user ) {
$current_device = isset( $_POST['device'] ) ? $this->sanitize_device_name( sanitize_text_field( $_POST['device'] ) ) : ''; // phpcs:ignore
// Add device identyfier in user meta if parameter was passed.
// TODO: considering to use $_SERVER['HTTP_USER_AGENT'] as default value for device in case it is empty.
if ( ! empty( $current_device ) ) {
$all_devices = get_user_meta( $user->ID, 'jwt_auth_device', false );
if ( empty( $all_devices ) || ! in_array( $current_device, $all_devices, true ) ) {
$data = array(
'agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : '', // phpcs:ignore
'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ),
'is_mobile' => wp_is_mobile(),
);
add_user_meta( $user->ID, 'jwt_auth_device', $current_device, false );
add_user_meta( $user->ID, $this->sanitize_device_key( $current_device ), $data, true );
}
}
// Add a pass if user doesn't have yet.
$pass = get_user_meta( $user->ID, 'jwt_auth_pass', true );
$pass = ( empty( $pass ) ) ? $this->refresh_pass( $user->ID ) : $pass;
$pass = apply_filters( 'jwt_auth_pass', $pass );
$payload['data']['user']['device'] = $current_device;
$payload['data']['user']['pass'] = $pass;
return $payload;
}
/**
* Filter token validation to check device and pass.
*
* @param string $error_msg The failed message.
* @param WP_User $user The user who owns the token.
* @param string $token The token.
* @param array $payload The token's payload.
*
* @return string The error message if failed, empty string if it passes.
*/
public function check_device_and_pass( $error_msg, $user, $token, $payload ) {
// Check if token has device filled.
if ( ! empty( $payload->data->user->device ) ) {
$all_devices = get_user_meta( $user->ID, 'jwt_auth_device', false );
if ( ! is_array( $all_devices ) || ! in_array( $payload->data->user->device, $all_devices, true ) ) {
return 'device unnabled';
}
}
// Check if user changed the password.
$pass = get_user_meta( $user->ID, 'jwt_auth_pass', true );
if ( $payload->data->user->pass !== $pass ) {
return 'password changed';
}
return '';
}
/**
* Sanitize the device name.
*
* @param string $device The device name.
* @return string The sanitized device name.
*/
private function sanitize_device_name( $device ) {
$unwanted_chars = array(
'Š' => 'S',
'š' => 's',
'Ž' => 'Z',
'ž' => 'z',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'A',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'Ø' => 'O',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ý' => 'Y',
'Þ' => 'B',
'ß' => 'Ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'a',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ý' => 'y',
'þ' => 'b',
'ÿ' => 'y',
);
$device = strtr( $device, $unwanted_chars );
$device = preg_replace( '/[^a-z0-9 ]/i', '', $device );
return $device;
}
/**
* Sanitize the device key.
*
* @param string $key The device key.
* @return string The sanitized device key.
*/
private function sanitize_device_key( $key ) {
return 'jwt_auth_device_' . str_replace( ' ', '_', $this->sanitize_device_name( $key ) );
}
/**
* Fires immediately after an existing user is updated.
*
* @since 2.0.0
*
* @param int $user_id User ID.
* @param WP_User $old_user_data Object containing user's data prior to update.
*/
public function profile_update( $user_id, $old_user_data ) {
$user = get_user_by( 'id', $user_id );
if ( $user->user_pass !== $old_user_data->user_pass ) {
$this->block_all_tokens( $user_id );
}
}
/**
* Fires after the user's password is reset.
*
* @since 4.4.0
*
* @param WP_User $user The user.
* @param string $new_pass New user password.
*/
public function after_password_reset( $user, $new_pass ) {
$this->block_all_tokens( $user->ID );
}
/**
* Fires after the user' is created
*
* @since 2.0.0
*
* @param int $user_id The user ID.
*/
public function after_user_creation( $user_id ) {
$this->refresh_pass( $user_id );
}
/**
* Block all access tokens.
*
* @param int $user_id The user id.
*/
private function block_all_tokens( $user_id ) {
// Clear devices list with access.
delete_user_meta( $user_id, 'jwt_auth_device' );
global $wpdb;
// ! Can we not using a direct database call? Because it is discouraged in wpcs.
// This is because performance. The key jwt_auth_device_% is has generic key name, so this is necessary. The query uses prepare() to avoid insections anyway
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key LIKE %s",
$user_id,
'jwt_auth_device_%'
)
);
// Add a hash for the new password.
$this->refresh_pass( $user_id );
}
/**
* Refresh the pass value in user meta.
*
* @param int $user_id The user id.
*/
private function refresh_pass( $user_id ) {
$pass = (string) md5( uniqid( wp_rand(), true ));
if(!empty(update_user_meta( $user_id, 'jwt_auth_pass', $pass ) )){
return $pass;
}
return '';
}
// -------------------------------------------------------------------------------------------------------
/**
* Remove the device from token access.
*/
public function remove_device() {
$nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$device = isset( $_POST['device'] ) ? sanitize_text_field( $_POST['device'] ) : ''; // phpcs:ignore
$user_id = isset( $_POST['user_id'] ) && is_numeric( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0; // phpcs:ignore
if(!wp_verify_nonce( $nonce, 'jwt_auth_remove_device_'.$user_id)){
wp_send_json_error();
wp_die();
return;
}
if ( delete_user_meta( $user_id, 'jwt_auth_device', $device ) ) {
delete_user_meta( $user_id, $this->sanitize_device_key( $device ) );
wp_send_json_success();
} else {
wp_send_json_error();
}
wp_die();
}
/**
* Show custom user profile fields.
*
* @param WP_User $profileuser The current WP_User object.
*/
public function custom_user_profile_fields( $profileuser ) {
// If is current user's profile (profile.php).
if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE ) {
$user_id = get_current_user_id();
} elseif ( ! empty( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ) { // phpcs:ignore
// If is another user's profile page.
$user_id = absint( $_GET['user_id'] ); // phpcs:ignore
} else {
// Otherwise something is wrong.
die( 'No user id defined.' );
}
?>
<h2><?php echo __( 'Connected Devices', 'jwt-auth' ); ?></h2>
<div id="jwt_auth_devices" style="width:33%">
<?php echo do_shortcode( '[jwt_auth_devices user_id=' . $user_id . ']' ); ?>
</div>
<?php
}
/**
* Shortcode to display the user ranking position.
*
* @param array $atts The shortcode attributes.
*/
public function shortcode_jwt_auth_devices( $atts ) {
if ( ! is_admin() ) {
return '';
}
$atts = shortcode_atts(
array(
'user_id' => get_current_user_id(),
),
$atts,
'jwt_auth_devices'
);
$user_id = absint( $atts['user_id'] );
if ( get_current_user_id() !== $user_id ) {
if ( ! current_user_can( 'administrator' ) ) {
return '';
}
}
$devices = get_user_meta( $user_id, 'jwt_auth_device', false );
if ( count( $devices ) === 0 ) {
return '<p>' . __( 'You have no devices connected', 'jwt-auth' ) . '</p>';
}
ob_start();
?>
<style>
.device_area {margin: 0 -5px;}
.device_area:after {
content: "";
display: table;
clear: both;
}
.device_column {
float: left;
width: 200px;
padding: 10px 10px;
box-sizing: border-box;
}
.device_card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
}
.device_title {
font-size:1vw;
}
.device_date {
font-size:0.7vw;
}
.device_agent {
font-size:0.5vw;
}
</style>
<script type="text/javascript">
function jwt_auth_remove_device(user_id, device_name, index) {
if (confirm('Are you sure you want to remove this device access?')) {
var totalDevices = <?php echo count( $devices ); ?>;
for (var i=0; i < totalDevices; i++) {
var btn = document.getElementById("jwt_auth_remove_button-"+i);
if(btn!=null) { btn.disabled=true; }
}
var data = {
'action': 'remove_device',
'user_id': user_id,
'device': device_name,
'nonce': '<?php echo wp_create_nonce('jwt_auth_remove_device_'.$user_id ); ?>',
};
jQuery.post(ajaxurl, data, function(response) {
if (response['success'] == true) {
var elem = document.getElementById("jwt_auth_device-"+index);
elem.parentNode.removeChild(elem);
for(var i=0; i < totalDevices; i++){
var btn = document.getElementById("jwt_auth_remove_button-"+i);
if(btn!=null) { btn.disabled=false; }
}
} else {
alert("<?php echo __( "Ops... device couldn't be removed!", 'jwt-auth' ); ?>");
}
});
}
}
</script>
<div id="jwt_auth_devices" class="device_area">
<?php
$line = false;
$total_devices = count( $devices );
for ( $i = 0; $i < $total_devices; ++$i ) {
$device = $devices[ $i ];
$title = preg_replace( '/(\S{15})(?=\S)/', '$1 ', $device );
$title = ( strlen( $title ) > 30 ) ? substr( $title, 0, 27 ) . '...' : $title;
$device_data = (array) get_user_meta( $user_id, $this->sanitize_device_key( $device ), true );
$icon = ( $device_data['is_mobile'] ) ? 'dashicons-smartphone' : 'dashicons-laptop';
$date = $device_data['date'];
$agent = $device_data['agent'];
$agent = preg_replace( '/(\S{15})(?=\S)/', '$1 ', $agent );
?>
<div class="device_column" id="jwt_auth_device-<?php echo esc_attr( $i ); ?>">
<div class="device_card">
<span class="dashicons <?php echo esc_attr( $icon ); ?>" style="font-size:28px; color:grey;" ></span>
<p class="device_title"><h3><?php echo esc_html( $title ); ?></h3></p>
<p class="device_date"><?php echo esc_html( $date ); ?></p>
<p class="device_agent"><?php echo esc_html( $agent ); ?></p>
<?php
echo '
<input id="jwt_auth_remove_button-' . esc_attr( $i ) .
'" class="button wp-generate-pw' .
'" type="button" value="' . __( 'Remove', 'jwt-auth' ) .
'" onclick="jwt_auth_remove_device(\'' . esc_attr( $user_id ) . '\',\'' . esc_attr( $device ) . '\',\'' . esc_attr( $i ) . '\' )" />
';
?>
</div>
</div>
<?php
}
?>
</div>
</br>
<?php
return ob_get_clean();
}
}