/
class.two-factor-sms.php
360 lines (327 loc) · 9.7 KB
/
class.two-factor-sms.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
<?php
/**
* Class for creating a SMS provider.
*
* @since 0.1-dev
*
* @package Two_Factor
*/
class Two_Factor_Sms extends Two_Factor_Provider {
/**
* Twilio Library
* @var Services_Twilio
*/
public static $twilio;
/**
* The user meta Twilio AccoutSID
* @type string
*/
const ACCOUNT_SID_META_KEY = '_two_factor_sms_twilio_account_sid';
/**
* The user meta Twilio AuthToken
* @type string
*/
const AUTH_TOKEN_META_KEY = '_two_factor_sms_auth_token';
/**
* The user meta Twilio Sender Phone Number
* @type string
*/
const SENDER_NUMBER_META_KEY = '_two_factor_sms_sender_number';
/**
* The user meta Twilio Receiver Phone Number
* @type string
*/
const RECEIVER_NUMBER_META_KEY = '_two_factor_sms_receiver_number';
/**
* The user meta token key.
* @type string
*/
const TOKEN_META_KEY = '_two_factor_sms_token';
/**
* Ensures only one instance of this class exists in memory at any one time.
*
* @since 0.1-dev
*/
static function get_instance() {
static $instance;
$class = __CLASS__;
if ( ! is_a( $instance, $class ) ) {
if ( did_action( 'plugins_loaded' ) ) {
self::load_plugin_textdomain();
} else {
add_action( 'plugins_loaded', array( __CLASS__, 'load_plugin_textdomain' ) );
}
$instance = new $class;
}
return $instance;
}
/**
* Class constructor.
*
* @since 0.1-dev
*/
protected function __construct() {
add_action( 'two-factor-user-options-' . __CLASS__, array( $this, 'user_options' ) );
add_action( 'show_user_security_settings', array( __CLASS__, 'show_user_profile' ) );
add_action( 'user_profile_twilio', array( __CLASS__, 'show_twilio_item' ) );
add_action( 'personal_options_update', array( __CLASS__, 'catch_submission' ), 0 );
add_action( 'edit_user_profile_update', array( __CLASS__, 'catch_submission' ), 0 );
return parent::__construct();
}
/**
* Returns the name of the provider.
*
* @since 0.1-dev
*/
public function get_label() {
return _x( 'SMS (Twilio)', 'Provider Label', 'two-factor-sms' );
}
/**
* Generate the user token.
*
* @since 0.1-dev
*
* @param int $user_id User ID.
* @return string
*/
public function generate_token( $user_id ) {
$token = $this->get_code();
update_user_meta( $user_id, self::TOKEN_META_KEY, wp_hash( $token ) );
return $token;
}
/**
* Validate the user token.
*
* @since 0.1-dev
*
* @param int $user_id User ID.
* @param string $token User token.
* @return boolean
*/
public function validate_token( $user_id, $token ) {
$hashed_token = get_user_meta( $user_id, self::TOKEN_META_KEY, true );
if ( wp_hash( $token ) !== $hashed_token ) {
$this->delete_token( $user_id );
return false;
}
return true;
}
/**
* Delete the user token.
*
* @since 0.1-dev
*
* @param int $user_id User ID.
*/
public function delete_token( $user_id ) {
delete_user_meta( $user_id, self::TOKEN_META_KEY );
}
/**
* Generate and sms the user token.
*
* @since 0.1-dev
*
* @param WP_User $user WP_User object of the logged-in user.
* @return boolean
*/
public function generate_and_sms_token( $user ) {
require_once( TWO_FACTOR_SMS_DIR . 'includes/Twilio/Services/Twilio.php' );
$sid = get_user_meta( $user->ID, self::ACCOUNT_SID_META_KEY, true );
$token = get_user_meta( $user->ID, self::AUTH_TOKEN_META_KEY, true );
$sender = get_user_meta( $user->ID, self::SENDER_NUMBER_META_KEY, true );
$receiver = get_user_meta( $user->ID, self::RECEIVER_NUMBER_META_KEY, true );
self::$twilio = new Services_Twilio( $sid, $token );
$code = $this->generate_token( $user->ID );
try {
$message = self::$twilio->account->messages->create( array(
'From' => $sender,
'To' => $receiver,
'Body' => wp_strip_all_tags(
sprintf(
__( 'Your login confirmation code for %s is %s.', 'two-factor-sms' ),
get_bloginfo( 'name' ),
$code
)
),
) );
} catch ( Services_Twilio_RestException $e ) {
return false;
}
return true;
}
/**
* Prints the form that prompts the user to authenticate.
*
* @since 0.1-dev
*
* @param WP_User $user WP_User object of the logged-in user.
*/
public function authentication_page( $user ) {
if ( ! $user ) {
return;
}
if ( $this->generate_and_sms_token( $user ) ) {
require_once( ABSPATH . '/wp-admin/includes/template.php' );
?>
<p><?php esc_html_e( 'A verification code has been sent to the phone number associated with your account.', 'two-factor-sms' ); ?></p>
<p>
<label for="authcode"><?php esc_html_e( 'Verification Code:', 'two-factor-sms' ); ?></label>
<input type="tel" name="two-factor-sms-code" id="authcode" class="input" value="" size="20" pattern="[0-9]*" />
</p>
<script type="text/javascript">
setTimeout( function(){
var d;
try{
d = document.getElementById('authcode');
d.value = '';
d.focus();
} catch(e){}
}, 200);
</script>
<?php
submit_button( __( 'Log In', 'two-factor-sms' ) );
} else {
?>
<p><?php esc_html_e( 'An error occured while sending SMS.', 'two-factor-sms' ); ?></p>
<?php
}
}
/**
* Validates the users input token.
*
* @since 0.1-dev
*
* @param WP_User $user WP_User object of the logged-in user.
* @return boolean
*/
public function validate_authentication( $user ) {
if ( ! isset( $user->ID ) || ! isset( $_REQUEST['two-factor-sms-code'] ) ) {
return false;
}
return $this->validate_token( $user->ID, $_REQUEST['two-factor-sms-code'] );
}
/**
* Whether this Two Factor provider is configured and available for the user specified.
*
* @since 0.1-dev
*
* @param WP_User $user WP_User object of the logged-in user.
* @return boolean
*/
public function is_available_for_user( $user ) {
if ( ! $user ) {
return false;
}
return (
! empty( get_user_meta( $user->ID, self::ACCOUNT_SID_META_KEY, true ) )
&& ! empty( get_user_meta( $user->ID, self::AUTH_TOKEN_META_KEY, true ) )
&& ! empty( get_user_meta( $user->ID, self::SENDER_NUMBER_META_KEY, true ) )
&& ! empty( get_user_meta( $user->ID, self::RECEIVER_NUMBER_META_KEY, true ) )
);
}
/**
* Inserts markup at the end of the user profile field for this provider.
*
* @since 0.1-dev
*
* @param WP_User $user WP_User object of the logged-in user.
*/
public function user_options( $user ) {
?>
<div>
<?php echo esc_html( __( 'You need Twilio account.', 'two-factor-sms' ) ); ?>
</div>
<?php
}
/**
* Display the Twilio section in a users profile.
*
* This executes during the `show_user_profile` & `edit_user_profile` actions.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param WP_User $user WP_User object of the logged-in user.
*/
public static function show_user_profile( $user ) {
if ( did_action( 'user_profile_twilio' ) ) {
return;
}
?>
<div class="twilio" id="twilio-section">
<h3><?php esc_html_e( 'Twilio', 'two-factor-sms' ); ?></h3>
<table class="form-table">
<?php do_action( 'user_profile_twilio', $user ); ?>
</table>
</div>
<?php
}
/**
* Display the form in Twilio section.
*
* @since 0.1.2
*
* @access public
* @static
*
* @param WP_User $user WP_User object of the logged-in user.
*/
public static function show_twilio_item( $user ) {
$sid = get_user_meta( $user->ID, self::ACCOUNT_SID_META_KEY, true );
$token = get_user_meta( $user->ID, self::AUTH_TOKEN_META_KEY, true );
$sender = get_user_meta( $user->ID, self::SENDER_NUMBER_META_KEY, true );
$receiver = get_user_meta( $user->ID, self::RECEIVER_NUMBER_META_KEY, true );
?>
<tr class="user-twilio-sid-wrap">
<th><label for="twilio-sid"><?php esc_html_e( 'AccountSID' , 'two-factor-sms' ); ?></label></th>
<td><input type="text" name="twilio-sms-sid" id="twilio-sms-sid" value="<?php echo esc_attr( $sid ) ?>" class="regular-text code"></td>
</tr>
<tr class="user-twilio-token-wrap">
<th><label for="twilio-token"><?php esc_html_e( 'AuthToken', 'two-factor-sms' ); ?></label></th>
<td><input type="password" name="twilio-sms-token" id="twilio-sms-token" value="<?php echo esc_attr( $token ) ?>" class="regular-text code"></td>
</tr>
<tr class="user-twilio-sender-wrap">
<th><label for="twilio-sender"><?php esc_html_e( 'Sender Phone Number', 'two-factor-sms' ); ?></label></th>
<td><input type="tel" name="twilio-sms-sender" id="twilio-sms-sender" value="<?php echo esc_attr( $sender ) ?>" class="regular-text code"></td>
</tr>
<tr class="user-twilio-receiver-wrap">
<th><label for="twilio-receiver"><?php esc_html_e( 'Receiver Phone Number', 'two-factor-sms' ); ?></label></th>
<td><input type="tel" name="twilio-sms-receiver" id="twilio-sms-receiver" value="<?php echo esc_attr( $receiver ) ?>" class="regular-text code"></td>
</tr>
<?php
}
/**
* Catch the non-ajax submission from the new form.
*
* This executes during the `personal_options_update` & `edit_user_profile_update` actions.
*
* @since 0.1-dev
*
* @access public
* @static
*
* @param int $user_id User ID.
*/
public static function catch_submission( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return;
}
update_user_meta( $user_id, self::ACCOUNT_SID_META_KEY, $_POST['twilio-sms-sid'] );
update_user_meta( $user_id, self::AUTH_TOKEN_META_KEY, $_POST['twilio-sms-token'] );
update_user_meta( $user_id, self::SENDER_NUMBER_META_KEY, $_POST['twilio-sms-sender'] );
update_user_meta( $user_id, self::RECEIVER_NUMBER_META_KEY, $_POST['twilio-sms-receiver'] );
}
/**
* Load Translations.
*
* @since 0.1.1
*
* @access public
* @static
*/
public static function load_plugin_textdomain() {
load_plugin_textdomain( 'two-factor-sms', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
}