@anmiles/google-api-wrapper 7.0.3 → 7.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [7.0.3](../../tags/v7.0.3) - 2023-04-22
8
+ ## [7.0.4](../../tags/v7.0.4) - 2023-04-22
9
9
  ### Changed
10
10
  - Always require refresh token for permanent credentials
11
11
 
@@ -16,7 +16,7 @@ declare function getScopes(): string[];
16
16
  declare function getSecrets(profile: string): Secrets;
17
17
  declare function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Client, options?: AuthOptions): Promise<GoogleApis.Auth.Credentials>;
18
18
  declare function validateCredentials(credentials: GoogleApis.Auth.Credentials): Promise<boolean>;
19
- declare function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client, options?: AuthOptions): Promise<GoogleApis.Auth.Credentials>;
19
+ declare function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client, options?: AuthOptions, prompt?: GoogleApis.Auth.GenerateAuthUrlOpts['prompt']): Promise<GoogleApis.Auth.Credentials>;
20
20
  declare function checkSecrets(profile: string, secretsObject: Secrets, secretsFile: string): true | void;
21
21
  declare function getScopesError(scopesFile: string): string;
22
22
  declare function getSecretsError(profile: string, secretsFile: string): string;
@@ -36,7 +36,8 @@ async function getCredentials(profile, auth, options) {
36
36
  return (0, jsonLib_1.getJSONAsync)(credentialsFile, async () => {
37
37
  // eslint-disable-next-line camelcase
38
38
  const refresh_token = (0, paths_1.ensureFile)(credentialsFile) ? (0, jsonLib_1.readJSON)(credentialsFile).refresh_token : undefined;
39
- const credentials = await secrets_1.default.createCredentials(profile, auth, options);
39
+ // eslint-disable-next-line camelcase
40
+ const credentials = await secrets_1.default.createCredentials(profile, auth, options, refresh_token ? undefined : 'consent');
40
41
  // eslint-disable-next-line camelcase
41
42
  return { refresh_token, ...credentials };
42
43
  }, secrets_1.default.validateCredentials);
@@ -54,13 +55,13 @@ async function validateCredentials(credentials) {
54
55
  }
55
56
  return new Date().getTime() - credentials.expiry_date < tokenExpiration;
56
57
  }
57
- async function createCredentials(profile, auth, options) {
58
+ async function createCredentials(profile, auth, options, prompt) {
58
59
  const scope = (options === null || options === void 0 ? void 0 : options.scopes) || secrets_1.default.getScopes();
59
60
  return new Promise((resolve) => {
60
61
  const authUrl = auth.generateAuthUrl({
61
62
  // eslint-disable-next-line camelcase
62
63
  access_type: 'offline',
63
- prompt: (options === null || options === void 0 ? void 0 : options.temporary) ? undefined : 'consent',
64
+ prompt,
64
65
  scope,
65
66
  });
66
67
  const server = http_1.default.createServer(async (request, response) => {
@@ -1 +1 @@
1
- {"version":3,"file":"secrets.js","sourceRoot":"","sources":["../../src/lib/secrets.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,oEAA2C;AAC3C,gDAAwB;AAGxB,uCAA4D;AAC5D,qCAAuC;AACvC,mCAAwF;AAExF,wDAAgC;AAGhC,kBAAe,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,CAAC;AAEhJ,MAAM,YAAY,GAAM,IAAI,CAAC;AAC7B,MAAM,QAAQ,GAAU,oBAAoB,YAAY,GAAG,CAAC;AAC5D,MAAM,WAAW,GAAO,oBAAoB,YAAY,gBAAgB,CAAC;AACzE,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEhD,SAAS,SAAS;IACjB,MAAM,UAAU,GAAG,IAAA,qBAAa,GAAE,CAAC;IACnC,MAAM,MAAM,GAAO,IAAA,iBAAO,EAAW,UAAU,EAAE,GAAG,EAAE,CAAC,IAAA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AAC,IAAI,CAAC,CAAC;AACd,CAAC;AAED,SAAS,eAAe,CAAC,OAAe,EAAE,WAAmB;IAC5D,OAAO;QACN,QAAQ,WAAW,aAAa;QAChC,2BAA2B;QAC3B,wDAAwD;QACxD,yBAAyB;QACzB,2DAA2D;QAC3D,yDAAyD;QACzD,+DAA+D;QAC/D,sCAAsC;QACtC,0BAA0B;QAC1B,yDAAyD;QACzD,iDAAiD;QACjD,qDAAqD;QACrD,2BAA2B;QAC3B,wBAAwB;QACxB,wCAAwC;QACxC,0GAA0G;QAC1G,mCAAmC;QACnC,oCAAoC;QACpC,uBAAuB,iBAAO,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QACtD,mCAAmC;QACnC,yBAAyB;QACzB,wBAAwB;QACxB,mCAAmC;QACnC,oDAAoD;QACpD,6CAA6C;QAC7C,+DAA+D;QAC/D,mDAAmD;QACnD,yCAAyC;QACzC,wCAAwC,WAAW,EAAE;QACrD,wBAAwB;QACxB,uEAAuE,OAAO,OAAO;QACrF,8BAA8B;KAC9B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anmiles/google-api-wrapper",
3
- "version": "7.0.3",
3
+ "version": "7.0.4",
4
4
  "description": "Provides quick interface for getting google API data",
5
5
  "keywords": [
6
6
  "google",
@@ -239,79 +239,75 @@ describe('src/lib/secrets', () => {
239
239
  expect(getJSONAsyncSpy).not.toBeCalled();
240
240
  });
241
241
 
242
- it('should call createCredentials in fallback', async () => {
242
+ it('should call createCredentials with consent in fallback if no existing credentials', async () => {
243
+ fileExists = false;
244
+
243
245
  await original.getCredentials(profile, auth);
244
246
 
245
247
  expect(secrets.createCredentials).not.toBeCalled();
246
248
 
247
249
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
248
- await fallback();
250
+ const result = await fallback();
249
251
 
250
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined);
252
+ expect(jsonLib.readJSON).not.toBeCalled();
253
+ expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, 'consent');
254
+ expect(result).toEqual(credentialsJSON);
251
255
  });
252
256
 
253
- it('should call createCredentials in fallback if temporariness not set', async () => {
257
+ it('should call createCredentials with consent in fallback if no existing credentials and pass temporariness', async () => {
258
+ fileExists = false;
259
+
254
260
  await original.getCredentials(profile, auth, { temporary : false });
255
261
 
256
262
  expect(secrets.createCredentials).not.toBeCalled();
257
263
 
258
- const fallback = getJSONAsyncSpy.mock.calls[0][1];
259
- await fallback();
260
-
261
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, { temporary : false });
262
- });
263
-
264
- it('should call createCredentials directly if temporariness set', async () => {
265
- await original.getCredentials(profile, auth, { temporary : true });
266
-
267
- expect(secrets.createCredentials).toBeCalledWith(profile, auth, { temporary : true });
268
- });
269
-
270
- it('should return credentials in fallback', async () => {
271
- await original.getCredentials(profile, auth);
272
-
273
264
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
274
265
  const result = await fallback();
275
266
 
267
+ expect(jsonLib.readJSON).not.toBeCalled();
268
+ expect(secrets.createCredentials).toBeCalledWith(profile, auth, { temporary : false }, 'consent');
276
269
  expect(result).toEqual(credentialsJSON);
277
270
  });
278
271
 
279
- it('should return credentials in fallback and copy existing refresh token from existing file if created credentials do not have refresh token', async () => {
272
+ it('should call createCredentials with consent in fallback if existing credentials do not have refresh token', async () => {
280
273
  fileExists = true;
281
- // eslint-disable-next-line camelcase
282
- jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
283
274
 
284
275
  await original.getCredentials(profile, auth);
285
276
 
277
+ expect(secrets.createCredentials).not.toBeCalled();
278
+
286
279
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
287
280
  const result = await fallback();
288
281
 
289
282
  expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
290
- // eslint-disable-next-line camelcase
291
- expect(result).toEqual({ ...credentialsJSON, refresh_token : 'refresh_token' });
283
+ expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, 'consent');
284
+ expect(result).toEqual(credentialsJSON);
292
285
  });
293
286
 
294
- it('should return credentials in fallback and leave refresh_token undefined if there is no existing file', async () => {
295
- fileExists = false;
287
+ it('should call createCredentials without consent in fallback and replace refresh_token if existing credentials have refresh token', async () => {
288
+ fileExists = true;
296
289
  // eslint-disable-next-line camelcase
297
290
  jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
298
291
 
299
292
  await original.getCredentials(profile, auth);
300
293
 
294
+ expect(secrets.createCredentials).not.toBeCalled();
295
+
301
296
  const fallback = getJSONAsyncSpy.mock.calls[0][1];
302
297
  const result = await fallback();
303
298
 
304
- expect(jsonLib.readJSON).not.toBeCalled();
299
+ expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
300
+ expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, undefined);
305
301
  // eslint-disable-next-line camelcase
306
- expect(result).toEqual(credentialsJSON);
302
+ expect(result).toEqual({ ... credentialsJSON, refresh_token : 'refresh_token' });
307
303
  });
308
304
 
309
- it('should return credentials in fallback and leave refresh_token as is if it is set from createCredentials', async () => {
305
+ it('should call createCredentials without consent in fallback and leave refresh token if existing credentials have refresh token', async () => {
310
306
  fileExists = true;
311
307
  // eslint-disable-next-line camelcase
312
- jest.spyOn(secrets, 'createCredentials').mockResolvedValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token_1' });
308
+ jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token' });
313
309
  // eslint-disable-next-line camelcase
314
- jest.spyOn(jsonLib, 'readJSON').mockReturnValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token_2' });
310
+ jest.spyOn(secrets, 'createCredentials').mockResolvedValueOnce({ ...credentialsJSON, refresh_token : 'refresh_token_exists' });
315
311
 
316
312
  await original.getCredentials(profile, auth);
317
313
 
@@ -319,8 +315,9 @@ describe('src/lib/secrets', () => {
319
315
  const result = await fallback();
320
316
 
321
317
  expect(jsonLib.readJSON).toBeCalledWith(credentialsFile);
318
+ expect(secrets.createCredentials).toBeCalledWith(profile, auth, undefined, undefined);
322
319
  // eslint-disable-next-line camelcase
323
- expect(result).toEqual({ ...credentialsJSON, refresh_token : 'refresh_token_1' });
320
+ expect(result).toEqual({ ...credentialsJSON, refresh_token : 'refresh_token_exists' });
324
321
  });
325
322
  });
326
323
 
@@ -365,7 +362,7 @@ describe('src/lib/secrets', () => {
365
362
  expect(auth.generateAuthUrl).toBeCalledWith({
366
363
  // eslint-disable-next-line camelcase
367
364
  access_type : 'offline',
368
- prompt : 'consent',
365
+ prompt : undefined,
369
366
  scope : [
370
367
  'https://www.googleapis.com/auth/calendar.calendars.readonly',
371
368
  'https://www.googleapis.com/auth/calendar.events.readonly',
@@ -373,14 +370,15 @@ describe('src/lib/secrets', () => {
373
370
  });
374
371
  });
375
372
 
376
- it('should generate authUrl and do not require consent if credentials are temporary because refresh token is not required in credentials JSON', async () => {
373
+ it('should generate authUrl and require consent if explicitly asked', async () => {
377
374
  willOpen(tokenUrl, 100);
378
375
 
379
- await original.createCredentials(profile, auth, { temporary : true });
376
+ await original.createCredentials(profile, auth, { temporary : true }, 'consent');
380
377
 
381
378
  expect(auth.generateAuthUrl).toBeCalledWith({
382
379
  // eslint-disable-next-line camelcase
383
380
  access_type : 'offline',
381
+ prompt : 'consent',
384
382
  scope : [
385
383
  'https://www.googleapis.com/auth/calendar.calendars.readonly',
386
384
  'https://www.googleapis.com/auth/calendar.events.readonly',
@@ -396,7 +394,7 @@ describe('src/lib/secrets', () => {
396
394
  expect(auth.generateAuthUrl).toBeCalledWith({
397
395
  // eslint-disable-next-line camelcase
398
396
  access_type : 'offline',
399
- prompt : 'consent',
397
+ prompt : undefined,
400
398
  scope : [ 'scope1', 'scope2' ],
401
399
  });
402
400
  });
@@ -40,7 +40,8 @@ async function getCredentials(profile: string, auth: GoogleApis.Common.OAuth2Cli
40
40
  return getJSONAsync(credentialsFile, async () => {
41
41
  // eslint-disable-next-line camelcase
42
42
  const refresh_token = ensureFile(credentialsFile) ? readJSON<GoogleApis.Auth.Credentials>(credentialsFile).refresh_token : undefined;
43
- const credentials = await secrets.createCredentials(profile, auth, options);
43
+ // eslint-disable-next-line camelcase
44
+ const credentials = await secrets.createCredentials(profile, auth, options, refresh_token ? undefined : 'consent');
44
45
  // eslint-disable-next-line camelcase
45
46
  return { refresh_token, ...credentials };
46
47
  }, secrets.validateCredentials);
@@ -62,14 +63,14 @@ async function validateCredentials(credentials: GoogleApis.Auth.Credentials): Pr
62
63
  return new Date().getTime() - credentials.expiry_date < tokenExpiration;
63
64
  }
64
65
 
65
- async function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client, options?: AuthOptions): Promise<GoogleApis.Auth.Credentials> {
66
+ async function createCredentials(profile: string, auth: GoogleApis.Auth.OAuth2Client, options?: AuthOptions, prompt?: GoogleApis.Auth.GenerateAuthUrlOpts['prompt']): Promise<GoogleApis.Auth.Credentials> {
66
67
  const scope = options?.scopes || secrets.getScopes();
67
68
 
68
69
  return new Promise((resolve) => {
69
70
  const authUrl = auth.generateAuthUrl({
70
71
  // eslint-disable-next-line camelcase
71
72
  access_type : 'offline',
72
- prompt : options?.temporary ? undefined : 'consent',
73
+ prompt,
73
74
  scope,
74
75
  });
75
76