Skip to content

Commit 8df2776

Browse files
ryanmatsbshaffer
authored andcommitted
Auth code samples (GoogleCloudPlatform#419)
1 parent e76ca18 commit 8df2776

17 files changed

+2956
-0
lines changed

auth/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Google Auth PHP Sample Application
2+
3+
## Description
4+
5+
This command-line application shows how to authenticate to Google Cloud APIs
6+
using different methods. This sample uses Storage as an example, but these
7+
methods will work on any Google Cloud API.
8+
9+
## Build and Run
10+
1. **Enable APIs** - [Enable the Storage API](https://console.cloud.google.com/flows/enableapi?apiid=storage-api.googleapis.com)
11+
and create a new project or select an existing project.
12+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
13+
and select "Service Account Key". Create a new service account, use the JSON key type, and
14+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
15+
to the path of the JSON key that was downloaded.
16+
3. **Clone the repo** and cd into this directory
17+
```
18+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
19+
$ cd php-docs-samples/auth
20+
```
21+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
22+
Run `php composer.phar install --no-dev` (if composer is installed locally) or `composer install --no-dev`
23+
(if composer is installed globally).
24+
5. Run `php auth.php`. The following commands are available and work on command line:
25+
```
26+
auth-cloud-implicit Authenticate to a cloud client library using a service account implicitly.
27+
auth-cloud-explicit Authenticate to a cloud client library using a service account explicitly.
28+
auth-api-implicit Authenticate to a cloud API using a service account implicitly.
29+
auth-api-explicit Authenticate to a cloud API using a service account explicitly.
30+
```
31+
6. The following commands are available but will throw a ServiceException when
32+
run from command-line. The Compute Engine method only works on Compute Engine,
33+
App Engine Flexible, Cloud Functions, and Container Engine. The App Engine
34+
method only works on App Engine Standard.
35+
```
36+
auth-cloud-explicit-compute-engine Authenticate to a cloud client library using Compute Engine credentials explicitly.
37+
auth-cloud-explicit-app-engine Authenticate to a cloud client library using App Engine Standard credentials explicitly.
38+
auth-api-explicit-compute-engine Authenticate to a cloud API using Compute Engine credentials explicitly.
39+
auth-api-explicit-app-engine Authenticate to a cloud API using App Engine Standard credentials explicitly.
40+
```
41+
7. You can test the samples that use Compute Engine / App Engine credentials by
42+
deploying to either App Engine Flexible (which allows usage of Compute Engine
43+
credentials since App Engine Flexible apps run on Compute Engine instances) or
44+
App Engine Standard. Run either `gcloud app deploy app-standard.yaml` or
45+
`gcloud app deploy app-flex.yaml`.
46+
47+
8. Run `php auth.php COMMAND --help` to print information about the usage of each command.
48+
49+
## Contributing changes
50+
51+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
52+
53+
## Licensing
54+
55+
* See [LICENSE](../../LICENSE)

auth/app-flex.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runtime: php
2+
env: flex
3+
4+
runtime_config:
5+
document_root: .

auth/app-standard.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: php55
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: /.*
7+
script: index.php

auth/auth.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
namespace Google\Cloud\Samples\Auth;
19+
20+
use Symfony\Component\Console\Application;
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
24+
# Includes the autoloader for libraries installed with composer
25+
require __DIR__ . '/vendor/autoload.php';
26+
27+
$application = new Application('Auth');
28+
29+
// Create auth-cloud-implicit Command.
30+
$application->add((new Command('auth-cloud-implicit'))
31+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
32+
->setDescription('Authenticate to a cloud client library using a service account implicitly.')
33+
->setHelp(<<
34+
The %command.name% command authenticates to a cloud client library
35+
using a service account implicitly.
36+
37+
php %command.full_name%
38+
39+
EOF
40+
)
41+
->setCode(function ($input, $output) {
42+
auth_cloud_implicit($input->getArgument('projectId'));
43+
})
44+
);
45+
46+
// Create auth-cloud-explicit Command.
47+
$application->add((new Command('auth-cloud-explicit'))
48+
->addArgument('serviceAccountPath', InputArgument::REQUIRED, 'Path to your service account.')
49+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
50+
->setDescription('Authenticate to a cloud client library using a service account explicitly.')
51+
->setHelp(<<
52+
The %command.name% command authenticates to a cloud client library
53+
using a service account explicitly.
54+
55+
php %command.full_name%
56+
57+
EOF
58+
)
59+
->setCode(function ($input, $output) {
60+
auth_cloud_explicit($input->getArgument('projectId'), $input->getArgument('serviceAccountPath'));
61+
})
62+
);
63+
64+
// Create auth-cloud-explicit-compute-engine Command.
65+
$application->add((new Command('auth-cloud-explicit-compute-engine'))
66+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
67+
->setDescription('Authenticate to a cloud client library using Compute Engine credentials explicitly.')
68+
->setHelp(<<
69+
The %command.name% command authenticates to a cloud client library
70+
using Compute Engine credentials explicitly.
71+
72+
php %command.full_name%
73+
74+
EOF
75+
)
76+
->setCode(function ($input, $output) {
77+
auth_cloud_explicit_compute_engine($input->getArgument('projectId'));
78+
})
79+
);
80+
81+
// Create auth-cloud-explicit-app-engine Command.
82+
$application->add((new Command('auth-cloud-explicit-app-engine'))
83+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
84+
->setDescription('Authenticate to a cloud client library using App Engine Standard credentials explicitly.')
85+
->setHelp(<<
86+
The %command.name% command authenticates to a cloud client library
87+
using App Engine Standard credentials explicitly.
88+
89+
php %command.full_name%
90+
91+
EOF
92+
)
93+
->setCode(function ($input, $output) {
94+
auth_cloud_explicit_app_engine($input->getArgument('projectId'));
95+
})
96+
);
97+
98+
// Create auth-api-implicit Command.
99+
$application->add((new Command('auth-api-implicit'))
100+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
101+
->setDescription('Authenticate to a cloud API using a service account implicitly.')
102+
->setHelp(<<
103+
The %command.name% command authenticates to a cloud API using a
104+
service account implicitly.
105+
106+
php %command.full_name%
107+
108+
EOF
109+
)
110+
->setCode(function ($input, $output) {
111+
auth_api_implicit($input->getArgument('projectId'));
112+
})
113+
);
114+
115+
// Create auth-api-explicit Command.
116+
$application->add((new Command('auth-api-explicit'))
117+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
118+
->addArgument('serviceAccountPath', InputArgument::REQUIRED, 'Path to your service account.')
119+
->setDescription('Authenticate to a cloud API using a service account explicitly.')
120+
->setHelp(<<
121+
The %command.name% command authenticates to a cloud API using a
122+
service account implicitly.
123+
124+
php %command.full_name%
125+
126+
EOF
127+
)
128+
->setCode(function ($input, $output) {
129+
$projectId = $input->getArgument('projectId');
130+
$serviceAccountPath = $input->getArgument('serviceAccountPath');
131+
auth_api_explicit($projectId, $serviceAccountPath);
132+
})
133+
);
134+
135+
// Create auth-api-explicit-compute-engine Command.
136+
$application->add((new Command('auth-api-explicit-compute-engine'))
137+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
138+
->setDescription('Authenticate to a cloud API using Compute Engine credentials explicitly.')
139+
->setHelp(<<
140+
The %command.name% command authenticates to a cloud API using
141+
Compute Engine credentials explicitly.
142+
143+
php %command.full_name%
144+
145+
EOF
146+
)
147+
->setCode(function ($input, $output) {
148+
$projectId = $input->getArgument('projectId');
149+
auth_api_explicit_compute_engine($projectId);
150+
})
151+
);
152+
153+
// Create auth-api-explicit-app-engine Command.
154+
$application->add((new Command('auth-api-explicit-app-engine'))
155+
->addArgument('projectId', InputArgument::REQUIRED, 'Your project ID')
156+
->setDescription('Authenticate to a cloud API using App Engine Standard credentials explicitly.')
157+
->setHelp(<<
158+
The %command.name% command authenticates to a cloud API using
159+
Compute Engine credentials explicitly.
160+
161+
php %command.full_name%
162+
163+
EOF
164+
)
165+
->setCode(function ($input, $output) {
166+
$projectId = $input->getArgument('projectId');
167+
auth_api_explicit_compute_engine($projectId);
168+
})
169+
);
170+
171+
if (getenv('PHPUNIT_TESTS') === '1') {
172+
return $application;
173+
}
174+
175+
$application->run();

auth/composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"require": {
3+
"google/apiclient": "^2.1",
4+
"google/cloud-storage": "^1.0",
5+
"symfony/console": " ^3.0",
6+
"google/auth":"^1.0"
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"Google\\Cloud\\Samples\\Auth\\": "src/"
11+
},
12+
"files": [
13+
"src/auth_cloud_implicit.php",
14+
"src/auth_cloud_explicit.php",
15+
"src/auth_cloud_explicit_compute_engine.php",
16+
"src/auth_cloud_explicit_app_engine.php",
17+
"src/auth_api_implicit.php",
18+
"src/auth_api_explicit.php",
19+
"src/auth_api_explicit_compute_engine.php",
20+
"src/auth_api_explicit_app_engine.php"
21+
]
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "~4"
25+
}
26+
}

0 commit comments

Comments
 (0)