| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Google Authenticator |
| 4 |
Plugin URI: http://henrik.schack.dk/google-authenticator-for-wordpress |
| 5 |
Description: Two-Factor Authentication for WordPress using the Android/iPhone/Blackberry app as One Time Password generator. |
| 6 |
Author: Henrik Schack |
| 7 |
Version: 0.36 |
| 8 |
Author URI: http://henrik.schack.dk/ |
| 9 |
Compatibility: WordPress 3.2.1 |
| 10 |
Text Domain: google-authenticator |
| 11 |
Domain Path: /lang |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
|
| 15 |
Thanks to Bryan Ruiz for his Base32 encode/decode class, found at php.net. |
| 16 |
Thanks to Tobias B�thge for his major code rewrite and German translation. |
| 17 |
|
| 18 |
---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
Copyright 2011 Henrik Schack (email : henrik@schack.dk) |
| 21 |
|
| 22 |
This program is free software; you can redistribute it and/or modify |
| 23 |
it under the terms of the GNU General Public License as published by |
| 24 |
the Free Software Foundation; either version 2 of the License, or |
| 25 |
(at your option) any later version. |
| 26 |
|
| 27 |
This program is distributed in the hope that it will be useful, |
| 28 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 |
GNU General Public License for more details. |
| 31 |
|
| 32 |
You should have received a copy of the GNU General Public License |
| 33 |
along with this program; if not, write to the Free Software |
| 34 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 35 |
*/ |
| 36 |
|
| 37 |
class GoogleAuthenticator { |
| 38 |
|
| 39 |
static $instance; // to store a reference to the plugin, allows other plugins to remove actions |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor, entry point of the plugin |
| 43 |
*/ |
| 44 |
function __construct() { |
| 45 |
self::$instance = $this; |
| 46 |
add_action( 'init', array( $this, 'init' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Initialization, Hooks, and localization |
| 51 |
*/ |
| 52 |
function init() { |
| 53 |
require_once( 'base32.php' ); |
| 54 |
|
| 55 |
add_action( 'login_form', array( $this, 'loginform' ) ); |
| 56 |
add_filter( 'authenticate', array( $this, 'check_otp' ), 50, 3 ); |
| 57 |
|
| 58 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 59 |
add_action( 'wp_ajax_GoogleAuthenticator_action', array( $this, 'ajax_callback' ) ); |
| 60 |
|
| 61 |
add_action( 'personal_options_update', array( $this, 'personal_options_update' ) ); |
| 62 |
add_action( 'profile_personal_options', array( $this, 'profile_personal_options' ) ); |
| 63 |
add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); |
| 64 |
add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) ); |
| 65 |
|
| 66 |
load_plugin_textdomain( 'google-authenticator', false, basename( dirname( __FILE__ ) ) . '/lang' ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Check the verification code entered by the user. |
| 71 |
*/ |
| 72 |
function verify( $secretkey, $thistry ) { |
| 73 |
|
| 74 |
$tm = floor( time() / 30 ); |
| 75 |
|
| 76 |
$secretkey=Base32::decode($secretkey); |
| 77 |
// Keys from 30 seconds before and after are valid aswell. |
| 78 |
for ($i=-1; $i<2; $i++) { |
| 79 |
// Pack time into binary string |
| 80 |
$time=chr(0).chr(0).chr(0).chr(0).pack('N*',$tm+$i); |
| 81 |
// Hash it with users secret key |
| 82 |
$hm = hash_hmac( 'SHA1', $time, $secretkey, true ); |
| 83 |
// Use last nipple of result as index/offset |
| 84 |
$offset = ord(substr($hm,-1)) & 0x0F; |
| 85 |
// grab 4 bytes of the result |
| 86 |
$hashpart=substr($hm,$offset,4); |
| 87 |
// Unpak binary value |
| 88 |
$value=unpack("N",$hashpart); |
| 89 |
$value=$value[1]; |
| 90 |
// Only 32 bits |
| 91 |
$value = $value & 0x7FFFFFFF; |
| 92 |
$value = $value % 1000000; |
| 93 |
if ( $value == $thistry ) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Create a new random secret for the Google Authenticator app. |
| 102 |
* 16 characters, randomly chosen from the allowed Base32 characters |
| 103 |
* equals 10 bytes = 80 bits, as 256^10 = 32^16 = 2^80 |
| 104 |
*/ |
| 105 |
function create_secret() { |
| 106 |
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; // allowed characters in Base32 |
| 107 |
$secret = ''; |
| 108 |
for ( $i = 0; $i < 16; $i++ ) { |
| 109 |
$secret .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 ); |
| 110 |
} |
| 111 |
return $secret; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Add verification code field to login form. |
| 117 |
*/ |
| 118 |
function loginform() { |
| 119 |
echo "\t<p>\n"; |
| 120 |
echo "\t\t<label><a href=\"http://code.google.com/p/google-authenticator/\" target=\"_blank\" title=\"".__('If you don\'t have Google Authenticator enabled for your WordPress account, leave this field empty.','google-authenticator')."\">".__('Google Authenticator code','google-authenticator')."</a><span id=\"google-auth-info\"></span><br />\n"; |
| 121 |
echo "\t\t<input type=\"password\" name=\"otp\" id=\"user_email\" class=\"input\" value=\"\" size=\"20\" tabindex=\"25\" /></label>\n"; |
| 122 |
echo "\t</p>\n"; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Login form handling. |
| 128 |
* Check Google Authenticator verification code, if user has been setup to do so. |
| 129 |
* @param wordpressuser |
| 130 |
* @return user/loginstatus |
| 131 |
*/ |
| 132 |
function check_otp( $user, $username = '', $password = '' ) { |
| 133 |
// Store result of loginprocess, so far. |
| 134 |
$userstate = $user; |
| 135 |
|
| 136 |
// Get information on user, we need this in case an app password has been enabled, |
| 137 |
// since the $user var only contain an error at this point in the login flow. |
| 138 |
$user = get_userdatabylogin( $username ); |
| 139 |
|
| 140 |
// Does the user have the Google Authenticator enabled ? |
| 141 |
if ( trim(get_user_option( 'googleauthenticator_enabled', $user->ID ) ) == 'enabled' ) { |
| 142 |
|
| 143 |
// Get the users secret |
| 144 |
$GA_secret = trim( get_user_option( 'googleauthenticator_secret', $user->ID ) ); |
| 145 |
|
| 146 |
// Get the verification code entered by the user trying to login |
| 147 |
$otp = intval( trim( $_POST[ 'otp' ] ) ); |
| 148 |
|
| 149 |
// Valid code ? |
| 150 |
if ( $this->verify( $GA_secret, $otp ) ) { |
| 151 |
return $userstate; |
| 152 |
} else { |
| 153 |
// No, lets see if an app password is enabled, and this is an XMLRPC / APP login ? |
| 154 |
if ( trim( get_user_option( 'googleauthenticator_pwdenabled', $user->ID ) ) == 'enabled' && ( defined('XMLRPC_REQUEST') || defined('APP_REQUEST') ) ) { |
| 155 |
$GA_passwords = json_decode( get_user_option( 'googleauthenticator_passwords', $user->ID ) ); |
| 156 |
$passwordsha1 = trim($GA_passwords->{'password'} ); |
| 157 |
$usersha1 = sha1( strtoupper( str_replace( ' ', '', $password ) ) ); |
| 158 |
if ( $passwordsha1 == $usersha1 ) { |
| 159 |
return new WP_User( $user->ID ); |
| 160 |
} else { |
| 161 |
// Wrong XMLRPC/APP password ! |
| 162 |
return new WP_Error( 'invalid_google_authenticator_password', __( '<strong>ERROR</strong>: The Google Authenticator password is incorrect.', 'google-authenticator' ) ); |
| 163 |
} |
| 164 |
} else { |
| 165 |
return new WP_Error( 'invalid_google_authenticator_token', __( '<strong>ERROR</strong>: The Google Authenticator code is incorrect or has expired.', 'google-authenticator' ) ); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
// Google Authenticator isn't enabled for this account, |
| 170 |
// just resume normal authentication. |
| 171 |
return $userstate; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Extend personal profile page with Google Authenticator settings. |
| 177 |
*/ |
| 178 |
function profile_personal_options() { |
| 179 |
global $user_id, $is_profile_page; |
| 180 |
|
| 181 |
$GA_secret = trim( get_user_option( 'googleauthenticator_secret', $user_id ) ); |
| 182 |
$GA_enabled = trim( get_user_option( 'googleauthenticator_enabled', $user_id ) ); |
| 183 |
$GA_description = trim( get_user_option( 'googleauthenticator_description', $user_id ) ); |
| 184 |
$GA_pwdenabled = trim( get_user_option( 'googleauthenticator_pwdenabled', $userid ) ); |
| 185 |
$GA_password = trim( get_user_option( 'googleauthenticator_passwords', $user_id ) ); |
| 186 |
|
| 187 |
// We dont store the generated app password in cleartext so there is no point in trying |
| 188 |
// to show the user anything except from the fact that a password exists. |
| 189 |
if ( $GA_password != '' ) { |
| 190 |
$GA_password = "XXXX XXXX XXXX XXXX"; |
| 191 |
} |
| 192 |
|
| 193 |
// In case the user has no secret ready (new install), we create one. |
| 194 |
if ( '' == $GA_secret ) { |
| 195 |
$GA_secret = $this->create_secret(); |
| 196 |
} |
| 197 |
|
| 198 |
// Use "WordPress Blog" as default description |
| 199 |
if ( '' == $GA_description ) { |
| 200 |
$GA_description = __( 'WordPress Blog', 'google-authenticator' ); |
| 201 |
} |
| 202 |
|
| 203 |
echo "<h3>".__( 'Google Authenticator Settings', 'google-authenticator' )."</h3>\n"; |
| 204 |
|
| 205 |
echo "<table class=\"form-table\">\n"; |
| 206 |
echo "<tbody>\n"; |
| 207 |
echo "<tr>\n"; |
| 208 |
echo "<th scope=\"row\">".__( 'Active', 'google-authenticator' )."</th>\n"; |
| 209 |
echo "<td>\n"; |
| 210 |
echo "<input name=\"GA_enabled\" id=\"GA_enabled\" class=\"tog\" type=\"checkbox\"" . checked( $GA_enabled, 'enabled', false ) . "/>\n"; |
| 211 |
echo "</td>\n"; |
| 212 |
echo "</tr>\n"; |
| 213 |
|
| 214 |
// Create URL for the Google charts QR code generator. |
| 215 |
$chl = urlencode( "otpauth://totp/{$GA_description}?secret={$GA_secret}" ); |
| 216 |
$qrcodeurl = "https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=H|0&chl={$chl}"; |
| 217 |
|
| 218 |
if ( $is_profile_page || IS_PROFILE_PAGE ) { |
| 219 |
echo "<tr>\n"; |
| 220 |
echo "<th><label for=\"GA_description\">".__('Description','google-authenticator')."</label></th>\n"; |
| 221 |
echo "<td><input name=\"GA_description\" id=\"GA_description\" value=\"{$GA_description}\" type=\"text\" size=\"25\" /><span class=\"description\">".__(' Description that you\'ll see in the Google Authenticator app on your phone.','google-authenticator')."</span><br /></td>\n"; |
| 222 |
echo "</tr>\n"; |
| 223 |
|
| 224 |
echo "<tr>\n"; |
| 225 |
echo "<th><label for=\"GA_secret\">".__('Secret','google-authenticator')."</label></th>\n"; |
| 226 |
echo "<td>\n"; |
| 227 |
echo "<input name=\"GA_secret\" id=\"GA_secret\" value=\"{$GA_secret}\" readonly=\"readonly\" type=\"text\" size=\"25\" />"; |
| 228 |
echo "<input name=\"GA_newsecret\" id=\"GA_newsecret\" value=\"".__("Create new secret",'google-authenticator')."\" type=\"button\" class=\"button\" />"; |
| 229 |
echo "<input name=\"show_qr\" id=\"show_qr\" value=\"".__("Show/Hide QR code",'google-authenticator')."\" type=\"button\" class=\"button\" onclick=\"jQuery('#GA_QR_INFO').toggle('slow');\" />"; |
| 230 |
echo "</td>\n"; |
| 231 |
echo "</tr>\n"; |
| 232 |
|
| 233 |
echo "<tr>\n"; |
| 234 |
echo "<th></th>\n"; |
| 235 |
echo "<td><div id=\"GA_QR_INFO\" style=\"display: none\" >"; |
| 236 |
echo "<img id=\"GA_QRCODE\" src=\"{$qrcodeurl}\" alt=\"QR Code\"/>"; |
| 237 |
echo '<span class="description"><br/> ' . __( 'Scan this with the Google Authenticator app.', 'google-authenticator' ) . '</span>'; |
| 238 |
echo "</div></td>\n"; |
| 239 |
echo "</tr>\n"; |
| 240 |
|
| 241 |
echo "<tr>\n"; |
| 242 |
echo "<th scope=\"row\">".__( 'Enable App password', 'google-authenticator' )."</th>\n"; |
| 243 |
echo "<td>\n"; |
| 244 |
echo "<input name=\"GA_pwdenabled\" id=\"GA_pwdenabled\" class=\"tog\" type=\"checkbox\"" . checked( $GA_pwdenabled, 'enabled', false ) . "/><span class=\"description\">".__(' Enabling an App password will decrease your overall login security.','google-authenticator')."</span>\n"; |
| 245 |
echo "</td>\n"; |
| 246 |
echo "</tr>\n"; |
| 247 |
|
| 248 |
echo "<tr>\n"; |
| 249 |
echo "<th></th>\n"; |
| 250 |
echo "<td>\n"; |
| 251 |
echo "<input name=\"GA_password\" id=\"GA_password\" readonly=\"readonly\" value=\"".$GA_password."\" type=\"text\" size=\"25\" />"; |
| 252 |
echo "<input name=\"GA_createpassword\" id=\"GA_createpassword\" value=\"".__("Create new password",'google-authenticator')."\" type=\"button\" class=\"button\" />"; |
| 253 |
echo "<span class=\"description\" id=\"GA_passworddesc\">".__(' Password is not stored in cleartext, this is your only chance to see it.','google-authenticator')."</span>\n"; |
| 254 |
echo "</td>\n"; |
| 255 |
echo "</tr>\n"; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
echo "</tbody></table>\n"; |
| 260 |
echo "<script type=\"text/javascript\">\n"; |
| 261 |
echo "var GAnonce='".wp_create_nonce('GoogleAuthenticatoraction')."';\n"; |
| 262 |
echo <<<ENDOFJS |
| 263 |
var pwdata; |
| 264 |
jQuery('#GA_newsecret').bind('click', function() { |
| 265 |
var data=new Object(); |
| 266 |
data['action'] = 'GoogleAuthenticator_action'; |
| 267 |
data['nonce'] = GAnonce; |
| 268 |
jQuery.post(ajaxurl, data, function(response) { |
| 269 |
jQuery('#GA_secret').val(response['new-secret']); |
| 270 |
chl=escape("otpauth://totp/"+jQuery('#GA_description').val()+"?secret="+jQuery('#GA_secret').val()); |
| 271 |
qrcodeurl="https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=H|0&chl="+chl; |
| 272 |
jQuery('#GA_QRCODE').attr('src',qrcodeurl); |
| 273 |
jQuery('#GA_QR_INFO').show('slow'); |
| 274 |
}); |
| 275 |
}); |
| 276 |
|
| 277 |
jQuery('#GA_description').bind('focus blur change keyup', function() { |
| 278 |
chl=escape("otpauth://totp/"+jQuery('#GA_description').val()+"?secret="+jQuery('#GA_secret').val()); |
| 279 |
qrcodeurl="https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=H|0&chl="+chl; |
| 280 |
jQuery('#GA_QRCODE').attr('src',qrcodeurl); |
| 281 |
}); |
| 282 |
|
| 283 |
jQuery('#GA_createpassword').bind('click',function() { |
| 284 |
var data=new Object(); |
| 285 |
data['action'] = 'GoogleAuthenticator_action'; |
| 286 |
data['nonce'] = GAnonce; |
| 287 |
data['save'] = 1; |
| 288 |
jQuery.post(ajaxurl, data, function(response) { |
| 289 |
jQuery('#GA_password').val(response['new-secret'].match(new RegExp(".{0,4}","g")).join(' ')); |
| 290 |
jQuery('#GA_passworddesc').show(); |
| 291 |
}); |
| 292 |
}); |
| 293 |
|
| 294 |
jQuery('#GA_enabled').bind('change',function() { |
| 295 |
GoogleAuthenticator_apppasswordcontrol(); |
| 296 |
}); |
| 297 |
|
| 298 |
jQuery(document).ready(function() { |
| 299 |
jQuery('#GA_passworddesc').hide(); |
| 300 |
GoogleAuthenticator_apppasswordcontrol(); |
| 301 |
}); |
| 302 |
|
| 303 |
function GoogleAuthenticator_apppasswordcontrol() { |
| 304 |
if (jQuery('#GA_enabled').is(':checked')) { |
| 305 |
jQuery('#GA_pwdenabled').removeAttr('disabled'); |
| 306 |
jQuery('#GA_createpassword').removeAttr('disabled'); |
| 307 |
} else { |
| 308 |
jQuery('#GA_pwdenabled').removeAttr('checked') |
| 309 |
jQuery('#GA_pwdenabled').attr('disabled', true); |
| 310 |
jQuery('#GA_createpassword').attr('disabled', true); |
| 311 |
} |
| 312 |
} |
| 313 |
</script> |
| 314 |
ENDOFJS; |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Form handling of Google Authenticator options added to personal profile page (user editing his own profile) |
| 320 |
*/ |
| 321 |
function personal_options_update() { |
| 322 |
global $user_id; |
| 323 |
|
| 324 |
$GA_enabled = trim( $_POST['GA_enabled'] ); |
| 325 |
$GA_secret = trim( $_POST['GA_secret'] ); |
| 326 |
$GA_pwdenabled = trim( $_POST['GA_pwdenabled'] ); |
| 327 |
$GA_password = str_replace(' ', '', trim( $_POST['GA_password'] ) ); |
| 328 |
|
| 329 |
if ( '' == $GA_enabled ) { |
| 330 |
$GA_enabled = 'disabled'; |
| 331 |
} else { |
| 332 |
$GA_enabled = 'enabled'; |
| 333 |
} |
| 334 |
|
| 335 |
if ( '' == $GA_pwdenabled ) { |
| 336 |
$GA_pwdenabled = 'disabled'; |
| 337 |
} else { |
| 338 |
$GA_pwdenabled = 'enabled'; |
| 339 |
} |
| 340 |
|
| 341 |
// Only store password if a new one has been generated. |
| 342 |
if (strtoupper($GA_password) != 'XXXXXXXXXXXXXXXX' ) { |
| 343 |
// Store the password in a format that can be expanded easily later on if needed. |
| 344 |
$GA_password = array( 'appname' => 'Default', 'password' => sha1( $GA_password ) ); |
| 345 |
update_user_option( $user_id, 'googleauthenticator_passwords', json_encode( $GA_password ), true ); |
| 346 |
} |
| 347 |
|
| 348 |
update_user_option( $user_id, 'googleauthenticator_enabled', $GA_enabled, true ); |
| 349 |
update_user_option( $user_id, 'googleauthenticator_secret', $GA_secret, true ); |
| 350 |
update_user_option( $user_id, 'googleauthenticator_pwdenabled', $GA_pwdenabled, true ); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Extend profile page with ability to enable/disable Google Authenticator authentication requirement. |
| 356 |
* Used by an administrator when editing other users. |
| 357 |
*/ |
| 358 |
function edit_user_profile() { |
| 359 |
global $user_id; |
| 360 |
$GA_enabled = trim( get_user_option( 'googleauthenticator_enabled', $user_id ) ); |
| 361 |
echo "<h3>".__('Google Authenticator Settings','google-authenticator')."</h3>\n"; |
| 362 |
echo "<table class=\"form-table\">\n"; |
| 363 |
echo "<tbody>\n"; |
| 364 |
echo "<tr>\n"; |
| 365 |
echo "<th scope=\"row\">".__('Active','google-authenticator')."</th>\n"; |
| 366 |
echo "<td>\n"; |
| 367 |
echo "<div><input name=\"GA_enabled\" id=\"GA_enabled\" class=\"tog\" type=\"checkbox\"" . checked( $GA_enabled, 'enabled', false ) . "/>\n"; |
| 368 |
echo "</td>\n"; |
| 369 |
echo "</tr>\n"; |
| 370 |
echo "</tbody>\n"; |
| 371 |
echo "</table>\n"; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Form handling of Google Authenticator options on edit profile page (admin user editing other user) |
| 376 |
*/ |
| 377 |
function edit_user_profile_update() { |
| 378 |
global $user_id; |
| 379 |
|
| 380 |
$GA_enabled = trim( $_POST['GA_enabled'] ); |
| 381 |
|
| 382 |
if ( '' == $GA_enabled ) { |
| 383 |
$GA_enabled = 'disabled'; |
| 384 |
} else { |
| 385 |
$GA_enabled = 'enabled'; |
| 386 |
} |
| 387 |
|
| 388 |
update_user_option( $user_id, 'googleauthenticator_enabled', $GA_enabled, true ); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
/** |
| 393 |
* AJAX callback function used to generate new secret |
| 394 |
*/ |
| 395 |
function ajax_callback() { |
| 396 |
global $user_id; |
| 397 |
|
| 398 |
// Some AJAX security |
| 399 |
check_ajax_referer( 'GoogleAuthenticatoraction', 'nonce' ); |
| 400 |
|
| 401 |
// Create new secret, using the users password hash as input for further hashing |
| 402 |
$secret = $this->create_secret(); |
| 403 |
|
| 404 |
$result = array( 'new-secret' => $secret ); |
| 405 |
header( 'Content-Type: application/json' ); |
| 406 |
echo json_encode( $result ); |
| 407 |
|
| 408 |
// die() is required to return a proper result |
| 409 |
die(); |
| 410 |
} |
| 411 |
|
| 412 |
} // end class |
| 413 |
|
| 414 |
new GoogleAuthenticator; |
| 415 |
?> |