diff options
author | vnugent <public@vaughnnugent.com> | 2024-01-30 15:23:06 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-01-30 15:23:06 -0500 |
commit | 5abc489b9954111d66d1385aa62a3ea962fa0a55 (patch) | |
tree | b1715e5c5e6316f33e3e33fb55397d93200ab518 /front-end/src/store/mfaSettingsPlugin.ts | |
parent | e4dc63ded324c6e9678603296953bb1f7dea7569 (diff) |
merge upstream. Add dynamic client-side support for optional oauth2 and social login methods
Diffstat (limited to 'front-end/src/store/mfaSettingsPlugin.ts')
-rw-r--r-- | front-end/src/store/mfaSettingsPlugin.ts | 109 |
1 files changed, 73 insertions, 36 deletions
diff --git a/front-end/src/store/mfaSettingsPlugin.ts b/front-end/src/store/mfaSettingsPlugin.ts index dffafce..b801f32 100644 --- a/front-end/src/store/mfaSettingsPlugin.ts +++ b/front-end/src/store/mfaSettingsPlugin.ts @@ -1,62 +1,99 @@ import 'pinia' -import { MaybeRef, shallowRef, watch } from 'vue'; -import { MfaMethod, PkiPublicKey, apiCall, useMfaConfig, usePkiConfig, usePkiAuth } from '@vnuge/vnlib.browser'; -import { useToggle, get } from '@vueuse/core'; +import { MaybeRef, ref, shallowRef, watch } from 'vue'; +import { MfaMethod, PkiPublicKey, apiCall, useMfaConfig, usePkiConfig, usePkiAuth, MfaApi } from '@vnuge/vnlib.browser'; +import { useToggle, get, set } from '@vueuse/core'; import { PiniaPluginContext, PiniaPlugin, storeToRefs } from 'pinia' import { includes } from 'lodash-es'; +import { storeExport, } from './index'; + +interface PkiStore { + publicKeys: PkiPublicKey[] + pkiConfig: ReturnType<typeof usePkiConfig> + pkiAuth: ReturnType<typeof usePkiAuth> + refresh: () => void +} + +export interface MfaSettingsStore{ + mfa:{ + enabledMethods: MfaMethod[] + refresh: () => void + } & MfaApi + pki?: PkiStore +} declare module 'pinia' { - export interface PiniaCustomProperties { - mfaEndabledMethods: MfaMethod[] - mfaConfig: ReturnType<typeof useMfaConfig> - pkiConfig: ReturnType<typeof usePkiConfig> - pkiAuth: ReturnType<typeof usePkiAuth> - pkiPublicKeys: PkiPublicKey[] - mfaRefreshMethods: () => void + export interface PiniaCustomProperties extends MfaSettingsStore { + } } export const mfaSettingsPlugin = (mfaEndpoint: MaybeRef<string>, pkiEndpoint?:MaybeRef<string>): PiniaPlugin => { - return ({ store }: PiniaPluginContext) => { + return ({ store }: PiniaPluginContext): MfaSettingsStore => { const { loggedIn } = storeToRefs(store) const mfaConfig = useMfaConfig(mfaEndpoint) - const pkiConfig = usePkiConfig(pkiEndpoint || '/') - const pkiAuth = usePkiAuth(pkiEndpoint || '/') - const [onRefresh, mfaRefreshMethods] = useToggle() + + const [onRefresh, refresh] = useToggle() + + const enabledMethods = ref<MfaMethod[]>([]) + + const usePki = () => { + + const publicKeys = shallowRef<PkiPublicKey[]>([]) + + const pkiConfig = usePkiConfig(pkiEndpoint || '/') + const pkiAuth = usePkiAuth(pkiEndpoint || '/') + + //Watch for changes to mfa methods (refresh) and update the pki keys + watch([enabledMethods], ([methods]) => { + if (!includes(methods, 'pki' as MfaMethod) || !get(pkiEndpoint)) { + set(publicKeys, []) + return + } - const mfaEndabledMethods = shallowRef<MfaMethod[]>([]) - const pkiPublicKeys = shallowRef<PkiPublicKey[]>([]) + //load the pki keys if pki is enabled + apiCall(async () => publicKeys.value = await pkiConfig.getAllKeys()) + }) + + return{ + publicKeys, + pkiConfig, + pkiAuth, + refresh + } + } watch([loggedIn, onRefresh], ([ li ]) => { if(!li){ - mfaEndabledMethods.value = [] + set(enabledMethods, []) return } //load the mfa methods if the user is logged in - apiCall(async () => mfaEndabledMethods.value = await mfaConfig.getMethods()) - }) - - //Watch for changes to mfa methods (refresh) and update the pki keys - watch([mfaEndabledMethods], ([ methods ]) => { - if(!includes(methods, 'pki' as MfaMethod) || !get(pkiEndpoint)){ - pkiPublicKeys.value = [] - return - } - - //load the pki keys if pki is enabled - apiCall(async () => pkiPublicKeys.value = await pkiConfig.getAllKeys()) + apiCall(async () => enabledMethods.value = await mfaConfig.getMethods()) }) - return{ - mfaRefreshMethods, - mfaEndabledMethods, - mfaConfig, - pkiConfig, - pkiAuth, - pkiPublicKeys + //Only return the pki store if pki is enabled + if(get(pkiEndpoint)){ + return storeExport({ + mfa:{ + enabledMethods, + refresh, + ...mfaConfig + }, + pki: usePki() + }) + } + else{ + return storeExport({ + mfa:{ + enabledMethods, + refresh, + ...mfaConfig + }, + }) + } } }
\ No newline at end of file |