Skip to content

Commit 0d85cea

Browse files
committed
Added HTTP implicit/explicit auth samples
1 parent 9747890 commit 0d85cea

File tree

5 files changed

+171
-1
lines changed

5 files changed

+171
-1
lines changed

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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 the Google Cloud Storage client library, Auth libraries, and Guzzle
27+
// HTTP libraries.
28+
use Google\Cloud\Storage\StorageClient;
29+
use Google\Auth\Credentials\ServiceAccountCredentials;
30+
use Google\Auth\Middleware\AuthTokenMiddleware;
31+
use GuzzleHttp\Client;
32+
use GuzzleHttp\HandlerStack;
33+
34+
function auth_http_explicit($projectId, $serviceAccountPath)
35+
{
36+
# Construct service account credentials using the service account key file
37+
# and Google Auth library's ServiceAccountCredentials class.
38+
$serviceAccountCredentials = new ServiceAccountCredentials(
39+
'https://www.googleapis.com/auth/cloud-platform',
40+
$serviceAccountPath);
41+
$middleware = new AuthTokenMiddleware($serviceAccountCredentials);
42+
$stack = HandlerStack::create();
43+
$stack->push($middleware);
44+
45+
# Create an HTTP Client using Guzzle and pass in the credentials.
46+
$http_client = new Client([
47+
'handler' => $stack,
48+
'base_uri' => 'https://www.googleapis.com/storage/v1/',
49+
'auth' => 'google_auth'
50+
]);
51+
52+
# Make an authenticated API request (listing storage buckets)
53+
$response = $http_client->request('GET', 'b', ['query' =>['project'=>$projectId]]);
54+
$body_content = json_decode($response->getBody());
55+
foreach ($body_content->items as $item) {
56+
$bucket = $item->id;
57+
printf('Bucket: %s' . PHP_EOL, $bucket);
58+
}
59+
}
60+
# [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 the Google Cloud Storage client library, Auth libraries, and Guzzle
27+
// HTTP libraries.
28+
use Google\Cloud\Storage\StorageClient;
29+
use Google\Auth\ApplicationDefaultCredentials;
30+
use Google\Auth\Middleware\AuthTokenMiddleware;
31+
use GuzzleHttp\Client;
32+
use GuzzleHttp\HandlerStack;
33+
34+
function auth_http_implicit($projectId)
35+
{
36+
# Get the credentials and project ID from the environment using Google Auth
37+
# library's ApplicationDefaultCredentials class.
38+
$middleware = ApplicationDefaultCredentials::getMiddleware(
39+
'https://www.googleapis.com/auth/cloud-platform');
40+
$stack = HandlerStack::create();
41+
$stack->push($middleware);
42+
43+
# Create a 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+
$response = $http_client->request('GET', 'b', ['query' =>['project'=>$projectId]]);
52+
$body_content = json_decode($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)