Ei kuvausta

UserRefreshCredentials.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * Copyright 2015 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. namespace Google\Auth\Credentials;
  18. use Google\Auth\CredentialsLoader;
  19. use Google\Auth\OAuth2;
  20. /**
  21. * Authenticates requests using User Refresh credentials.
  22. *
  23. * This class allows authorizing requests from user refresh tokens.
  24. *
  25. * This the end of the result of a 3LO flow. E.g, the end result of
  26. * 'gcloud auth login' saves a file with these contents in well known
  27. * location
  28. *
  29. * @see [Application Default Credentials](http://goo.gl/mkAHpZ)
  30. */
  31. class UserRefreshCredentials extends CredentialsLoader
  32. {
  33. /**
  34. * The OAuth2 instance used to conduct authorization.
  35. *
  36. * @var OAuth2
  37. */
  38. protected $auth;
  39. /**
  40. * Create a new UserRefreshCredentials.
  41. *
  42. * @param string|array $scope the scope of the access request, expressed
  43. * either as an Array or as a space-delimited String.
  44. * @param string|array $jsonKey JSON credential file path or JSON credentials
  45. * as an associative array
  46. */
  47. public function __construct(
  48. $scope,
  49. $jsonKey
  50. ) {
  51. if (is_string($jsonKey)) {
  52. if (!file_exists($jsonKey)) {
  53. throw new \InvalidArgumentException('file does not exist');
  54. }
  55. $jsonKeyStream = file_get_contents($jsonKey);
  56. if (!$jsonKey = json_decode($jsonKeyStream, true)) {
  57. throw new \LogicException('invalid json for auth config');
  58. }
  59. }
  60. if (!array_key_exists('client_id', $jsonKey)) {
  61. throw new \InvalidArgumentException(
  62. 'json key is missing the client_id field');
  63. }
  64. if (!array_key_exists('client_secret', $jsonKey)) {
  65. throw new \InvalidArgumentException(
  66. 'json key is missing the client_secret field');
  67. }
  68. if (!array_key_exists('refresh_token', $jsonKey)) {
  69. throw new \InvalidArgumentException(
  70. 'json key is missing the refresh_token field');
  71. }
  72. $this->auth = new OAuth2([
  73. 'clientId' => $jsonKey['client_id'],
  74. 'clientSecret' => $jsonKey['client_secret'],
  75. 'refresh_token' => $jsonKey['refresh_token'],
  76. 'scope' => $scope,
  77. 'tokenCredentialUri' => self::TOKEN_CREDENTIAL_URI,
  78. ]);
  79. }
  80. /**
  81. * @param callable $httpHandler
  82. *
  83. * @return array
  84. */
  85. public function fetchAuthToken(callable $httpHandler = null)
  86. {
  87. return $this->auth->fetchAuthToken($httpHandler);
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function getCacheKey()
  93. {
  94. return $this->auth->getClientId() . ':' . $this->auth->getCacheKey();
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getLastReceivedToken()
  100. {
  101. return $this->auth->getLastReceivedToken();
  102. }
  103. }