public
Created

A "Google Identity Toolkit" Demo-WordPress-Plugin

  • Download Gist
identity-toolkit.php
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
<?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;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.