From b0f0b1b947926f8c206adf571669562e487bcd76 Mon Sep 17 00:00:00 2001 From: Megan Slater Date: Thu, 13 Sep 2018 13:47:12 -0700 Subject: [PATCH 1/4] * Remove recursion possibility from the TokenStorage delete method * Check for expiration and delete in authenticate method --- package.json | 1 + src/authentication/authenticator.ts | 4 ++++ src/authentication/token.manager.ts | 23 ----------------------- 3 files changed, 5 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index f8b6cc7..3594982 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@types/jest": "22.1.3", "@types/lodash": "4.14.104", "@types/node": "9.4.6", + "@types/tapable": "^0.2.5", "@types/webpack": "3.8.8", "awesome-typescript-loader": "3.4.1", "babel-core": "6.26.0", diff --git a/src/authentication/authenticator.ts b/src/authentication/authenticator.ts index e74f13b..9911fe4 100644 --- a/src/authentication/authenticator.ts +++ b/src/authentication/authenticator.ts @@ -67,6 +67,10 @@ export class Authenticator { return Promise.resolve(token); } + if (hasTokenExpired) { + this.tokens.delete(provider); + } + return this._openAuthDialog(provider, useMicrosoftTeams); } diff --git a/src/authentication/token.manager.ts b/src/authentication/token.manager.ts index ec8653b..3deab6a 100644 --- a/src/authentication/token.manager.ts +++ b/src/authentication/token.manager.ts @@ -63,29 +63,6 @@ export class TokenStorage extends Storage { } } - /** - * Extends Storage's default get method - * Gets an OAuth Token after checking its expiry - * - * @param {string} provider Unique name of the corresponding OAuth Token. - * @return {object} Returns the token or null if its either expired or doesn't exist. - */ - get(provider: string): IToken { - let token = super.get(provider); - if (token == null) { - return token; - } - - let expired = TokenStorage.hasExpired(token); - if (expired) { - super.delete(provider); - return null; - } - else { - return token; - } - } - /** * Extends Storage's default add method * Adds a new OAuth Token after settings its expiry From c5bfbb3d4b26d61e2d50af9578e779058b1792f7 Mon Sep 17 00:00:00 2001 From: Megan Slater Date: Fri, 14 Sep 2018 16:51:59 -0700 Subject: [PATCH 2/4] Fix date deserialize so it recoginizes ISO dates. --- src/helpers/storage.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index a256c52..52b6dd8 100644 --- a/src/helpers/storage.ts +++ b/src/helpers/storage.ts @@ -5,7 +5,7 @@ import { Observable } from 'rxjs/Observable'; import { Exception } from '../errors/exception'; const NOTIFICATION_DEBOUNCE = 300; -const DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/; +const DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/; export enum StorageType { LocalStorage, @@ -248,7 +248,7 @@ export class Storage { /** * Determine if the value was a Date type and if so return a Date object instead. - * https://blog.mariusschulz.com/2016/04/28/deserializing-json-strings-as-javascript-date-objects + * Regex matches an ISO date string. */ private _reviver(_key: string, value: any) { if (isString(value) && DATE_REGEX.test(value)) { From 0663a497a7e865847bcfc8762660a5df09582bf4 Mon Sep 17 00:00:00 2001 From: Megan Slater Date: Fri, 14 Sep 2018 16:52:08 -0700 Subject: [PATCH 3/4] Create tests for token storage. --- src/authentication/token.spec.ts | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/authentication/token.spec.ts diff --git a/src/authentication/token.spec.ts b/src/authentication/token.spec.ts new file mode 100644 index 0000000..2d9a56a --- /dev/null +++ b/src/authentication/token.spec.ts @@ -0,0 +1,43 @@ +import { TokenStorage, IToken } from './token.manager'; + +describe('The TokenStorage class', () => { + it('can be instantiated', () => { + // Setup + const tokenStorage = new TokenStorage(); + + // Assert + expect(tokenStorage).toBeDefined(); + }); + + it('can get a token that has not expired', () => { + // Setup + const tokenStorage = new TokenStorage(); + const mockToken: IToken = { + provider: 'mockProvider', + expires_at: new Date(Date.now() + 3 * 60 * 60 * 1000) + }; + const mockTokenName = 'mockTokenName'; + + // Act + tokenStorage.set(mockTokenName, mockToken); + + // Assert + expect(tokenStorage.get(mockTokenName)).toEqual(mockToken); + }); + + it('can get a token that has expired', () => { + // Setup + const tokenStorage = new TokenStorage(); + const mockToken: IToken = { + provider: 'mockProvider', + expires_at: new Date(Date.now() - 3 * 60 * 60 * 1000) + }; + const mockTokenName = 'mockTokenName'; + + // Act + tokenStorage.set(mockTokenName, mockToken); + + // Assert + expect(tokenStorage.get(mockTokenName)).toEqual(mockToken); + }); +}); From f23994607d44a785b13c122807e4ab175a5f7ef9 Mon Sep 17 00:00:00 2001 From: Yannick Reekmans Date: Mon, 6 May 2019 09:49:04 -0700 Subject: [PATCH 4/4] Check for existence of token Co-Authored-By: meslater1030 --- src/authentication/authenticator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/authentication/authenticator.ts b/src/authentication/authenticator.ts index 9911fe4..1417021 100644 --- a/src/authentication/authenticator.ts +++ b/src/authentication/authenticator.ts @@ -67,7 +67,7 @@ export class Authenticator { return Promise.resolve(token); } - if (hasTokenExpired) { + if (token && hasTokenExpired) { this.tokens.delete(provider); }