From 5abc489b9954111d66d1385aa62a3ea962fa0a55 Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 30 Jan 2024 15:23:06 -0500 Subject: merge upstream. Add dynamic client-side support for optional oauth2 and social login methods --- front-end/src/store/mfaSettingsPlugin.ts | 109 +++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 36 deletions(-) (limited to 'front-end/src/store/mfaSettingsPlugin.ts') 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 + pkiAuth: ReturnType + refresh: () => void +} + +export interface MfaSettingsStore{ + mfa:{ + enabledMethods: MfaMethod[] + refresh: () => void + } & MfaApi + pki?: PkiStore +} declare module 'pinia' { - export interface PiniaCustomProperties { - mfaEndabledMethods: MfaMethod[] - mfaConfig: ReturnType - pkiConfig: ReturnType - pkiAuth: ReturnType - pkiPublicKeys: PkiPublicKey[] - mfaRefreshMethods: () => void + export interface PiniaCustomProperties extends MfaSettingsStore { + } } export const mfaSettingsPlugin = (mfaEndpoint: MaybeRef, pkiEndpoint?:MaybeRef): 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([]) + + const usePki = () => { + + const publicKeys = shallowRef([]) + + 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([]) - const pkiPublicKeys = shallowRef([]) + //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 -- cgit