GoogleStrategy.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Google strategy for Opauth
  4. * based on https://developers.google.com/accounts/docs/OAuth2
  5. *
  6. * More information on Opauth: http://opauth.org
  7. *
  8. * @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
  9. * @link http://opauth.org
  10. * @package Opauth.GoogleStrategy
  11. * @license MIT License
  12. */
  13. /**
  14. * Google strategy for Opauth
  15. * based on https://developers.google.com/accounts/docs/OAuth2
  16. *
  17. * @package Opauth.Google
  18. */
  19. class GoogleStrategy extends OpauthStrategy{
  20. /**
  21. * Compulsory config keys, listed as unassociative arrays
  22. */
  23. public $expects = array('client_id', 'client_secret');
  24. /**
  25. * Optional config keys, without predefining any default values.
  26. */
  27. public $optionals = array('redirect_uri', 'scope', 'state', 'access_type', 'approval_prompt');
  28. /**
  29. * Optional config keys with respective default values, listed as associative arrays
  30. * eg. array('scope' => 'email');
  31. */
  32. public $defaults = array(
  33. 'redirect_uri' => '{complete_url_to_strategy}oauth2callback',
  34. 'scope' => 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'
  35. );
  36. /**
  37. * Auth request
  38. */
  39. public function request(){
  40. $url = 'https://accounts.google.com/o/oauth2/auth';
  41. $params = array(
  42. 'client_id' => $this->strategy['client_id'],
  43. 'redirect_uri' => $this->strategy['redirect_uri'],
  44. 'response_type' => 'code',
  45. 'scope' => $this->strategy['scope']
  46. );
  47. foreach ($this->optionals as $key){
  48. if (!empty($this->strategy[$key])) $params[$key] = $this->strategy[$key];
  49. }
  50. $this->clientGet($url, $params);
  51. }
  52. /**
  53. * Internal callback, after OAuth
  54. */
  55. public function oauth2callback(){
  56. if (array_key_exists('code', $_GET) && !empty($_GET['code'])){
  57. $code = $_GET['code'];
  58. $url = 'https://accounts.google.com/o/oauth2/token';
  59. $params = array(
  60. 'code' => $code,
  61. 'client_id' => $this->strategy['client_id'],
  62. 'client_secret' => $this->strategy['client_secret'],
  63. 'redirect_uri' => $this->strategy['redirect_uri'],
  64. 'grant_type' => 'authorization_code'
  65. );
  66. $response = $this->serverPost($url, $params, null, $headers);
  67. $results = json_decode($response);
  68. if (!empty($results) && !empty($results->access_token)){
  69. $userinfo = $this->userinfo($results->access_token);
  70. $this->auth = array(
  71. 'uid' => $userinfo['id'],
  72. 'info' => array(),
  73. 'credentials' => array(
  74. 'token' => $results->access_token,
  75. 'expires' => date('c', time() + $results->expires_in)
  76. ),
  77. 'raw' => $userinfo
  78. );
  79. if (!empty($results->refresh_token))
  80. {
  81. $this->auth['credentials']['refresh_token'] = $results->refresh_token;
  82. }
  83. $this->mapProfile($userinfo, 'name', 'info.name');
  84. $this->mapProfile($userinfo, 'email', 'info.email');
  85. $this->mapProfile($userinfo, 'given_name', 'info.first_name');
  86. $this->mapProfile($userinfo, 'family_name', 'info.last_name');
  87. $this->mapProfile($userinfo, 'picture', 'info.image');
  88. $this->callback();
  89. }
  90. else{
  91. $error = array(
  92. 'code' => 'access_token_error',
  93. 'message' => 'Failed when attempting to obtain access token',
  94. 'raw' => array(
  95. 'response' => $response,
  96. 'headers' => $headers
  97. )
  98. );
  99. $this->errorCallback($error);
  100. }
  101. }
  102. else{
  103. $error = array(
  104. 'code' => 'oauth2callback_error',
  105. 'raw' => $_GET
  106. );
  107. $this->errorCallback($error);
  108. }
  109. }
  110. /**
  111. * Queries Google API for user info
  112. *
  113. * @param string $access_token
  114. * @return array Parsed JSON results
  115. */
  116. private function userinfo($access_token){
  117. $userinfo = $this->serverGet('https://www.googleapis.com/oauth2/v1/userinfo', array('access_token' => $access_token), null, $headers);
  118. if (!empty($userinfo)){
  119. return $this->recursiveGetObjectVars(json_decode($userinfo));
  120. }
  121. else{
  122. $error = array(
  123. 'code' => 'userinfo_error',
  124. 'message' => 'Failed when attempting to query for user information',
  125. 'raw' => array(
  126. 'response' => $userinfo,
  127. 'headers' => $headers
  128. )
  129. );
  130. $this->errorCallback($error);
  131. }
  132. }
  133. }