Skip to content

Commit 3876f12

Browse files
committed
Add basic auth samples.
1 parent 3845805 commit 3876f12

File tree

4 files changed

+3721
-0
lines changed

4 files changed

+3721
-0
lines changed

auth/auth.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/**
17+
* Demonstrates how to authenticate to Google Cloud Platform APIs using the
18+
* Google Cloud Client Libraries.
19+
*/
20+
21+
'use strict';
22+
23+
function authCloudImplicit () {
24+
// [START auth_cloud_implicit]
25+
// Imports the Google Cloud client library.
26+
const Storage = require('@google-cloud/storage');
27+
28+
// Instantiates a client. If you don't specify credentials when constructing
29+
// the client, the client library will look for credentials in the
30+
// environment.
31+
const storage = Storage();
32+
33+
// Makes an authenticated API request.
34+
storage
35+
.getBuckets()
36+
.then((results) => {
37+
const buckets = results[0];
38+
39+
console.log('Buckets:');
40+
buckets.forEach((bucket) => {
41+
console.log(bucket.name);
42+
});
43+
})
44+
.catch((err) => {
45+
console.error('ERROR:', err);
46+
});
47+
// [END auth_cloud_implicit]
48+
}
49+
50+
function authCloudExplicit () {
51+
// [START auth_cloud_explicit]
52+
// Imports the Google Cloud client library.
53+
const Storage = require('@google-cloud/storage');
54+
55+
// Instantiates a client. Explicitly use service account credentials by
56+
// specifying the private key file. All clients in google-cloud-node have this
57+
// helper, see https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/latest/guides/authentication
58+
const storage = Storage({
59+
keyFilename: '/path/to/keyfile.json'
60+
});
61+
62+
// Makes an authenticated API request.
63+
storage
64+
.getBuckets()
65+
.then((results) => {
66+
const buckets = results[0];
67+
68+
console.log('Buckets:');
69+
buckets.forEach((bucket) => {
70+
console.log(bucket.name);
71+
});
72+
})
73+
.catch((err) => {
74+
console.error('ERROR:', err);
75+
});
76+
// [END auth_cloud_explicit]
77+
}
78+
79+
const cli = require(`yargs`)
80+
.demand(1)
81+
.command(
82+
`auth-cloud-implicit`,
83+
`Loads credentials implicitly.`,
84+
{},
85+
authCloudImplicit
86+
)
87+
.command(
88+
`auth-cloud-explicit`,
89+
`Loads credentials implicitly.`,
90+
{},
91+
authCloudExplicit
92+
)
93+
.example(`node $0 implicit`, `Loads credentials implicitly.`)
94+
.example(`node $0 explicit`, `Loads credentials explicitly.`)
95+
.wrap(120)
96+
.recommendCommands()
97+
.epilogue(`For more information, see https://cloud.google.com/docs/authentication`)
98+
.help()
99+
.strict();
100+
101+
if (module === require.main) {
102+
cli.parse(process.argv.slice(2));
103+
}

auth/package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "nodejs-docs-samples-auth",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=4.3.2"
13+
},
14+
"scripts": {
15+
"lint": "samples lint",
16+
"pretest": "npm run lint",
17+
"system-test": "ava -T 20s --verbose system-test/*.test.js",
18+
"test": "npm run system-test"
19+
},
20+
"dependencies": {
21+
"@google-cloud/storage": "1.1.1",
22+
"yargs": "8.0.2"
23+
},
24+
"devDependencies": {
25+
"@google-cloud/nodejs-repo-tools": "1.4.15",
26+
"ava": "0.19.1"
27+
},
28+
"cloud-repo-tools": {
29+
"requiresKeyFile": true,
30+
"requiresProjectId": true
31+
}
32+
}

auth/system-test/auth.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const path = require(`path`);
19+
const test = require(`ava`);
20+
const tools = require(`@google-cloud/nodejs-repo-tools`);
21+
22+
const cwd = path.join(__dirname, `..`);
23+
const cmd = `node auth.js`;
24+
25+
test.before(tools.checkCredentials);
26+
27+
test.serial(`should load credentials implicitly`, async (t) => {
28+
const output = await tools.runAsync(`${cmd} auth-cloud-implicit`, cwd);
29+
t.is(output.includes(`Buckets:`), true);
30+
});
31+
32+
test.serial(`should load credentials explicitly`, async (t) => {
33+
const output = await tools.runAsync(`${cmd} auth-cloud-explicit`, cwd);
34+
t.is(!!output, false);
35+
});

0 commit comments

Comments
 (0)