No Description

ServiceAccountJwtAccessCredentials.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Google's Service Account credentials via
  22. * JWT Access.
  23. *
  24. * This class allows authorizing requests for service accounts directly
  25. * from credentials from a json key file downloaded from the developer
  26. * console (via 'Generate new Json Key'). It is not part of any OAuth2
  27. * flow, rather it creates a JWT and sends that as a credential.
  28. */
  29. class ServiceAccountJwtAccessCredentials extends CredentialsLoader
  30. {
  31. /**
  32. * The OAuth2 instance used to conduct authorization.
  33. *
  34. * @var OAuth2
  35. */
  36. protected $auth;
  37. /**
  38. * Create a new ServiceAccountJwtAccessCredentials.
  39. *
  40. * @param string|array $jsonKey JSON credential file path or JSON credentials
  41. * as an associative array
  42. */
  43. public function __construct($jsonKey)
  44. {
  45. if (is_string($jsonKey)) {
  46. if (!file_exists($jsonKey)) {
  47. throw new \InvalidArgumentException('file does not exist');
  48. }
  49. $jsonKeyStream = file_get_contents($jsonKey);
  50. if (!$jsonKey = json_decode($jsonKeyStream, true)) {
  51. throw new \LogicException('invalid json for auth config');
  52. }
  53. }
  54. if (!array_key_exists('client_email', $jsonKey)) {
  55. throw new \InvalidArgumentException(
  56. 'json key is missing the client_email field');
  57. }
  58. if (!array_key_exists('private_key', $jsonKey)) {
  59. throw new \InvalidArgumentException(
  60. 'json key is missing the private_key field');
  61. }
  62. $this->auth = new OAuth2([
  63. 'issuer' => $jsonKey['client_email'],
  64. 'sub' => $jsonKey['client_email'],
  65. 'signingAlgorithm' => 'RS256',
  66. 'signingKey' => $jsonKey['private_key'],
  67. ]);
  68. }
  69. /**
  70. * Updates metadata with the authorization token.
  71. *
  72. * @param array $metadata metadata hashmap
  73. * @param string $authUri optional auth uri
  74. * @param callable $httpHandler callback which delivers psr7 request
  75. *
  76. * @return array updated metadata hashmap
  77. */
  78. public function updateMetadata(
  79. $metadata,
  80. $authUri = null,
  81. callable $httpHandler = null
  82. ) {
  83. if (empty($authUri)) {
  84. return $metadata;
  85. }
  86. $this->auth->setAudience($authUri);
  87. return parent::updateMetadata($metadata, $authUri, $httpHandler);
  88. }
  89. /**
  90. * Implements FetchAuthTokenInterface#fetchAuthToken.
  91. *
  92. * @param callable $httpHandler
  93. *
  94. * @return array|void
  95. */
  96. public function fetchAuthToken(callable $httpHandler = null)
  97. {
  98. $audience = $this->auth->getAudience();
  99. if (empty($audience)) {
  100. return null;
  101. }
  102. $access_token = $this->auth->toJwt();
  103. return array('access_token' => $access_token);
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getCacheKey()
  109. {
  110. return $this->auth->getCacheKey();
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getLastReceivedToken()
  116. {
  117. return $this->auth->getLastReceivedToken();
  118. }
  119. }