/
test.auth.ts
283 lines (253 loc) · 10.2 KB
/
test.auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2013-2016, 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.
import * as assert from 'assert';
import {OAuth2Client} from 'google-auth-library';
import {APIEndpoint} from 'googleapis-common';
import * as nock from 'nock';
import {GoogleApis} from '../src';
import {Utils} from './utils';
const assertRejects = require('assert-rejects');
const googleapis = new GoogleApis();
describe('JWT client', () => {
it('should expose the default auth module', () => {
assert(googleapis.auth.getApplicationDefault);
});
it('should create a jwt through googleapis', () => {
const jwt = new googleapis.auth.JWT(
'someone@somewhere.com', 'file1', 'key1', 'scope1', 'subject1');
assert.strictEqual(jwt.email, 'someone@somewhere.com');
assert.strictEqual(jwt.keyFile, 'file1');
assert.strictEqual(jwt.key, 'key1');
assert.strictEqual(jwt.scopes, 'scope1');
assert.strictEqual(jwt.subject, 'subject1');
});
});
describe('Compute client', () => {
it('should create a computeclient', () => {
const compute = new googleapis.auth.Compute();
});
});
async function testNoTokens(urlshortener: APIEndpoint, client: OAuth2Client) {
await assertRejects(
urlshortener.url.get({shortUrl: '123', auth: client}),
/No access, refresh token or API key is set./);
}
async function testNoBearer(
urlshortener: APIEndpoint, oauth2client: OAuth2Client) {
await urlshortener.url.list({auth: oauth2client});
assert.strictEqual(oauth2client.credentials.token_type, 'Bearer');
}
async function testExpired(
drive: APIEndpoint, oauth2client: OAuth2Client, now: number) {
nock(Utils.baseUrl).get('/drive/v2/files/wat').reply(200);
await drive.files.get({fileId: 'wat', auth: oauth2client});
const expiryDate = oauth2client.credentials.expiry_date;
assert.notEqual(expiryDate, undefined);
if (!expiryDate) return;
assert(expiryDate > now);
assert(expiryDate < now + 5000);
assert.strictEqual(oauth2client.credentials.refresh_token, 'abc');
assert.strictEqual(oauth2client.credentials.access_token, 'abc123');
}
async function testNoAccessToken(
drive: APIEndpoint, oauth2client: OAuth2Client, now: number) {
nock(Utils.baseUrl).get('/drive/v2/files/wat').reply(200);
await drive.files.get({fileId: 'wat', auth: oauth2client});
const expiryDate = oauth2client.credentials.expiry_date;
assert.notEqual(expiryDate, undefined);
assert(expiryDate! > now);
assert(expiryDate! < now + 4000);
assert.strictEqual(oauth2client.credentials.refresh_token, 'abc');
assert.strictEqual(oauth2client.credentials.access_token, 'abc123');
}
describe('OAuth2 client', () => {
let localDrive: APIEndpoint, remoteDrive: APIEndpoint;
let localUrlshortener: APIEndpoint, remoteUrlshortener: APIEndpoint;
before(async () => {
nock.cleanAll();
const google = new GoogleApis();
nock.enableNetConnect();
[remoteDrive, remoteUrlshortener] = await Promise.all([
Utils.loadApi(google, 'drive', 'v2'),
Utils.loadApi(google, 'urlshortener', 'v1')
]);
nock.disableNetConnect();
});
beforeEach(() => {
nock.cleanAll();
nock.disableNetConnect();
const google = new GoogleApis();
localDrive = google.drive('v2');
localUrlshortener = google.urlshortener('v1');
});
const CLIENT_ID = 'CLIENT_ID';
const CLIENT_SECRET = 'CLIENT_SECRET';
const REDIRECT_URI = 'REDIRECT';
it('should return err if no access or refresh token is set', async () => {
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
await testNoTokens(localUrlshortener, oauth2client);
await testNoTokens(remoteUrlshortener, oauth2client);
});
it('should not error if only refresh token is set', () => {
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = {refresh_token: 'refresh_token'};
assert.doesNotThrow(() => {
const options = {auth: oauth2client, shortUrl: '...'};
localUrlshortener.url.get(options, Utils.noop);
remoteUrlshortener.url.get(options, Utils.noop);
});
});
it('should set access token type to Bearer if none is set', async () => {
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = {access_token: 'foo', refresh_token: ''};
const scope = nock(Utils.baseUrl)
.get('/urlshortener/v1/url/history')
.times(2)
.reply(200);
await testNoBearer(localUrlshortener, oauth2client);
await testNoBearer(remoteUrlshortener, oauth2client);
});
it('should refresh if access token is expired', async () => {
const scope = nock('https://oauth2.googleapis.com')
.post('/token')
.times(2)
.reply(200, {access_token: 'abc123', expires_in: 1});
let oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
let now = new Date().getTime();
let twoSecondsAgo = now - 2000;
oauth2client.credentials = {
refresh_token: 'abc',
expiry_date: twoSecondsAgo
};
await testExpired(localDrive, oauth2client, now);
oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
now = new Date().getTime();
twoSecondsAgo = now - 2000;
oauth2client.credentials = {
refresh_token: 'abc',
expiry_date: twoSecondsAgo
};
await testExpired(remoteDrive, oauth2client, now);
});
it('should make request if access token not expired', async () => {
const scope = nock('https://www.googleapis.com')
.post('/oauth2/v4/token')
.times(2)
.reply(200, {access_token: 'abc123', expires_in: 10000});
let oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
let now = (new Date()).getTime();
let tenMinutesFromNow = now + 1000 * 60 * 10;
oauth2client.credentials = {
access_token: 'abc123',
refresh_token: 'abc',
expiry_date: tenMinutesFromNow
};
nock(Utils.baseUrl).get('/drive/v2/files/wat').reply(200);
await localDrive.files.get({fileId: 'wat', auth: oauth2client});
assert.strictEqual(
JSON.stringify(oauth2client.credentials), JSON.stringify({
access_token: 'abc123',
refresh_token: 'abc',
expiry_date: tenMinutesFromNow,
token_type: 'Bearer'
}));
assert.throws(() => {
scope.done();
}, 'AssertionError');
oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
now = (new Date()).getTime();
tenMinutesFromNow = now + 1000 * 60 * 10;
oauth2client.credentials = {
access_token: 'abc123',
refresh_token: 'abc',
expiry_date: tenMinutesFromNow
};
nock(Utils.baseUrl).get('/drive/v2/files/wat').reply(200);
await remoteDrive.files.get({fileId: 'wat', auth: oauth2client});
assert.strictEqual(
JSON.stringify(oauth2client.credentials), JSON.stringify({
access_token: 'abc123',
refresh_token: 'abc',
expiry_date: tenMinutesFromNow,
token_type: 'Bearer'
}));
assert.throws(() => {
scope.done();
}, 'AssertionError');
});
it('should refresh if have refresh token but no access token', async () => {
const scope = nock('https://oauth2.googleapis.com')
.post('/token')
.times(2)
.reply(200, {access_token: 'abc123', expires_in: 1});
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
let now = (new Date()).getTime();
oauth2client.credentials = {refresh_token: 'abc'};
await testNoAccessToken(localDrive, oauth2client, now);
now = (new Date()).getTime();
oauth2client.credentials = {refresh_token: 'abc'};
await testNoAccessToken(remoteDrive, oauth2client, now);
});
describe('revokeCredentials()', () => {
it('should revoke credentials if access token present', async () => {
const scope = nock('https://oauth2.googleapis.com')
.post('/revoke?token=abc')
.reply(200, {success: true});
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = {access_token: 'abc', refresh_token: 'abc'};
const res = await oauth2client.revokeCredentials();
scope.done();
assert.strictEqual(res.data.success, true);
assert.deepStrictEqual(oauth2client.credentials, {});
});
it('should clear credentials and return error if no access token to revoke',
async () => {
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
oauth2client.credentials = {refresh_token: 'abc'};
await assertRejects(
oauth2client.revokeCredentials(),
/Error: No access token to revoke./);
assert.deepStrictEqual(oauth2client.credentials, {});
});
});
describe('getToken()', () => {
it('should return expiry_date', async () => {
const now = (new Date()).getTime();
const scope =
nock('https://oauth2.googleapis.com').post('/token').reply(200, {
access_token: 'abc',
refresh_token: '123',
expires_in: 10
});
const oauth2client =
new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
const res = await oauth2client.getToken('code here');
assert(res.tokens.expiry_date! >= now + (10 * 1000));
assert(res.tokens.expiry_date! <= now + (15 * 1000));
});
});
after(() => {
nock.cleanAll();
nock.enableNetConnect();
});
});