| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2011 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
include_once "templates/base.php"; |
| 18 |
session_start(); |
| 19 |
|
| 20 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 21 |
|
| 22 |
/************************************************ |
| 23 |
ATTENTION: Fill in these values! Make sure |
| 24 |
the redirect URI is to this page, e.g: |
| 25 |
http://localhost:8080/user-example.php |
| 26 |
************************************************/ |
| 27 |
$client_id = '<YOUR_CLIENT_ID>'; |
| 28 |
$client_secret = '<YOUR_CLIENT_SECRET>'; |
| 29 |
$redirect_uri = '<YOUR_REDIRECT_URI>'; |
| 30 |
|
| 31 |
$client = new Google_Client(); |
| 32 |
$client->setClientId($client_id); |
| 33 |
$client->setClientSecret($client_secret); |
| 34 |
$client->setRedirectUri($redirect_uri); |
| 35 |
$client->setScopes('email'); |
| 36 |
|
| 37 |
/************************************************ |
| 38 |
If we're logging out we just need to clear our |
| 39 |
local access token in this case |
| 40 |
************************************************/ |
| 41 |
if (isset($_REQUEST['logout'])) { |
| 42 |
unset($_SESSION['access_token']); |
| 43 |
} |
| 44 |
|
| 45 |
/************************************************ |
| 46 |
If we have a code back from the OAuth 2.0 flow, |
| 47 |
we need to exchange that with the authenticate() |
| 48 |
function. We store the resultant access token |
| 49 |
bundle in the session, and redirect to ourself. |
| 50 |
************************************************/ |
| 51 |
if (isset($_GET['code'])) { |
| 52 |
$client->authenticate($_GET['code']); |
| 53 |
$_SESSION['access_token'] = $client->getAccessToken(); |
| 54 |
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 55 |
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 56 |
} |
| 57 |
|
| 58 |
/************************************************ |
| 59 |
If we have an access token, we can make |
| 60 |
requests, else we generate an authentication URL. |
| 61 |
************************************************/ |
| 62 |
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { |
| 63 |
$client->setAccessToken($_SESSION['access_token']); |
| 64 |
} else { |
| 65 |
$authUrl = $client->createAuthUrl(); |
| 66 |
} |
| 67 |
|
| 68 |
/************************************************ |
| 69 |
If we're signed in we can go ahead and retrieve |
| 70 |
the ID token, which is part of the bundle of |
| 71 |
data that is exchange in the authenticate step |
| 72 |
- we only need to do a network call if we have |
| 73 |
to retrieve the Google certificate to verify it, |
| 74 |
and that can be cached. |
| 75 |
************************************************/ |
| 76 |
if ($client->getAccessToken()) { |
| 77 |
$_SESSION['access_token'] = $client->getAccessToken(); |
| 78 |
$token_data = $client->verifyIdToken()->getAttributes(); |
| 79 |
} |
| 80 |
|
| 81 |
echo pageHeader("User Query - Retrieving An Id Token"); |
| 82 |
if (strpos($client_id, "googleusercontent") == false) { |
| 83 |
echo missingClientSecretsWarning(); |
| 84 |
exit; |
| 85 |
} |
| 86 |
?> |
| 87 |
<div class="box"> |
| 88 |
<div class="request"> |
| 89 |
<?php |
| 90 |
if (isset($authUrl)) { |
| 91 |
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; |
| 92 |
} else { |
| 93 |
echo "<a class='logout' href='?logout'>Logout</a>"; |
| 94 |
} |
| 95 |
?> |
| 96 |
</div> |
| 97 |
|
| 98 |
<div class="data"> |
| 99 |
<?php |
| 100 |
if (isset($token_data)) { |
| 101 |
var_dump($token_data); |
| 102 |
} |
| 103 |
?> |
| 104 |
</div> |
| 105 |
</div> |
| 106 |
<?php |
| 107 |
echo pageFooter(__FILE__); |
| 108 |
|