diff --git a/README.md b/README.md index 2fc8b4d184e..47824f2387d 100644 --- a/README.md +++ b/README.md @@ -239,16 +239,17 @@ const compute = google.compute('v1'); async function main () { // This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS // environment variables. - const auth = await google.auth.getClient({ + const auth = new google.auth.GoogleAuth({ // Scopes can be specified either as an array or as a single, space-delimited string. scopes: ['https://www.googleapis.com/auth/compute'] }); + const authClient = await auth.getClient(); // obtain the current project Id - const project = await google.auth.getProjectId(); + const project = await auth.getProjectId(); // Fetch the list of GCE zones within a project. - const res = await compute.zones.list({ project, auth }); + const res = await compute.zones.list({ project, auth: authClient }); console.log(res.data); } @@ -440,18 +441,19 @@ async function main() { // This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS // environment variables. - const client = await google.auth.getClient({ + const auth = new google.auth.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/cloud-platform'] }); + const authClient = await auth.getClient(); - const projectId = await google.auth.getProjectId(); + const projectId = await auth.getProjectId(); const request = { projectId, datasetId: '', // This is a "request-level" option - auth: client + auth: authClient }; const res = await bigquery.datasets.delete(request); diff --git a/package.json b/package.json index 8106cc22adf..71585fb133c 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "license": "Apache-2.0", "dependencies": { - "google-auth-library": "^4.0.0", - "googleapis-common": "^2.0.2" + "google-auth-library": "^5.1.0", + "googleapis-common": "^3.0.0" }, "files": [ "build/src", diff --git a/samples/defaultauth.js b/samples/defaultauth.js index e7b3f7646e5..e7592fdd95c 100644 --- a/samples/defaultauth.js +++ b/samples/defaultauth.js @@ -36,13 +36,13 @@ const compute = google.compute('v1'); // Get the appropriate type of credential client, depending upon the runtime environment. async function main() { // The `getClient` method will choose a service based authentication model - const auth = await google.auth.getClient({ + const auth = new google.auth.GoogleAuth({ // Scopes can be specified either as an array or as a single, space-delimited string. scopes: ['https://www.googleapis.com/auth/compute'], }); // Obtain the current project Id - const project = await google.auth.getProjectId(); + const project = await auth.getProjectId(); // Get the list of available compute zones for your project const res = await compute.zones.list({project, auth}); diff --git a/samples/drive/download.js b/samples/drive/download.js index a2f9b085527..ef13eaae2aa 100644 --- a/samples/drive/download.js +++ b/samples/drive/download.js @@ -26,37 +26,38 @@ const drive = google.drive({ }); async function runSample(fileId) { - return new Promise(async (resolve, reject) => { - const filePath = path.join(os.tmpdir(), uuid.v4()); - console.log(`writing to ${filePath}`); - const dest = fs.createWriteStream(filePath); - let progress = 0; - // For converting document formats, and for downloading template - // documents, see the method drive.files.export(): - // https://developers.google.com/drive/api/v3/manage-downloads - const res = await drive.files.get( - {fileId, alt: 'media'}, - {responseType: 'stream'} - ); - res.data - .on('end', () => { - console.log('Done downloading file.'); - resolve(filePath); - }) - .on('error', err => { - console.error('Error downloading file.'); - reject(err); - }) - .on('data', d => { - progress += d.length; - if (process.stdout.isTTY) { - process.stdout.clearLine(); - process.stdout.cursorTo(0); - process.stdout.write(`Downloaded ${progress} bytes`); - } - }) - .pipe(dest); - }); + // For converting document formats, and for downloading template + // documents, see the method drive.files.export(): + // https://developers.google.com/drive/api/v3/manage-downloads + return drive.files + .get({fileId, alt: 'media'}, {responseType: 'stream'}) + .then(res => { + return new Promise((resolve, reject) => { + const filePath = path.join(os.tmpdir(), uuid.v4()); + console.log(`writing to ${filePath}`); + const dest = fs.createWriteStream(filePath); + let progress = 0; + + res.data + .on('end', () => { + console.log('Done downloading file.'); + resolve(filePath); + }) + .on('error', err => { + console.error('Error downloading file.'); + reject(err); + }) + .on('data', d => { + progress += d.length; + if (process.stdout.isTTY) { + process.stdout.clearLine(); + process.stdout.cursorTo(0); + process.stdout.write(`Downloaded ${progress} bytes`); + } + }) + .pipe(dest); + }); + }); } // if invoked directly (not tests), authenticate and run the samples diff --git a/samples/jwt.js b/samples/jwt.js index 3e841548acf..bbe9a2b12e4 100644 --- a/samples/jwt.js +++ b/samples/jwt.js @@ -27,10 +27,11 @@ const path = require('path'); */ async function runSample() { // Create a new JWT client using the key file downloaded from the Google Developer Console - const client = await google.auth.getClient({ + const auth = new google.auth.GoogleAuth({ keyFile: path.join(__dirname, 'jwt.keys.json'), scopes: 'https://www.googleapis.com/auth/drive.readonly', }); + const client = await auth.getClient(); // Obtain a new drive client, making sure you pass along the auth client const drive = google.drive({ diff --git a/samples/oauth2.js b/samples/oauth2.js index 9f66b67332c..94d412fd409 100644 --- a/samples/oauth2.js +++ b/samples/oauth2.js @@ -65,7 +65,7 @@ async function authenticate(scopes) { res.end('Authentication successful! Please return to the console.'); server.destroy(); const {tokens} = await oauth2Client.getToken(qs.get('code')); - oauth2Client.credentials = tokens; + oauth2Client.credentials = tokens; // eslint-disable-line require-atomic-updates resolve(oauth2Client); } } catch (e) { diff --git a/src/googleapis.ts b/src/googleapis.ts index f37c22d7d7a..b4651af9b4a 100644 --- a/src/googleapis.ts +++ b/src/googleapis.ts @@ -11,24 +11,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {Compute, GoogleAuth, JWT, OAuth2Client} from 'google-auth-library'; +import * as apis from './apis'; + import { + AuthPlus, APIEndpoint, Discovery, Endpoint, GlobalOptions, } from 'googleapis-common'; -import * as apis from './apis'; - -export class AuthPlus extends GoogleAuth { - // tslint:disable-next-line: variable-name - JWT = JWT; - // tslint:disable-next-line: variable-name - Compute = Compute; - // tslint:disable-next-line: variable-name - OAuth2 = OAuth2Client; -} +export {AuthPlus}; export class GoogleApis extends apis.GeneratedAPIs { private _discovery = new Discovery({debug: false, includePrivate: false}); diff --git a/system-test/auth.test.ts b/system-test/auth.test.ts new file mode 100644 index 00000000000..f8e40c2fccc --- /dev/null +++ b/system-test/auth.test.ts @@ -0,0 +1,74 @@ +/** + * Copyright 2019 Google Inc. All Rights Reserved. + * + * 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. + */ + +import {expect} from 'chai'; +import {google} from '../src'; +const compute = google.compute('v1'); + +describe('google.auth', async () => { + describe('google.auth.getClient', async () => { + it('allows client to be configured using historical API', async () => { + const authClient = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + const projectId = await google.auth.getProjectId(); + const result = await compute.instances.aggregatedList({ + auth: authClient, + project: projectId, + }); + const vms = result.data; + expect(vms.kind).to.be.a('string'); + }); + + it('uses projectId from cached client', async () => { + const authClient = await google.auth.getClient({ + projectId: 'foo-project-id', + }); + const projectId = await google.auth.getProjectId(); + expect(projectId).to.equal('foo-project-id'); + }); + + it('uses the last configured client settings', async () => { + let authClient = await google.auth.getClient(); + authClient = await google.auth.getClient({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + const projectId = await google.auth.getProjectId(); + const result = await compute.instances.aggregatedList({ + auth: authClient, + project: projectId, + }); + const vms = result.data; + expect(vms.kind).to.be.a('string'); + }); + }); + + describe('new google.auth.GoogleAuth', async () => { + it('allows client to be configured using historical API', async () => { + const auth = new google.auth.GoogleAuth({ + scopes: ['https://www.googleapis.com/auth/cloud-platform'], + }); + const authClient = await auth.getClient(); + const projectId = await google.auth.getProjectId(); + const result = await compute.instances.aggregatedList({ + auth: authClient, + project: projectId, + }); + const vms = result.data; + expect(vms.kind).to.be.a('string'); + }); + }); +});