Skip to content

Commit 168ad6b

Browse files
author
Benjamin E. Coe
authored
refactor(auth)!: pulling in updated idempotent google-auth-library (#1769)
1 parent 86074f3 commit 168ad6b

File tree

8 files changed

+125
-54
lines changed

8 files changed

+125
-54
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,17 @@ const compute = google.compute('v1');
239239
async function main () {
240240
// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
241241
// environment variables.
242-
const auth = await google.auth.getClient({
242+
const auth = new google.auth.GoogleAuth({
243243
// Scopes can be specified either as an array or as a single, space-delimited string.
244244
scopes: ['https://www.googleapis.com/auth/compute']
245245
});
246+
const authClient = await auth.getClient();
246247

247248
// obtain the current project Id
248-
const project = await google.auth.getProjectId();
249+
const project = await auth.getProjectId();
249250

250251
// Fetch the list of GCE zones within a project.
251-
const res = await compute.zones.list({ project, auth });
252+
const res = await compute.zones.list({ project, auth: authClient });
252253
console.log(res.data);
253254
}
254255

@@ -440,18 +441,19 @@ async function main() {
440441

441442
// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
442443
// environment variables.
443-
const client = await google.auth.getClient({
444+
const auth = new google.auth.GoogleAuth({
444445
scopes: ['https://www.googleapis.com/auth/cloud-platform']
445446
});
447+
const authClient = await auth.getClient();
446448

447-
const projectId = await google.auth.getProjectId();
449+
const projectId = await auth.getProjectId();
448450

449451
const request = {
450452
projectId,
451453
datasetId: '<YOUR_DATASET_ID>',
452454

453455
// This is a "request-level" option
454-
auth: client
456+
auth: authClient
455457
};
456458

457459
const res = await bigquery.datasets.delete(request);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"license": "Apache-2.0",
33
"dependencies": {
4-
"google-auth-library": "^4.0.0",
5-
"googleapis-common": "^2.0.2"
4+
"google-auth-library": "^5.1.0",
5+
"googleapis-common": "^3.0.0"
66
},
77
"files": [
88
"build/src",

samples/defaultauth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const compute = google.compute('v1');
3636
// Get the appropriate type of credential client, depending upon the runtime environment.
3737
async function main() {
3838
// The `getClient` method will choose a service based authentication model
39-
const auth = await google.auth.getClient({
39+
const auth = new google.auth.GoogleAuth({
4040
// Scopes can be specified either as an array or as a single, space-delimited string.
4141
scopes: ['https://www.googleapis.com/auth/compute'],
4242
});
4343

4444
// Obtain the current project Id
45-
const project = await google.auth.getProjectId();
45+
const project = await auth.getProjectId();
4646

4747
// Get the list of available compute zones for your project
4848
const res = await compute.zones.list({project, auth});

samples/drive/download.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,38 @@ const drive = google.drive({
2626
});
2727

2828
async function runSample(fileId) {
29-
return new Promise(async (resolve, reject) => {
30-
const filePath = path.join(os.tmpdir(), uuid.v4());
31-
console.log(`writing to ${filePath}`);
32-
const dest = fs.createWriteStream(filePath);
33-
let progress = 0;
34-
// For converting document formats, and for downloading template
35-
// documents, see the method drive.files.export():
36-
// https://developers.google.com/drive/api/v3/manage-downloads
37-
const res = await drive.files.get(
38-
{fileId, alt: 'media'},
39-
{responseType: 'stream'}
40-
);
41-
res.data
42-
.on('end', () => {
43-
console.log('Done downloading file.');
44-
resolve(filePath);
45-
})
46-
.on('error', err => {
47-
console.error('Error downloading file.');
48-
reject(err);
49-
})
50-
.on('data', d => {
51-
progress += d.length;
52-
if (process.stdout.isTTY) {
53-
process.stdout.clearLine();
54-
process.stdout.cursorTo(0);
55-
process.stdout.write(`Downloaded ${progress} bytes`);
56-
}
57-
})
58-
.pipe(dest);
59-
});
29+
// For converting document formats, and for downloading template
30+
// documents, see the method drive.files.export():
31+
// https://developers.google.com/drive/api/v3/manage-downloads
32+
return drive.files
33+
.get({fileId, alt: 'media'}, {responseType: 'stream'})
34+
.then(res => {
35+
return new Promise((resolve, reject) => {
36+
const filePath = path.join(os.tmpdir(), uuid.v4());
37+
console.log(`writing to ${filePath}`);
38+
const dest = fs.createWriteStream(filePath);
39+
let progress = 0;
40+
41+
res.data
42+
.on('end', () => {
43+
console.log('Done downloading file.');
44+
resolve(filePath);
45+
})
46+
.on('error', err => {
47+
console.error('Error downloading file.');
48+
reject(err);
49+
})
50+
.on('data', d => {
51+
progress += d.length;
52+
if (process.stdout.isTTY) {
53+
process.stdout.clearLine();
54+
process.stdout.cursorTo(0);
55+
process.stdout.write(`Downloaded ${progress} bytes`);
56+
}
57+
})
58+
.pipe(dest);
59+
});
60+
});
6061
}
6162

6263
// if invoked directly (not tests), authenticate and run the samples

samples/jwt.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ const path = require('path');
2727
*/
2828
async function runSample() {
2929
// Create a new JWT client using the key file downloaded from the Google Developer Console
30-
const client = await google.auth.getClient({
30+
const auth = new google.auth.GoogleAuth({
3131
keyFile: path.join(__dirname, 'jwt.keys.json'),
3232
scopes: 'https://www.googleapis.com/auth/drive.readonly',
3333
});
34+
const client = await auth.getClient();
3435

3536
// Obtain a new drive client, making sure you pass along the auth client
3637
const drive = google.drive({

samples/oauth2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function authenticate(scopes) {
6565
res.end('Authentication successful! Please return to the console.');
6666
server.destroy();
6767
const {tokens} = await oauth2Client.getToken(qs.get('code'));
68-
oauth2Client.credentials = tokens;
68+
oauth2Client.credentials = tokens; // eslint-disable-line require-atomic-updates
6969
resolve(oauth2Client);
7070
}
7171
} catch (e) {

src/googleapis.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,17 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import {Compute, GoogleAuth, JWT, OAuth2Client} from 'google-auth-library';
14+
import * as apis from './apis';
15+
1516
import {
17+
AuthPlus,
1618
APIEndpoint,
1719
Discovery,
1820
Endpoint,
1921
GlobalOptions,
2022
} from 'googleapis-common';
2123

22-
import * as apis from './apis';
23-
24-
export class AuthPlus extends GoogleAuth {
25-
// tslint:disable-next-line: variable-name
26-
JWT = JWT;
27-
// tslint:disable-next-line: variable-name
28-
Compute = Compute;
29-
// tslint:disable-next-line: variable-name
30-
OAuth2 = OAuth2Client;
31-
}
24+
export {AuthPlus};
3225

3326
export class GoogleApis extends apis.GeneratedAPIs {
3427
private _discovery = new Discovery({debug: false, includePrivate: false});

system-test/auth.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2019 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {expect} from 'chai';
18+
import {google} from '../src';
19+
const compute = google.compute('v1');
20+
21+
describe('google.auth', async () => {
22+
describe('google.auth.getClient', async () => {
23+
it('allows client to be configured using historical API', async () => {
24+
const authClient = await google.auth.getClient({
25+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
26+
});
27+
const projectId = await google.auth.getProjectId();
28+
const result = await compute.instances.aggregatedList({
29+
auth: authClient,
30+
project: projectId,
31+
});
32+
const vms = result.data;
33+
expect(vms.kind).to.be.a('string');
34+
});
35+
36+
it('uses projectId from cached client', async () => {
37+
const authClient = await google.auth.getClient({
38+
projectId: 'foo-project-id',
39+
});
40+
const projectId = await google.auth.getProjectId();
41+
expect(projectId).to.equal('foo-project-id');
42+
});
43+
44+
it('uses the last configured client settings', async () => {
45+
let authClient = await google.auth.getClient();
46+
authClient = await google.auth.getClient({
47+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
48+
});
49+
const projectId = await google.auth.getProjectId();
50+
const result = await compute.instances.aggregatedList({
51+
auth: authClient,
52+
project: projectId,
53+
});
54+
const vms = result.data;
55+
expect(vms.kind).to.be.a('string');
56+
});
57+
});
58+
59+
describe('new google.auth.GoogleAuth', async () => {
60+
it('allows client to be configured using historical API', async () => {
61+
const auth = new google.auth.GoogleAuth({
62+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
63+
});
64+
const authClient = await auth.getClient();
65+
const projectId = await google.auth.getProjectId();
66+
const result = await compute.instances.aggregatedList({
67+
auth: authClient,
68+
project: projectId,
69+
});
70+
const vms = result.data;
71+
expect(vms.kind).to.be.a('string');
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)