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 efb93ef..5a8502f 100644 --- a/src/authentication/authenticator.ts +++ b/src/authentication/authenticator.ts @@ -67,6 +67,10 @@ export class Authenticator { return Promise.resolve(token); } + if (token && 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 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); + }); +}); diff --git a/src/helpers/storage.ts b/src/helpers/storage.ts index a677991..9f46c2c 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)) {