103 |
- |
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 |
|
|
|
23 |
/**
|
|
|
24 |
* CredentialsLoader contains the behaviour used to locate and find default
|
|
|
25 |
* credentials files on the file system.
|
|
|
26 |
*/
|
|
|
27 |
abstract class CredentialsLoader implements FetchAuthTokenInterface
|
|
|
28 |
{
|
|
|
29 |
const TOKEN_CREDENTIAL_URI = 'https://www.googleapis.com/oauth2/v4/token';
|
|
|
30 |
const ENV_VAR = 'GOOGLE_APPLICATION_CREDENTIALS';
|
|
|
31 |
const WELL_KNOWN_PATH = 'gcloud/application_default_credentials.json';
|
|
|
32 |
const NON_WINDOWS_WELL_KNOWN_PATH_BASE = '.config';
|
|
|
33 |
const AUTH_METADATA_KEY = 'Authorization';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @param string $cause
|
|
|
37 |
* @return string
|
|
|
38 |
*/
|
|
|
39 |
private static function unableToReadEnv($cause)
|
|
|
40 |
{
|
|
|
41 |
$msg = 'Unable to read the credential file specified by ';
|
|
|
42 |
$msg .= ' GOOGLE_APPLICATION_CREDENTIALS: ';
|
|
|
43 |
$msg .= $cause;
|
|
|
44 |
|
|
|
45 |
return $msg;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @return bool
|
|
|
50 |
*/
|
|
|
51 |
private static function isOnWindows()
|
|
|
52 |
{
|
|
|
53 |
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Load a JSON key from the path specified in the environment.
|
|
|
58 |
*
|
|
|
59 |
* Load a JSON key from the path specified in the environment
|
|
|
60 |
* variable GOOGLE_APPLICATION_CREDENTIALS. Return null if
|
|
|
61 |
* GOOGLE_APPLICATION_CREDENTIALS is not specified.
|
|
|
62 |
*
|
|
|
63 |
* @return array JSON key | null
|
|
|
64 |
*/
|
|
|
65 |
public static function fromEnv()
|
|
|
66 |
{
|
|
|
67 |
$path = getenv(self::ENV_VAR);
|
|
|
68 |
if (empty($path)) {
|
|
|
69 |
return;
|
|
|
70 |
}
|
|
|
71 |
if (!file_exists($path)) {
|
|
|
72 |
$cause = 'file ' . $path . ' does not exist';
|
|
|
73 |
throw new \DomainException(self::unableToReadEnv($cause));
|
|
|
74 |
}
|
|
|
75 |
$jsonKey = file_get_contents($path);
|
|
|
76 |
return json_decode($jsonKey, true);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Load a JSON key from a well known path.
|
|
|
81 |
*
|
|
|
82 |
* The well known path is OS dependent:
|
|
|
83 |
* - windows: %APPDATA%/gcloud/application_default_credentials.json
|
|
|
84 |
* - others: $HOME/.config/gcloud/application_default_credentials.json
|
|
|
85 |
*
|
|
|
86 |
* If the file does not exists, this returns null.
|
|
|
87 |
*
|
|
|
88 |
* @return array JSON key | null
|
|
|
89 |
*/
|
|
|
90 |
public static function fromWellKnownFile()
|
|
|
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 = implode(DIRECTORY_SEPARATOR, $path);
|
|
|
99 |
if (!file_exists($path)) {
|
|
|
100 |
return;
|
|
|
101 |
}
|
|
|
102 |
$jsonKey = file_get_contents($path);
|
|
|
103 |
return json_decode($jsonKey, true);
|
|
|
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 |
* @param array $jsonKey the JSON credentials.
|
|
|
112 |
*
|
|
|
113 |
* @return ServiceAccountCredentials|UserRefreshCredentials
|
|
|
114 |
*/
|
|
|
115 |
public static function makeCredentials($scope, array $jsonKey)
|
|
|
116 |
{
|
|
|
117 |
if (!array_key_exists('type', $jsonKey)) {
|
|
|
118 |
throw new \InvalidArgumentException('json key is missing the type field');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if ($jsonKey['type'] == 'service_account') {
|
|
|
122 |
return new ServiceAccountCredentials($scope, $jsonKey);
|
|
|
123 |
} elseif ($jsonKey['type'] == 'authorized_user') {
|
|
|
124 |
return new UserRefreshCredentials($scope, $jsonKey);
|
|
|
125 |
} else {
|
|
|
126 |
throw new \InvalidArgumentException('invalid value in the type field');
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* export a callback function which updates runtime metadata.
|
|
|
132 |
*
|
|
|
133 |
* @return array updateMetadata function
|
|
|
134 |
*/
|
|
|
135 |
public function getUpdateMetadataFunc()
|
|
|
136 |
{
|
|
|
137 |
return array($this, 'updateMetadata');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Updates metadata with the authorization token.
|
|
|
142 |
*
|
|
|
143 |
* @param array $metadata metadata hashmap
|
|
|
144 |
* @param string $authUri optional auth uri
|
|
|
145 |
* @param callable $httpHandler callback which delivers psr7 request
|
|
|
146 |
*
|
|
|
147 |
* @return array updated metadata hashmap
|
|
|
148 |
*/
|
|
|
149 |
public function updateMetadata(
|
|
|
150 |
$metadata,
|
|
|
151 |
$authUri = null,
|
|
|
152 |
callable $httpHandler = null
|
|
|
153 |
) {
|
|
|
154 |
$result = $this->fetchAuthToken($httpHandler);
|
|
|
155 |
if (!isset($result['access_token'])) {
|
|
|
156 |
return $metadata;
|
|
|
157 |
}
|
|
|
158 |
$metadata_copy = $metadata;
|
|
|
159 |
$metadata_copy[self::AUTH_METADATA_KEY] = array('Bearer ' . $result['access_token']);
|
|
|
160 |
|
|
|
161 |
return $metadata_copy;
|
|
|
162 |
}
|
|
|
163 |
}
|