idtoken.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
  20. /************************************************
  21. ATTENTION: Fill in these values! Make sure
  22. the redirect URI is to this page, e.g:
  23. http://localhost:8080/user-example.php
  24. ************************************************/
  25. $client_id = '<YOUR_CLIENT_ID>';
  26. $client_secret = '<YOUR_CLIENT_SECRET>';
  27. $redirect_uri = '<YOUR_REDIRECT_URI>';
  28. $client = new Google_Client();
  29. $client->setClientId($client_id);
  30. $client->setClientSecret($client_secret);
  31. $client->setRedirectUri($redirect_uri);
  32. $client->setScopes('email');
  33. /************************************************
  34. If we're logging out we just need to clear our
  35. local access token in this case
  36. ************************************************/
  37. if (isset($_REQUEST['logout'])) {
  38. unset($_SESSION['access_token']);
  39. }
  40. /************************************************
  41. If we have a code back from the OAuth 2.0 flow,
  42. we need to exchange that with the authenticate()
  43. function. We store the resultant access token
  44. bundle in the session, and redirect to ourself.
  45. ************************************************/
  46. if (isset($_GET['code'])) {
  47. $client->authenticate($_GET['code']);
  48. $_SESSION['access_token'] = $client->getAccessToken();
  49. $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  50. header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  51. }
  52. /************************************************
  53. If we have an access token, we can make
  54. requests, else we generate an authentication URL.
  55. ************************************************/
  56. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  57. $client->setAccessToken($_SESSION['access_token']);
  58. } else {
  59. $authUrl = $client->createAuthUrl();
  60. }
  61. /************************************************
  62. If we're signed in we can go ahead and retrieve
  63. the ID token, which is part of the bundle of
  64. data that is exchange in the authenticate step
  65. - we only need to do a network call if we have
  66. to retrieve the Google certificate to verify it,
  67. and that can be cached.
  68. ************************************************/
  69. if ($client->getAccessToken()) {
  70. $_SESSION['access_token'] = $client->getAccessToken();
  71. $token_data = $client->verifyIdToken()->getAttributes();
  72. }
  73. echo pageHeader("User Query - Retrieving An Id Token");
  74. if (strpos($client_id, "googleusercontent") == false) {
  75. echo missingClientSecretsWarning();
  76. exit;
  77. }
  78. ?>
  79. <div class="box">
  80. <div class="request">
  81. <?php
  82. if (isset($authUrl)) {
  83. echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  84. } else {
  85. echo "<a class='logout' href='?logout'>Logout</a>";
  86. }
  87. ?>
  88. </div>
  89. <div class="data">
  90. <?php
  91. if (isset($token_data)) {
  92. var_dump($token_data);
  93. }
  94. ?>
  95. </div>
  96. </div>
  97. <?php
  98. echo pageFooter(__FILE__);