-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): prevent infinite loop if handshake token is not found in u…
- Loading branch information
1 parent
4451644
commit bc2bf9c
Showing
5 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type BrowserStorage = Pick< | ||
Storage, | ||
'setItem' | 'getItem' | 'removeItem' | ||
>; | ||
|
||
export function getBrowserStorage(): BrowserStorage { | ||
try { | ||
return window.localStorage; | ||
} catch { | ||
return { | ||
getItem: () => null, | ||
setItem: () => {}, | ||
removeItem: () => {}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {BrowserStorage} from './browser-storage'; | ||
import {buildSamlState, SamlState} from './saml-state'; | ||
|
||
describe('buildSamlState', () => { | ||
let storage: BrowserStorage; | ||
let samlState: SamlState; | ||
|
||
function buildMockStorage(): BrowserStorage { | ||
return { | ||
getItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
setItem: jest.fn(), | ||
}; | ||
} | ||
|
||
function initSamlState() { | ||
samlState = buildSamlState({storage}); | ||
} | ||
|
||
beforeEach(() => { | ||
storage = buildMockStorage(); | ||
initSamlState(); | ||
}); | ||
|
||
it('#setLoginPending sets the "samlLoginPending" to true', () => { | ||
samlState.setLoginPending(); | ||
|
||
expect(storage.setItem).toBeCalledWith('samlLoginPending', 'true'); | ||
}); | ||
|
||
describe('#isLoginPending', () => { | ||
it('when #storage.getItem returns "true"', () => { | ||
(storage.getItem as jest.Mock).mockReturnValue('true'); | ||
expect(samlState.isLoginPending).toBe(true); | ||
}); | ||
|
||
it('when #storage.getItem returns null', () => { | ||
(storage.getItem as jest.Mock).mockReturnValue(null); | ||
expect(samlState.isLoginPending).toBe(false); | ||
}); | ||
}); | ||
|
||
it('#removeLoginPending', () => { | ||
samlState.removeLoginPending(); | ||
expect(storage.removeItem).toBeCalledWith('samlLoginPending'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {BrowserStorage, getBrowserStorage} from './browser-storage'; | ||
|
||
interface SamlStateOptions { | ||
storage?: BrowserStorage; | ||
} | ||
|
||
export interface SamlState { | ||
isLoginPending: boolean; | ||
removeLoginPending(): void; | ||
setLoginPending(): void; | ||
} | ||
|
||
export function buildSamlState(config: SamlStateOptions = {}): SamlState { | ||
const loginPendingFlag = 'samlLoginPending'; | ||
const storage = config.storage || getBrowserStorage(); | ||
|
||
return { | ||
get isLoginPending() { | ||
return storage.getItem(loginPendingFlag) === 'true'; | ||
}, | ||
|
||
removeLoginPending() { | ||
storage.removeItem(loginPendingFlag); | ||
}, | ||
|
||
setLoginPending() { | ||
storage.setItem(loginPendingFlag, 'true'); | ||
}, | ||
}; | ||
} |