Skip to content

Commit 066e46a

Browse files
committed
Merge pull request googleglass#6 from googleglass/reauth
Bug fixes and improved MyGlass 'off' handling
2 parents 9381359 + 415e061 commit 066e46a

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

index.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
header('Location: ' . $base_url . '/oauth2callback.php');
3030
exit;
3131
} else {
32+
verify_credentials(get_credentials($_SESSION['userid']));
3233
$client->setAccessToken(get_credentials($_SESSION['userid']));
3334
}
3435

@@ -170,11 +171,6 @@
170171
<div class="navbar-inner">
171172
<div class="container">
172173
<a class="brand" href="#">Glassware Starter Project: PHP Edition</a>
173-
<div class="nav-collapse collapse">
174-
<form class="navbar-form pull-right" action="signout.php" method="post">
175-
<button type="submit" class="btn">Sign out</button>
176-
</form>
177-
</div>
178174
</div>
179175
</div>
180176
</div>

mirror-client.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// found at https://developers.google.com/glass/v1/reference
2323

2424
require_once 'config.php';
25+
require_once 'google-api-php-client/src/Google_Client.php';
26+
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
2527

2628
// Returns an unauthenticated service
2729
function get_google_api_client() {
@@ -49,6 +51,38 @@ function get_google_api_client() {
4951
return $client;
5052
}
5153

54+
/*
55+
* Verify the credentials. If they're broken, attempt to re-auth
56+
* This will only work if you haven't printed anything yet (since
57+
* it uses an HTTP header for the redirect)
58+
*/
59+
function verify_credentials($credentials) {
60+
//TODO: Use the oauth2.tokeninfo() method instead once it's
61+
// exposed by the PHP client library
62+
global $base_url;
63+
64+
$client = get_google_api_client();
65+
$client->setAccessToken($credentials);
66+
67+
$token_checker = new Google_Oauth2Service($client);
68+
try {
69+
$token_checker->userinfo->get();
70+
} catch (Google_ServiceException $e) {
71+
if($e->getCode() == 401) {
72+
// This user may have disabled the Glassware on MyGlass.
73+
// Clean up the mess and attempt to re-auth.
74+
unset($_SESSION['userid']);
75+
header('Location: ' . $base_url . '/oauth2callback.php');
76+
exit;
77+
78+
} else {
79+
// Let it go...
80+
throw $e;
81+
}
82+
}
83+
84+
}
85+
5286
function insert_timeline_item($service, $timeline_item, $content_type, $attachment)
5387
{
5488
try {

util.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function store_credentials($user_id, $credentials) {
2828
$user_id = sqlite_escape_string(strip_tags($user_id));
2929
$credentials = sqlite_escape_string(strip_tags($credentials));
3030

31-
$insert = "insert into credentials values ('$user_id', '$credentials')";
31+
$insert = "insert or replace into credentials values ('$user_id', '$credentials')";
3232
sqlite_exec($db, $insert);
3333

3434
}
@@ -59,7 +59,8 @@ function init_db() {
5959
$db = sqlite_open($sqlite_database);
6060
$test_query = "select count(*) from sqlite_master where name = 'credentials'";
6161
if (sqlite_fetch_single(sqlite_query($db, $test_query)) == 0) {
62-
$create_table = "create table credentials (userid text, credentials text);";
62+
$create_table = "create table credentials (userid text not null unique, " .
63+
"credentials text not null);";
6364
sqlite_exec($db, $create_table);
6465
}
6566
return $db;

0 commit comments

Comments
 (0)