Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions auth/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Demonstrates how to authenticate to Google Cloud Platform APIs using the
* Google Cloud Client Libraries.
*/

'use strict';

function authCloudImplicit () {
// [START auth_cloud_implicit]
// Imports the Google Cloud client library.
const Storage = require('@google-cloud/storage');

// Instantiates a client. If you don't specify credentials when constructing
// the client, the client library will look for credentials in the
// environment.
const storage = Storage();

// Makes an authenticated API request.
storage
.getBuckets()
.then((results) => {
const buckets = results[0];

console.log('Buckets:');
buckets.forEach((bucket) => {
console.log(bucket.name);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
// [END auth_cloud_implicit]
}

function authCloudExplicit () {
// [START auth_cloud_explicit]
// Imports the Google Cloud client library.
const Storage = require('@google-cloud/storage');

// Instantiates a client. Explicitly use service account credentials by
// specifying the private key file. All clients in google-cloud-node have this
// helper, see https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/latest/guides/authentication
const storage = Storage({
keyFilename: '/path/to/keyfile.json'
});

// Makes an authenticated API request.
storage
.getBuckets()
.then((results) => {
const buckets = results[0];

console.log('Buckets:');
buckets.forEach((bucket) => {
console.log(bucket.name);
});
})
.catch((err) => {
console.error('ERROR:', err);
});
// [END auth_cloud_explicit]
}

const cli = require(`yargs`)
.demand(1)
.command(
`auth-cloud-implicit`,
`Loads credentials implicitly.`,
{},
authCloudImplicit
)
.command(
`auth-cloud-explicit`,
`Loads credentials implicitly.`,
{},
authCloudExplicit
)
.example(`node $0 implicit`, `Loads credentials implicitly.`)
.example(`node $0 explicit`, `Loads credentials explicitly.`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/docs/authentication`)
.help()
.strict();

if (module === require.main) {
cli.parse(process.argv.slice(2));
}
32 changes: 32 additions & 0 deletions auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "nodejs-docs-samples-auth",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "ava -T 20s --verbose system-test/*.test.js",
"test": "npm run system-test"
},
"dependencies": {
"@google-cloud/storage": "1.1.1",
"yargs": "8.0.2"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.15",
"ava": "0.19.1"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true
}
}
35 changes: 35 additions & 0 deletions auth/system-test/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const cwd = path.join(__dirname, `..`);
const cmd = `node auth.js`;

test.before(tools.checkCredentials);

test.serial(`should load credentials implicitly`, async (t) => {
const output = await tools.runAsync(`${cmd} auth-cloud-implicit`, cwd);
t.is(output.includes(`Buckets:`), true);
});

test.serial(`should load credentials explicitly`, async (t) => {
const output = await tools.runAsync(`${cmd} auth-cloud-explicit`, cwd);
t.is(!!output, false);
});
Loading