openWallet Version: 0.0.1 Author: Windy Road Author URI: http://windyroad.org Copyright (C)2007 Windy Road This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.This work is licensed under a Creative Commons Attribution 2.5 Australia License http://creativecommons.org/licenses/by/2.5/au/ */ define( OW_URL, "http://localhost:7274/" ); define( OW_KEY_TABLE, 'openwallet_keyid'); function open_wallet_handle_action() { if( isset( $_GET[ 'ow-action' ] ) ) { $args = array(); $args[ 'ow-nonce' ] = substr(wp_hash(ceil(time()) . $args[ 'ow-action' ]), -12, 10); session_start(); $_SESSION[ 'ow-nonce' ] = $args[ 'ow-nonce' ]; $args[ 'ow-url' ] = get_bloginfo( 'url' ); $url = OW_URL; if( $_GET[ 'ow-action' ] == 'login' ) { $url .= 'login/'; } else if( $_GET[ 'ow-action' ] == 'upload-key' ) { $url .= 'requestkey/'; } $url = add_query_arg( $args, $url ); wp_redirect($url); exit; } if( isset( $_GET[ 'ow-login' ] ) ) { open_wallet_verify_user(); } if( isset( $_GET[ 'ow-keyupload' ] ) ) { open_wallet_store_key(); } } function open_wallet_verify_sig() { $output = array(); $result = 0; $data_fn = tempnam("c:\\temp","ow-"); $data_f = fopen($data_fn, "w"); session_start(); fwrite( $data_f, get_bloginfo('url') . $_SESSION[ 'ow-nonce' ] . "\n" ); // each nonce is for one use only, so once we have read it, // we can remove it from the session. unset( $_SESSION[ 'ow-nonce'] ); session_write_close(); fclose( $data_f ); $sig_fn = tempnam("c:\\temp","ow-"); $sig_f = fopen($sig_fn, "w"); fwrite( $sig_f, $_GET[ 'ow-sig' ] ); fclose( $sig_f ); $cmd = open_wallet_get_gpg() . ' ' . open_wallet_get_gpg_homedir_param() . ' --batch --no-tty --verbose --verbose --verify ' . $sig_fn . ' ' . $data_fn . ' 2>&1'; exec( $cmd, $output, $result ); // echo "
"; // print_r( $_GET ); // print_r( $_SESSION ); // print_r( "
cmd: " . $cmd . "
" ); // print_r( "result: " . $result . "
" ); // print_r( $output ); exit; unlink( $data_fn ); unlink( $sig_fn ); if( $result == 0 ) { $words = explode( ' ', $output[ 2 ] ); $id = $words[ count( $words )-1 ]; // echo ""; // print_r( "
id: " . $id . "
" ); // print_r( "
cmd: " . $cmd . "
" ); // print_r( "result: " . $result . "
" ); // print_r( $output ); exit; // woohoo! verified the user return $id; } else { if( count( $output ) == 11 ) { $words = explode( ' ', $output[ 10 ] ); if( $words[ count( $words )-1 ] == "found" ) { // we don't have the users public key, go fetch $url = '/?ow-action=upload-key/'; wp_redirect($url); exit(); } else if( false && $words[ 1 ] == "BAD" ) { // TODO: pretty error message. echo "Sorry, we could not verify your login."; exit(); } } else if( count( $output ) == 1 ) { $words = explode( ' ', $output[ 0 ] ); if( $words[ 3 ] == "failed:" ) { echo "Sorry, we could not verify your login."; exit(); } } open_wallet_failed( $result, $output ); } } function open_wallet_verify_user() { $id = open_wallet_verify_sig(); open_wallet_login( $id ); } function open_wallet_failed( $result, $output ) { header('HTTP/1.1 500 Internal Server Error' ); header('Status: 500 Internal Server Error' ); ?>500 Internal Server Error
Unexpected GnuPG Output
"; print_r( "result: " . $result . "
" ); print_r( "output:
" ); print_r( $output ); echo ""; exit; } function open_wallet_store_key() { $output = array(); $result = 0; $tempfn = tempnam("c:\\temp","ow-"); $fh = fopen($tempfn, "w"); fwrite( $fh, $_GET[ 'ow-key' ]); fclose( $fh ); // if( isLoggedIn() ) // { // exec( GPG . ' ' . GPG_HOMEDIR . ' --batch --no-tty --yes --delete-keys ' . $_SESSION[ 'id' ] . ' 2>&1', $output2, $result ); // $_SESSION[ 'output2' ] = $output2; // } $cmd = open_wallet_get_gpg() . ' ' . open_wallet_get_gpg_homedir_param() . ' --batch --no-tty --import --verbose --verbose ' . $tempfn . ' 2>&1'; exec( $cmd, $output, $result ); // echo ""; // print_r( $_GET[ 'ow-key' ] ); // print_r( "
cmd: " . $cmd . "
" ); // print_r( "result: " . $result . "
" ); // print_r( $output ); exit; unlink( $tempfn ); if( $result == 0 ) { get_currentuserinfo(); global $current_user; if( $current_user->ID == 0 ) { // user is not yet logged in. // go and verify the signature. open_wallet_verify_user(); } else { // firstly, lets verify that the signature is good, // otherwise someone might be trying to trick the system $key_id = open_wallet_verify_sig(); // NOTE: for security reasons, $key_id is the key id from the signature, // not the key they sent us. We can only ever trust signatures. // first we need to see if this key is currently associated with // another user. $matching_user_id = open_wallet_find_user( $key_id ); if( NULL !== $matching_user_id ) { // this key belongs to another user. // This can occur if the user has multiple accounts on // this blog and they are trying to associate the key // with both accounts. // TODO: pretty error message echo "Error: this key is already associated with another account"; exit(); } else { // associate the key with the current user. global $wpdb, $current_user; $user_id = $current_user->ID; $table_name = $wpdb->prefix . OW_KEY_TABLE; $wpdb->query("INSERT INTO {$table_name} (user_id, key_id) VALUES ({$user_id}, '" . $wpdb->escape($key_id) . "')"); wp_redirect("/wordpress/wp-admin/profile.php"); exit; } } } else { open_wallet_failed( $result, $output ); } } function open_wallet_do_login( $user_id ) { $user = new WP_User( $user_id ); if( wp_login( $user->user_login, md5($user->user_pass), true ) ) { do_action('wp_login', $user->user_login); wp_clearcookie(); wp_setcookie($user->user_login, md5($user->user_pass), true, '', '', true); wp_redirect("/wordpress/wp-admin/profile.php"); exit; } } function open_wallet_find_user( $key_id ) { global $wpdb; $table_name = $wpdb->prefix . OW_KEY_TABLE; $row = $wpdb->get_row("SELECT user_ID FROM {$table_name} WHERE key_id = '" . $wpdb->escape($key_id) . "' LIMIT 1"); return $row->user_ID; } function open_wallet_get_user_keys() { global $current_user; $user_id = $current_user->ID; global $wpdb; $table_name = $wpdb->prefix . OW_KEY_TABLE; $results = $wpdb->get_results("SELECT key_id FROM {$table_name} WHERE user_ID = " . $wpdb->escape($user_id)); $rval = array(); foreach( $results as $row ) { $rval[] = $row->key_id; } return $rval; } function open_wallet_create_user( $key_id ) { @include_once( ABSPATH . 'wp-admin/upgrade-functions.php'); // 2.1 @include_once( ABSPATH . WPINC . '/registration-functions.php'); // 2.0.4 $username = $key_id; $password = substr( md5( uniqid( microtime() ) ), 0, 7); $user_id = wp_create_user( $username, $password ); global $wpdb; $table_name = $wpdb->prefix . OW_KEY_TABLE; $wpdb->query("INSERT INTO {$table_name} (user_id, key_id) VALUES ({$user_id}, '" . $wpdb->escape($key_id) . "')"); if( $user_id ) { // created ok update_usermeta( $user_id, 'registered_with_openwallet', true ); $user = new WP_User( $user_id ); if( wp_login( $user->user_login, md5($user->user_pass), true ) ) { do_action('wp_login', $user->user_login); // Call the usual user-registration hooks do_action('user_register', $user_id); wp_new_user_notification( $user->user_login ); wp_clearcookie(); wp_setcookie($user->user_login, md5($user->user_pass), true, '', '', true); wp_redirect("/wordpress/wp-admin/profile.php"); exit; } else { // failed to create user for some reason. echo "openWallet authentication successful, but failed to login the Wordpress user. This is probably a bug."; exit; } } else { // failed to create user for some reason. echo "openWallet authentication successful, but failed to create Wordpress user. This is probably a bug."; exit; } } function open_wallet_login( $id ) { $matching_user_id = open_wallet_find_user( $id ); if( NULL !== $matching_user_id ) { open_wallet_do_login( $matching_user_id ); } else { // key id is new, so create a new user open_wallet_create_user( $id ); } } add_action('init', 'open_wallet_handle_action' ); function open_wallet_register_link( $link ) { if ( ! is_user_logged_in() && get_option('users_can_register') ) { $link = str_replace("Login with openWallet
Or
Login Using openWallet »
What is openWallet?
Options saved.