Skip to content

Commit f5dfbe2

Browse files
authored
Merge pull request GoogleCloudPlatform#425 from GoogleCloudPlatform/auth-samples
Auth samples - added HTTP samples
2 parents 8df2776 + 6b959aa commit f5dfbe2

File tree

6 files changed

+174
-1
lines changed

6 files changed

+174
-1
lines changed

auth/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ methods will work on any Google Cloud API.
2727
auth-cloud-explicit Authenticate to a cloud client library using a service account explicitly.
2828
auth-api-implicit Authenticate to a cloud API using a service account implicitly.
2929
auth-api-explicit Authenticate to a cloud API using a service account explicitly.
30+
auth-http-implicit Authenticate to a cloud API with HTTP using a service account implicitly.
31+
auth-http-explicit Authenticate to a cloud API with HTTP using a service account explicitly.
3032
```
3133
6. The following commands are available but will throw a ServiceException when
3234
run from command-line. The Compute Engine method only works on Compute Engine,

auth/auth.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,43 @@
168168
})
169169
);
170170

171+
// Create auth-http-implicit Command.
172+
$application->add((new Command('auth-http-implicit'))
173+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
174+
->setDescription('Authenticate to a cloud API with HTTP using a service account implicitly.')
175+
->setHelp(<<
176+
The %command.name% command authenticates to a cloud API with HTTP
177+
using a service account implicitly.
178+
179+
php %command.full_name%
180+
181+
EOF
182+
)
183+
->setCode(function ($input, $output) {
184+
auth_http_implicit($input->getArgument('projectId'));
185+
})
186+
);
187+
188+
// Create auth-http-explicit Command.
189+
$application->add((new Command('auth-http-explicit'))
190+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
191+
->addArgument('serviceAccountPath', InputArgument::REQUIRED, 'Path to your service account.')
192+
->setDescription('Authenticate to a cloud API with HTTP using a service account explicitly.')
193+
->setHelp(<<
194+
The %command.name% command authenticates to a cloud API with HTTP
195+
using a service account explicitly.
196+
197+
php %command.full_name%
198+
199+
EOF
200+
)
201+
->setCode(function ($input, $output) {
202+
$projectId = $input->getArgument('projectId');
203+
$serviceAccountPath = $input->getArgument('serviceAccountPath');
204+
auth_http_explicit($projectId, $serviceAccountPath);
205+
})
206+
);
207+
171208
if (getenv('PHPUNIT_TESTS') === '1') {
172209
return $application;
173210
}

auth/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"src/auth_api_implicit.php",
1818
"src/auth_api_explicit.php",
1919
"src/auth_api_explicit_compute_engine.php",
20-
"src/auth_api_explicit_app_engine.php"
20+
"src/auth_api_explicit_app_engine.php",
21+
"src/auth_http_implicit.php",
22+
"src/auth_http_explicit.php"
2123
]
2224
},
2325
"require-dev": {

auth/src/auth_http_explicit.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* Copyright 2017 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+
* For instructions on how to run the full sample:
19+
*
20+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/auth/README.md
21+
*/
22+
23+
# [START auth_http_explicit]
24+
namespace Google\Cloud\Samples\Auth;
25+
26+
# Imports Auth libraries and Guzzle HTTP libraries.
27+
use Google\Auth\Credentials\ServiceAccountCredentials;
28+
use Google\Auth\Middleware\AuthTokenMiddleware;
29+
use GuzzleHttp\Client;
30+
use GuzzleHttp\HandlerStack;
31+
32+
function auth_http_explicit($projectId, $serviceAccountPath)
33+
{
34+
# Construct service account credentials using the service account key file
35+
# and Google Auth library's ServiceAccountCredentials class.
36+
$serviceAccountCredentials = new ServiceAccountCredentials(
37+
'https://www.googleapis.com/auth/cloud-platform',
38+
$serviceAccountPath);
39+
$middleware = new AuthTokenMiddleware($serviceAccountCredentials);
40+
$stack = HandlerStack::create();
41+
$stack->push($middleware);
42+
43+
# Create an HTTP Client using Guzzle and pass in the credentials.
44+
$http_client = new Client([
45+
'handler' => $stack,
46+
'base_uri' => 'https://www.googleapis.com/storage/v1/',
47+
'auth' => 'google_auth'
48+
]);
49+
50+
# Make an authenticated API request (listing storage buckets)
51+
$query = ['project' => $projectId];
52+
$response = $http_client->request('GET', 'b', [
53+
'query' => $query
54+
]);
55+
$body_content = json_decode((string) $response->getBody());
56+
foreach ($body_content->items as $item) {
57+
$bucket = $item->id;
58+
printf('Bucket: %s' . PHP_EOL, $bucket);
59+
}
60+
}
61+
# [END auth_http_explicit]

auth/src/auth_http_implicit.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2017 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+
* For instructions on how to run the full sample:
19+
*
20+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/auth/README.md
21+
*/
22+
23+
# [START auth_http_implicit]
24+
namespace Google\Cloud\Samples\Auth;
25+
26+
# Imports Auth libraries and Guzzle HTTP libraries.
27+
use Google\Auth\ApplicationDefaultCredentials;
28+
use GuzzleHttp\Client;
29+
use GuzzleHttp\HandlerStack;
30+
31+
function auth_http_implicit($projectId)
32+
{
33+
# Get the credentials and project ID from the environment using Google Auth
34+
# library's ApplicationDefaultCredentials class.
35+
$middleware = ApplicationDefaultCredentials::getMiddleware(
36+
'https://www.googleapis.com/auth/cloud-platform');
37+
$stack = HandlerStack::create();
38+
$stack->push($middleware);
39+
40+
# Create a HTTP Client using Guzzle and pass in the credentials.
41+
$http_client = new Client([
42+
'handler' => $stack,
43+
'base_uri' => 'https://www.googleapis.com/storage/v1/',
44+
'auth' => 'google_auth'
45+
]);
46+
47+
# Make an authenticated API request (listing storage buckets)
48+
$query = ['project' => $projectId];
49+
$response = $http_client->request('GET', 'b', [
50+
'query' => $query
51+
]);
52+
$body_content = json_decode((string) $response->getBody());
53+
foreach ($body_content->items as $item) {
54+
$bucket = $item->id;
55+
printf('Bucket: %s' . PHP_EOL, $bucket);
56+
}
57+
}
58+
# [END auth_http_implicit]

auth/test/authTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ public function testAuthApiExplicitCommand()
6161
$this->assertContains($this->bucketName, $output);
6262
}
6363

64+
public function testAuthHttpImplicitCommand()
65+
{
66+
$output = $this->runCommand('auth-http-implicit', $this->projectId);
67+
$this->assertContains($this->bucketName, $output);
68+
}
69+
70+
public function testAuthHttpExplicitCommand()
71+
{
72+
$serviceAccountPath = getenv('GOOGLE_APPLICATION_CREDENTIALS');
73+
$output = $this->runCommand('auth-http-explicit', $this->projectId, $serviceAccountPath);
74+
$this->assertContains($this->bucketName, $output);
75+
}
76+
6477
private function runCommand($commandName, $projectId = null, $serviceAccountPath=null)
6578
{
6679
$application = require __DIR__ . '/../auth.php';

0 commit comments

Comments
 (0)