From e87c4b69036e32b4fcf3df89e8158fb52df6a4e0 Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 4 Jan 2024 11:13:31 -0500 Subject: package updates & partial account page added --- extension/src/features/mfa-api.ts | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 extension/src/features/mfa-api.ts (limited to 'extension/src/features/mfa-api.ts') diff --git a/extension/src/features/mfa-api.ts b/extension/src/features/mfa-api.ts new file mode 100644 index 0000000..fc6d51a --- /dev/null +++ b/extension/src/features/mfa-api.ts @@ -0,0 +1,87 @@ +// Copyright (C) 2023 Vaughn Nugent +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +import { get, set, useToggle, watchDebounced } from "@vueuse/core"; +import { computed, shallowRef } from "vue"; +import { } from "lodash"; +import { useSession, useMfaConfig, MfaMethod } from "@vnuge/vnlib.browser"; +import type { TotpUpdateMessage, Watchable } from "./types"; +import type { AppSettings } from "./settings"; +import { type FeatureApi, type BgRuntime, type IFeatureExport, exportForegroundApi, optionsOnly } from "./framework"; +import { waitForChangeFn } from "./util"; + +export type MfaUpdateResult = TotpUpdateMessage + +export interface MfaConfigApi extends FeatureApi, Watchable { + getMfaMethods: () => Promise + enableOrUpdate: (method: MfaMethod, password: string) => Promise + disableMethod: (method: MfaMethod, password: string) => Promise + refresh: () => Promise +} + +export const useMfaConfigApi = (): IFeatureExport => { + + return { + background: ({ state }: BgRuntime): MfaConfigApi => { + const { loggedIn } = useSession(); + const { currentConfig } = state + + const [onRefresh, refresh] = useToggle() + + const mfaPath = computed(() => `${currentConfig.value.accountBasePath}/mfa`) + const mfaConfig = useMfaConfig(mfaPath) + const mfaEnabledMethods = shallowRef([]) + + //Update enabled methods + watchDebounced([currentConfig, loggedIn, onRefresh], async () => { + if(!loggedIn.value){ + set(mfaEnabledMethods, []) + return + } + const methods = await mfaConfig.getMethods() + set(mfaEnabledMethods, methods) + }, { debounce: 100 }) + + return { + waitForChange: waitForChangeFn([currentConfig, loggedIn, mfaEnabledMethods]), + + getMfaMethods: optionsOnly(() => { + return Promise.resolve(get(mfaEnabledMethods)) + }), + enableOrUpdate: optionsOnly(async (method: MfaMethod, password: string) => { + //Exec request to update mfa method + const result = await mfaConfig.initOrUpdateMethod(method, password) + refresh() + return result.getResultOrThrow() + }), + disableMethod: optionsOnly(async (method: MfaMethod, password: string) => { + await mfaConfig.disableMethod(method, password) + refresh() + }), + refresh() { + refresh() + return Promise.resolve() + } + } + }, + foreground: exportForegroundApi([ + 'waitForChange', + 'getMfaMethods', + 'enableOrUpdate', + 'disableMethod', + 'refresh' + ]), + } +} \ No newline at end of file -- cgit