PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / src / CredentialsLoader.php

CredentialsLoader.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/Google/vendor/google/auth/src/CredentialsLoader.php

168 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
18 namespace Google\Auth;
19
20 use Google\Auth\Credentials\ServiceAccountCredentials;
21 use Google\Auth\Credentials\UserRefreshCredentials;
22 use GuzzleHttp\Psr7;
23 use Psr\Http\Message\StreamInterface;
24
25 /**
26 * CredentialsLoader contains the behaviour used to locate and find default
27 * credentials files on the file system.
28 */
29 abstract class CredentialsLoader implements FetchAuthTokenInterface
30 {
31 const TOKEN_CREDENTIAL_URI = 'https://www.googleapis.com/oauth2/v4/token';
32 const ENV_VAR = 'GOOGLE_APPLICATION_CREDENTIALS';
33 const WELL_KNOWN_PATH = 'gcloud/application_default_credentials.json';
34 const NON_WINDOWS_WELL_KNOWN_PATH_BASE = '.config';
35 const AUTH_METADATA_KEY = 'Authorization';
36
37 private static function unableToReadEnv($cause)
38 {
39 $msg = 'Unable to read the credential file specified by ';
40 $msg .= ' GOOGLE_APPLICATION_CREDENTIALS: ';
41 $msg .= $cause;
42 return $msg;
43 }
44
45 private static function isOnWindows()
46 {
47 return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
48 }
49
50 /**
51 * Create a credentials instance from the path specified in the environment.
52 *
53 * Creates a credentials instance from the path specified in the environment
54 * variable GOOGLE_APPLICATION_CREDENTIALS. Return null if
55 * GOOGLE_APPLICATION_CREDENTIALS is not specified.
56 *
57 * @param string|array scope the scope of the access request, expressed
58 * either as an Array or as a space-delimited String.
59 *
60 * @return a Credentials instance | null
61 */
62 public static function fromEnv($scope = null)
63 {
64 $path = getenv(self::ENV_VAR);
65 if (empty($path)) {
66 return null;
67 }
68 if (!file_exists($path)) {
69 $cause = "file " . $path . " does not exist";
70 throw new \DomainException(self::unableToReadEnv($cause));
71 }
72 $keyStream = Psr7\stream_for(file_get_contents($path));
73 return static::makeCredentials($scope, $keyStream);
74 }
75
76 /**
77 * Create a credentials instance from a well known path.
78 *
79 * The well known path is OS dependent:
80 * - windows: %APPDATA%/gcloud/application_default_credentials.json
81 * - others: $HOME/.config/gcloud/application_default_credentials.json
82 *
83 * If the file does not exists, this returns null.
84 *
85 * @param string|array scope the scope of the access request, expressed
86 * either as an Array or as a space-delimited String.
87 *
88 * @return a Credentials instance | null
89 */
90 public static function fromWellKnownFile($scope = null)
91 {
92 $rootEnv = self::isOnWindows() ? 'APPDATA' : 'HOME';
93 $path = [getenv($rootEnv)];
94 if (!self::isOnWindows()) {
95 $path[] = self::NON_WINDOWS_WELL_KNOWN_PATH_BASE;
96 }
97 $path[] = self::WELL_KNOWN_PATH;
98 $path = join(DIRECTORY_SEPARATOR, $path);
99 if (!file_exists($path)) {
100 return null;
101 }
102 $keyStream = Psr7\stream_for(file_get_contents($path));
103 return static::makeCredentials($scope, $keyStream);
104 }
105
106 /**
107 * Create a new Credentials instance.
108 *
109 * @param string|array scope the scope of the access request, expressed
110 * either as an Array or as a space-delimited String.
111 *
112 * @param StreamInterface jsonKeyStream read it to get the JSON credentials.
113 *
114 */
115 public static function makeCredentials($scope, StreamInterface $jsonKeyStream)
116 {
117 $jsonKey = json_decode($jsonKeyStream->getContents(), true);
118 if (!array_key_exists('type', $jsonKey)) {
119 throw new \InvalidArgumentException(
120 'json key is missing the type field');
121 }
122
123 if ($jsonKey['type'] == 'service_account') {
124 return new ServiceAccountCredentials($scope, $jsonKey);
125
126 } else if ($jsonKey['type'] == 'authorized_user') {
127 return new UserRefreshCredentials($scope, $jsonKey);
128
129 } else {
130 throw new \InvalidArgumentException(
131 'invalid value in the type field');
132 }
133 }
134
135 /**
136 * export a callback function which updates runtime metadata
137 *
138 * @return an updateMetadata function
139 */
140 public function getUpdateMetadataFunc()
141 {
142 return array($this, 'updateMetadata');
143 }
144
145 /**
146 * Updates metadata with the authorization token
147 *
148 * @param array $metadata metadata hashmap
149 * @param string $authUri optional auth uri
150 * @param callable $httpHandler callback which delivers psr7 request
151 *
152 * @return array updated metadata hashmap
153 */
154 public function updateMetadata(
155 $metadata,
156 $authUri = null,
157 callable $httpHandler = null
158 ) {
159 $result = $this->fetchAuthToken($httpHandler);
160 if (!isset($result['access_token'])) {
161 return $metadata;
162 }
163 $metadata_copy = $metadata;
164 $metadata_copy[self::AUTH_METADATA_KEY] = array('Bearer ' . $result['access_token']);
165 return $metadata_copy;
166 }
167 }
168