Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from googleglass/reauth
Browse files Browse the repository at this point in the history
Bug fixes and improved MyGlass 'off' handling
  • Loading branch information
mimming committed Jul 3, 2013
2 parents 9381359 + 415e061 commit 066e46a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
6 changes: 1 addition & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
header('Location: ' . $base_url . '/oauth2callback.php');
exit;
} else {
verify_credentials(get_credentials($_SESSION['userid']));
$client->setAccessToken(get_credentials($_SESSION['userid']));
}

Expand Down Expand Up @@ -170,11 +171,6 @@
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Glassware Starter Project: PHP Edition</a>
<div class="nav-collapse collapse">
<form class="navbar-form pull-right" action="signout.php" method="post">
<button type="submit" class="btn">Sign out</button>
</form>
</div>
</div>
</div>
</div>
Expand Down
34 changes: 34 additions & 0 deletions mirror-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// found at https://developers.google.com/glass/v1/reference

require_once 'config.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

// Returns an unauthenticated service
function get_google_api_client() {
Expand Down Expand Up @@ -49,6 +51,38 @@ function get_google_api_client() {
return $client;
}

/*
* Verify the credentials. If they're broken, attempt to re-auth
* This will only work if you haven't printed anything yet (since
* it uses an HTTP header for the redirect)
*/
function verify_credentials($credentials) {
//TODO: Use the oauth2.tokeninfo() method instead once it's
// exposed by the PHP client library
global $base_url;

$client = get_google_api_client();
$client->setAccessToken($credentials);

$token_checker = new Google_Oauth2Service($client);
try {
$token_checker->userinfo->get();
} catch (Google_ServiceException $e) {
if($e->getCode() == 401) {
// This user may have disabled the Glassware on MyGlass.
// Clean up the mess and attempt to re-auth.
unset($_SESSION['userid']);
header('Location: ' . $base_url . '/oauth2callback.php');
exit;

} else {
// Let it go...
throw $e;
}
}

}

function insert_timeline_item($service, $timeline_item, $content_type, $attachment)
{
try {
Expand Down
5 changes: 3 additions & 2 deletions util.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function store_credentials($user_id, $credentials) {
$user_id = sqlite_escape_string(strip_tags($user_id));
$credentials = sqlite_escape_string(strip_tags($credentials));

$insert = "insert into credentials values ('$user_id', '$credentials')";
$insert = "insert or replace into credentials values ('$user_id', '$credentials')";
sqlite_exec($db, $insert);

}
Expand Down Expand Up @@ -59,7 +59,8 @@ function init_db() {
$db = sqlite_open($sqlite_database);
$test_query = "select count(*) from sqlite_master where name = 'credentials'";
if (sqlite_fetch_single(sqlite_query($db, $test_query)) == 0) {
$create_table = "create table credentials (userid text, credentials text);";
$create_table = "create table credentials (userid text not null unique, " .
"credentials text not null);";
sqlite_exec($db, $create_table);
}
return $db;
Expand Down

0 comments on commit 066e46a

Please sign in to comment.