Created
January 10, 2012 17:46
-
-
Save pfefferle/1590200 to your computer and use it in GitHub Desktop.
A "Google Identity Toolkit" Demo-WordPress-Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Identity Toolkit Demo | |
Plugin URI: | |
Description: a quick and dirty Identity Toolkit-plugin | |
Version: demo | |
Author: Matthias Pfefferle | |
Author URI: http://notizblog.org/ | |
*/ | |
################################################################################################### | |
# # | |
# this is only a demo plugin, if you search for a fully functional Identity Toolkit WordPress # | |
# plugin, search here: http://wordpress.org/extend/plugins/search.php?q=google+identity+toolkit # | |
# # | |
################################################################################################### | |
define("IT_API_KEY", "AIzaSyBKMok8BprpkZprfyqFeS43Cw2Q1op12Jk"); | |
// add the BrowserID javascript-code to the header | |
add_action('login_head', 'it_add_js_header'); | |
function it_add_js_header() { | |
?> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/googleapis/0.0.4/googleapis.min.js"></script> | |
<script type="text/javascript" src="https://ajax.googleapis.com/jsapi"></script> | |
<script type="text/javascript"> | |
google.load("identitytoolkit", "1.0", {packages: ["ac"]}); | |
</script> | |
<script type="text/javascript"> | |
$(function(){ | |
window.google.identitytoolkit.setConfig({ | |
developerKey: "<?php echo IT_API_KEY; ?>", | |
companyName: "Demo", | |
callbackUrl: "<?php echo trailingslashit(get_site_url()); ?>?identity-toolkit=callback", // must be a full URL | |
userStatusUrl: "<?php echo trailingslashit(get_site_url()); ?>?identity-toolkit=user-status", // these can just be partial paths | |
loginUrl: "<?php echo trailingslashit(get_site_url()); ?>?identity-toolkit=login", | |
signupUrl: "<?php echo site_url('wp-login.php?action=register', 'login'); ?>", | |
homeUrl: "<?php echo get_site_url(); ?>", | |
logoutUrl: "<?php echo wp_logout_url(); ?>", | |
realm: "", // optional | |
language: "en", | |
idps: ["Gmail", "AOL", "Hotmail", "Yahoo"], | |
tryFederatedFirst: true, | |
useCachedUserStatus: false | |
}); | |
$('.forgetmenot').accountChooser(); | |
}); | |
</script> | |
<?php | |
} | |
// add 'browser_id_assertion' as wordpress query var | |
add_filter('query_vars', 'it_query_vars'); | |
function it_query_vars($vars) { | |
$vars[] = 'identity-toolkit'; | |
$vars[] = 'email'; | |
$vars[] = 'password'; | |
return $vars; | |
} | |
add_action('parse_request', 'it_request_delegater'); | |
function it_request_delegater() { | |
global $wp_query, $wp, $user; | |
// check if it is an identity-toolkit request | |
if( array_key_exists('identity-toolkit', $wp->query_vars) ) { | |
// choose what to do | |
switch ($wp->query_vars['identity-toolkit']) { | |
// returns the user status | |
case "user-status": | |
$return = '{ "registered": false }'; | |
// get email address | |
if ($email = $wp->query_vars['email']) { | |
$userdata = get_user_by('email', $email); | |
if ($userdata) | |
$return = '{ "registered": true }'; | |
} | |
// send json response | |
header('Content-type: application/json'); | |
echo $return; | |
exit; | |
// try to login the user | |
case "login": | |
$return = '{ "status": "passwordError" }'; | |
if ($userdata = get_user_by('email', $wp->query_vars['email'])) { | |
if (wp_check_password($wp->query_vars['password'], $userdata->user_pass, $userdata->ID)) { | |
$user = new WP_User($userdata->ID); | |
wp_set_current_user($userdata->ID, $userdata->user_login); | |
wp_set_auth_cookie($userdata->ID, true); | |
do_action('wp_login', $userdata->user_login); | |
$return = '{ "status": "OK" }'; | |
} | |
} | |
// send json response | |
header('Content-type: application/json'); | |
echo $return; | |
exit; | |
case "callback": | |
$args = array( | |
'method' => 'POST', | |
'timeout' => 30, | |
'redirection' => 0, | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array('Content-Type: application/json'), | |
'body' => json_encode(array( | |
'requestUri' => get_site_url(), | |
'postBody' => $_REQUEST // verify the response params | |
)), | |
'cookies' => array(), | |
'sslverify' => 0 | |
); | |
$response = wp_remote_post("https://www.googleapis.com/identitytoolkit/v1/relyingparty/verifyAssertion?key=".IT_API_KEY, $args); | |
if (!is_wp_error($response)) { | |
// if request works login or register | |
} else { | |
// show error | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment