forked from atmshop/authy-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
392 lines (360 loc) · 16.8 KB
/
helpers.php
File metadata and controls
392 lines (360 loc) · 16.8 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
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
<?php
/**
* Header for authy pages
*/
function authy_header( $step = '' ) { ?>
<head>
<?php
global $wp_version;
if ( version_compare( $wp_version, '3.3', '<=' ) ) {?>
<link rel="stylesheet" type="text/css" href="<?php echo admin_url( 'css/login.css' ); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo admin_url( 'css/colors-fresh.css' ); ?>" />
<?php
} else {
?>
<link rel="stylesheet" type="text/css" href="<?php echo admin_url( 'css/wp-admin.css' ); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo includes_url( 'css/buttons.css' ); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo admin_url( 'css/colors-fresh.css' ); ?>" />
<?php
}
?>
<link href="https://www.authy.com/form.authy.min.css" media="screen" rel="stylesheet" type="text/css">
<script src="https://www.authy.com/form.authy.min.js" type="text/javascript"></script>
<?php if ( $step == 'verify_installation' ) { ?>
<link href="<?php echo plugins_url( 'assets/authy.css', __FILE__ ); ?>" media="screen" rel="stylesheet" type="text/css">
<script type="text/javascript">
/* <![CDATA[ */
var AuthyAjax = {"ajaxurl":"<?php echo admin_url( 'admin-ajax.php' ); ?>"};
/* ]]> */
</script>
<script src="<?php echo admin_url( 'load-scripts.php?c=1&load=jquery,utils' ); ?>" type="text/javascript"></script>
<script src="<?php echo plugins_url( 'assets/authy-installation.js', __FILE__ ); ?>" type="text/javascript"></script>
<?php } ?>
</head>
<?php }
/**
* Generate the authy token form
* @param string $username
* @param array $user_data
* @param array $user_signature
* @return string
*/
function authy_token_form( $username, $user_data, $user_signature, $redirect ) {?>
<html>
<?php echo authy_header(); ?>
<body class='login wp-core-ui'>
<div id="login">
<h1>
<a href="http://wordpress.org/" title="Powered by WordPress"><?php echo get_bloginfo( 'name' ); ?></a>
</h1>
<h3 style="text-align: center; margin-bottom:10px;">Authy Two-Factor Authentication</h3>
<p class="message">
<?php _e( "You can get this token from the Authy mobile app. If you are not using the Authy app we've automatically sent you a token via text-message to cellphone number: ", 'authy' ); ?>
<strong>
<?php
$cellphone = normalize_cellphone( $user_data['phone'] );
$cellphone = preg_replace( "/^\d{1,3}\-/", 'XXX-', $cellphone );
$cellphone = preg_replace( "/\-\d{1,3}\-/", '-XXX-', $cellphone );
echo esc_attr( $cellphone );
?>
</strong>
</p>
<form method="POST" id="authy" action="wp-login.php">
<label for="authy_token">
<?php _e( 'Authy Token', 'authy' ); ?>
<br>
<input type="text" name="authy_token" id="authy-token" class="input" value="" size="20" autofocus="true" />
</label>
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect ); ?>"/>
<input type="hidden" name="username" value="<?php echo esc_attr( $username ); ?>"/>
<?php if ( isset( $user_signature['authy_signature'] ) && isset( $user_signature['signed_at'] ) ) { ?>
<input type="hidden" name="authy_signature" value="<?php echo esc_attr( $user_signature['authy_signature'] ); ?>"/>
<?php } ?>
<p class="submit">
<input type="submit" value="<?php echo esc_attr_e( 'Login', 'authy' ) ?>" id="wp_submit" class="button button-primary button-large" />
</p>
</form>
</div>
</body>
</html>
<?php }
/**
* Enable authy page
*
* @param mixed $user
* @return string
*/
function render_enable_authy_page( $user, $signature, $errors = array() ) {?>
<html>
<?php echo authy_header(); ?>
<body class='login wp-core-ui'>
<div id="login">
<h1><a href="http://wordpress.org/" title="Powered by WordPress"><?php echo get_bloginfo( 'name' ); ?></a></h1>
<h3 style="text-align: center; margin-bottom:10px;">Enable Authy Two-Factor Authentication</h3>
<?php
if ( !empty( $errors ) ) {
$message = '';
foreach ( $errors as $msg ) {
$message .= '<strong>ERROR: </strong>' . $msg . '<br>';
}
?><div id="login_error"><?php echo _e( $message, 'authy' ); ?></div><?php
}
?>
<p class="message"><?php _e( 'Your administrator has requested that you add Two-Factor Authentication to your account, please enter your cellphone below to enable.', 'authy' ); ?></p>
<form method="POST" id="authy" action="wp-login.php">
<label for="authy_user[country_code]"><?php _e( 'Country', 'authy' ); ?></label>
<input type="text" name="authy_user[country_code]" id="authy-countries" class="input" />
<label for="authy_user[cellphone]"><?php _e( 'Cellphone number', 'authy' ); ?></label>
<input type="tel" name="authy_user[cellphone]" id="authy-cellphone" class="input" />
<input type="hidden" name="username" value="<?php echo esc_attr( $user->user_login ); ?>"/>
<input type="hidden" name="step" value="enable_authy"/>
<input type="hidden" name="authy_signature" value="<?php echo esc_attr( $signature ); ?>"/>
<p class="submit">
<input type="submit" value="<?php echo esc_attr_e( 'Enable', 'authy' ) ?>" id="wp_submit" class="button button-primary button-large">
</p>
</form>
</div>
</body>
</html>
<?php }
/**
* Form enable authy on profile
* @param string $users_key
* @param array $user_datas
* @return string
*/
function register_form_on_profile( $users_key, $user_data ) {?>
<table class="form-table" id="<?php echo esc_attr( $users_key ); ?>">
<tr>
<th><label for="phone"><?php _e( 'Country', 'authy' ); ?></label></th>
<td>
<input type="text" id="authy-countries" class="small-text" name="<?php echo esc_attr( $users_key ); ?>[country_code]" value="<?php echo esc_attr( $user_data['country_code'] ); ?>" />
</td>
</tr>
<tr>
<th><label for="phone"><?php _e( 'Cellphone number', 'authy' ); ?></label></th>
<td>
<input type="tel" id="authy-cellphone" class="regular-text" name="<?php echo esc_attr( $users_key ); ?>[phone]" value="<?php echo esc_attr( $user_data['phone'] ); ?>" />
<?php wp_nonce_field( $users_key . 'edit_own', $users_key . '[nonce]' ); ?>
</td>
</tr>
</table>
<?php }
/**
* Form disable authy on profile
* @return string
*/
function disable_form_on_profile( $users_key ) {?>
<table class="form-table" id="<?php echo esc_attr( $users_key ); ?>">
<tr>
<th><label for="<?php echo esc_attr( $users_key ); ?>_disable"><?php _e( 'Disable Two Factor Authentication?', 'authy' ); ?></label></th>
<td>
<input type="checkbox" id="<?php echo esc_attr( $users_key ); ?>_disable" name="<?php echo esc_attr( $users_key ); ?>[disable_own]" value="1" />
<label for="<?php echo esc_attr( $users_key ); ?>_disable"><?php _e( 'Yes, disable Authy for your account.', 'authy' ); ?></label>
<?php wp_nonce_field( $users_key . 'disable_own', $users_key . '[nonce]' ); ?>
</td>
</tr>
</table>
<?php }
/**
* Form verify authy installation
* @return string
*/
function authy_installation_form( $user, $user_data, $user_signature, $errors ) {?>
<html>
<?php echo authy_header( 'verify_installation' ); ?>
<body class='login wp-core-ui'>
<div id="authy-verify">
<h1><a href="http://wordpress.org/" title="Powered by WordPress"><?php echo get_bloginfo( 'name' ); ?></a></h1>
<?php if ( !empty( $errors ) ) {?>
<div id="login_error"><strong><?php echo esc_attr_e( 'ERROR: ', 'authy' ); ?></strong><?php echo esc_attr_e( $errors, 'authy' ); ?></div>
<?php } ?>
<form method="POST" id="authy" action="wp-login.php">
<p><?php echo esc_attr_e( 'To activate your account you need to setup Authy Two-Factor Authentication.', 'authy' ); ?></p>
<div class='step'>
<div class='description-step'>
<span class='number'>1.</span>
<span>On your phone browser go to <a href="https://www.authy.com/install" alt="install authy" style="padding-left: 18px;">https://www.authy.com/install</a></span>
</div>
<img src="<?php echo plugins_url( '/assets/images/step1-image.png', __FILE__ ); ?>" alt='installation' />
</div>
<div class='step'>
<div class='description-step'>
<span class='number'>2.</span>
<span>Open the App and register.</span>
</div>
<img src="<?php echo plugins_url( '/assets/images/step2-image.png', __FILE__ ); ?>" alt='smartphones' style='padding-left: 22px;' />
</div>
<p class='italic-text'>
<?php echo esc_attr_e( 'If you don’t have an iPhone or Android ', 'authy' ); ?>
<a href="#" class="request-sms-link"
data-username="<?php echo esc_attr( $user->user_login );?>"
data-signature="<?php echo esc_attr( $user_signature ); ?>"><?php echo esc_attr_e( 'click here to get the Token as a Text Message.', 'authy' ); ?>
</a>
</p>
<label for="authy_token">
<?php _e( 'Authy Token', 'authy' ); ?>
<br>
<input type="text" name="authy_token" id="authy-token" class="input" value="" size="20" />
</label>
<input type="hidden" name="username" value="<?php echo esc_attr( $user->user_login ); ?>"/>
<input type="hidden" name="step" value="verify_installation"/>
<?php if ( isset( $user_signature ) ) { ?>
<input type="hidden" name="authy_signature" value="<?php echo esc_attr( $user_signature ); ?>"/>
<?php } ?>
<input type="submit" value="<?php echo esc_attr_e( 'Verify Token', 'authy' ) ?>" id="wp_submit" class="button button-primary">
<div class="rsms">
<img src="<?php echo plugins_url( '/assets/images/phone-icon.png', __FILE__ ); ?>" alt="cellphone">
<a href="#" class='request-sms-link' data-username="<?php echo esc_attr( $user->user_login );?>" data-signature="<?php echo esc_attr( $user_signature ); ?>">
<?php echo esc_attr_e( 'Get the token via SMS', 'authy' ); ?>
</a>
</div>
</form>
</div>
</body>
</html>
<?php }
/**
* Form for enable authy with JS
* @return string
*/
function form_enable_on_modal( $users_key, $username, $authy_data, $errors ) {?>
<p><?php printf( __( 'Authy is not yet configured for your the <strong>%s</strong> account.', 'authy' ), $username ); ?></p>
<p><?php _e( 'To enable Authy for this account, complete the form below, then click <em>Continue</em>.', 'authy' ); ?></p>
<?php if ( !empty($errors) ) { ?>
<div class='error'>
<?php
foreach ($errors as $key => $value) {
if ($key == 'country_code') : ?>
<p><strong>Country code</strong> is not valid.</p>
<?php elseif ( $key != 'message' ) : ?>
<p><strong><?php echo ucfirst($key); ?></strong><?php echo ' ' . $value; ?></p>
<?php endif;
}
?>
</div>
<?php } ?>
<table class="form-table" id="<?php echo esc_attr( $users_key ); ?>-ajax">
<tr>
<th><label for="phone"><?php _e( 'Country', 'authy' ); ?></label></th>
<td>
<input type="text" id="authy-countries" class="small-text" name="authy_country_code" value="<?php echo esc_attr( $authy_data['country_code'] ); ?>" required />
</td>
</tr>
<tr>
<th><label for="phone"><?php _e( 'Cellphone number', 'authy' ); ?></label></th>
<td>
<input type="tel" id="authy-cellphone" class="regular-text" name="authy_phone" value="<?php echo esc_attr( $authy_data['phone'] ); ?>" style="width:140px;" />
</td>
</tr>
</table>
<input type="hidden" name="authy_step" value="" />
<?php wp_nonce_field( $users_key . '_ajax_check' ); ?>
<p class="submit">
<input name="Continue" type="submit" value="<?php esc_attr_e( 'Continue' );?>" class="button-primary">
</p>
<?php }
/**
* Checkbox for admin disable authy to the user
* @return string
*/
function checkbox_for_admin_disable_authy( $users_key ) { ?>
<tr>
<th><label for="<?php echo esc_attr( $users_key ); ?>"><?php _e( 'Two Factor Authentication', 'authy' ); ?></label></th>
<td>
<input type="checkbox" id="<?php echo esc_attr( $users_key ); ?>" name="<?php echo esc_attr( $users_key ); ?>" value="1" checked/>
</td>
</tr>
<?php }
/**
* Render the form to enable authy by Admin user
* @return string
*/
function render_admin_form_enable_authy( $users_key, $authy_data ) { ?>
<tr>
<p><?php _e( 'To enable Authy enter the country and cellphone number of the person who is going to use this account.', 'authy' )?></p>
<th><label for="phone"><?php _e( 'Country', 'authy' ); ?></label></th>
<td>
<input type="text" id="authy-countries" class="small-text" name="<?php echo esc_attr( $users_key ); ?>[country_code]" value="<?php echo esc_attr( $authy_data['country_code'] ); ?>" />
</td>
</tr>
<tr>
<th><label for="phone"><?php _e( 'Cellphone number', 'authy' ); ?></label></th>
<td>
<input type="tel" class="regular-text" id="authy-cellphone" name="<?php echo esc_attr( $users_key ); ?>[phone]" value="<?php echo esc_attr( $authy_data['phone'] ); ?>" />
</td>
<?php wp_nonce_field( $users_key . '_edit', "_{$users_key}_wpnonce" ); ?>
</tr>
<tr>
<th><?php _e( 'Force enable Authy', 'authy' ); ?></th>
<td>
<label for="force-enable">
<input name="<?php echo esc_attr( $users_key ); ?>[force_enable_authy]" type="checkbox" value="true" <?php if ($authy_data['force_by_admin'] == 'true') echo 'checked="checked"'; ?> />
<?php _e( 'Force this user to enable Authy Two-Factor Authentication on the next login.', 'authy' ); ?>
</label>
</td>
</tr>
<?php }
/**
* Input for user disable authy on modal
* @return string
*/
function render_disable_authy_on_modal( $users_key, $username ) { ?>
<p><?php _e( 'Authy is enabled for this account.', 'authy' ); ?></p>
<p><?php printf( __( 'Click the button below to disable Two-Factor Authentication for <strong>%s</strong>', 'authy' ), $username ); ?></p>
<p class="submit">
<input name="Disable" type="submit" value="<?php esc_attr_e( 'Disable Authy' );?>" class="button-primary">
</p>
<input type="hidden" name="authy_step" value="disable" />
<?php wp_nonce_field( $users_key . '_ajax_disable' );
}
/**
* Confirmation when the user enables Authy.
* @return string
*/
function render_confirmation_authy_enabled( $authy_id, $username, $cellphone, $ajax_url ) {
if ( $authy_id ) : ?>
<p>
<?php printf( __( 'Congratulations, Authy is now configured for <strong>%s</strong> user account.', 'authy' ), $username ); ?>
</p>
<p>
<?php _e( "We've sent you an e-mail and text-message with instruction on how to install the Authy App. If you do not install the App, we'll automatically send you a text-message to your cellphone ", 'authy' ); ?>
<strong><?php echo esc_attr( $cellphone ); ?></strong>
<?php _e( 'on every login with the token that you need to use for when you login.', 'authy' ); ?>
</p>
<p><a class="button button-primary" href="#" onClick="self.parent.tb_remove();return false;"><?php _e( 'Return to your profile', 'authy' ); ?></a></p>
<?php else : ?>
<p><?php printf( __( 'Authy could not be activated for the <strong>%s</strong> user account.', 'authy' ), $username ); ?></p>
<p><?php _e( 'Please try again later.', 'authy' ); ?></p>
<p>
<a class="button button-primary" href="<?php echo esc_url( $ajax_url ); ?>"><?php _e( 'Try again', 'authy' ); ?></a>
</p>
<?php endif;
}
/**
* Confirmation when the user disables Authy.
*/
function render_confirmation_authy_disabled( ) { ?>
<p><?php echo esc_attr_e( 'Authy was disabled', 'authy' );?></p>
<p>
<a class="button button-primary" href="#" onClick="self.parent.tb_remove();return false;">
<?php _e( 'Return to your profile', 'authy' ); ?>
</a>
</p>
<?php }
/**
* Normalize cellphone
* given a cellphone return the normal form
* 17654305034 -> 765-430-5034
* normal form: 10 digits, {3}-{3}-{4}
* @param string $cellphone
* @return string
*/
function normalize_cellphone( $cellphone ) {
$cellphone = substr( $cellphone, 0, -4 ) . '-' . substr( $cellphone, -4 );
if ( strlen( $cellphone ) - 5 > 3 ) {
$cellphone = substr( $cellphone, 0, -8 ) . '-' . substr( $cellphone, -8 );
}
return $cellphone;
}
// closing the last tag is not recommended: http://php.net/basic-syntax.instruction-separation