|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import React from 'react'; |
| 6 | +import { Meta } from '@storybook/react'; |
| 7 | +import { withLocalization, withLocation } from 'fxa-react/lib/storybooks'; |
| 8 | +import { action } from '@storybook/addon-actions'; |
| 9 | +import { AppContext } from '../../../models'; |
| 10 | +import { mockAppContext } from '../../../models/mocks'; |
| 11 | +import { MfaGuard } from './index'; |
| 12 | +import { JwtTokenCache } from '../../../lib/cache'; |
| 13 | + |
| 14 | +const scope: 'test' = 'test'; |
| 15 | +const session = 'session-xyz'; |
| 16 | + |
| 17 | +function initLocalAccount(sessionToken: string) { |
| 18 | + // Match Storage fullKey logic: '__fxa_storage.' prefix |
| 19 | + const NS = '__fxa_storage'; |
| 20 | + const uid = 'abc123'; |
| 21 | + const accounts = { |
| 22 | + [uid]: { |
| 23 | + uid, |
| 24 | + sessionToken, |
| 25 | + |
| 26 | + verified: true, |
| 27 | + lastLogin: Date.now(), |
| 28 | + }, |
| 29 | + }; |
| 30 | + window.localStorage.setItem(`${NS}.accounts`, JSON.stringify(accounts)); |
| 31 | + window.localStorage.setItem(`${NS}.currentAccountUid`, JSON.stringify(uid)); |
| 32 | +} |
| 33 | + |
| 34 | +const authClient = { |
| 35 | + mfaRequestOtp: async (st: string, sc: string) => { |
| 36 | + action('mfaRequestOtp')({ sessionToken: st, scope: sc }); |
| 37 | + return undefined; |
| 38 | + }, |
| 39 | + mfaOtpVerify: async (st: string, code: string, sc: string) => { |
| 40 | + action('mfaOtpVerify')({ sessionToken: st, code, scope: sc }); |
| 41 | + return { accessToken: 'storybook-jwt' } as any; |
| 42 | + }, |
| 43 | +} as any; |
| 44 | + |
| 45 | +export default { |
| 46 | + title: 'Components/Settings/MfaGuard', |
| 47 | + component: MfaGuard, |
| 48 | + decorators: [withLocalization, withLocation('/settings')], |
| 49 | +} as Meta; |
| 50 | + |
| 51 | +export const JwtMissingShowsModal = () => { |
| 52 | + initLocalAccount(session); |
| 53 | + // Ensure no token present so guard triggers OTP flow |
| 54 | + try { |
| 55 | + if (JwtTokenCache.hasToken(session, scope)) { |
| 56 | + JwtTokenCache.removeToken(session, scope); |
| 57 | + } |
| 58 | + } catch {} |
| 59 | + |
| 60 | + return ( |
| 61 | + <AppContext.Provider value={mockAppContext({ authClient } as any)}> |
| 62 | + <MfaGuard requiredScope={scope}> |
| 63 | + <div>Secured content</div> |
| 64 | + </MfaGuard> |
| 65 | + </AppContext.Provider> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +export const JwtPresentRendersChildren = () => { |
| 70 | + initLocalAccount(session); |
| 71 | + JwtTokenCache.setToken(session, scope, 'jwt-present'); |
| 72 | + |
| 73 | + return ( |
| 74 | + <AppContext.Provider value={mockAppContext({ authClient } as any)}> |
| 75 | + <MfaGuard requiredScope={scope}> |
| 76 | + <div>Secured content</div> |
| 77 | + </MfaGuard> |
| 78 | + </AppContext.Provider> |
| 79 | + ); |
| 80 | +}; |
0 commit comments