Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>pull/56067/head
parent
095e4709b7
commit
95f2d5dbac
@ -1,16 +0,0 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import Axios from '@nextcloud/axios' |
||||
import { generateUrl } from '@nextcloud/router' |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
export function generateCodes() { |
||||
const url = generateUrl('/apps/twofactor_backupcodes/settings/create') |
||||
|
||||
return Axios.post(url, {}).then((resp) => resp.data) |
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import axios from '@nextcloud/axios' |
||||
import { generateUrl } from '@nextcloud/router' |
||||
|
||||
export interface ITwoFactorBackupCodesState { |
||||
enabled: boolean |
||||
total: number |
||||
used: number |
||||
} |
||||
|
||||
export interface IApiResponse { |
||||
codes: string[] |
||||
state: ITwoFactorBackupCodesState |
||||
} |
||||
|
||||
/** |
||||
* Generate new backup codes |
||||
*/ |
||||
export async function generateCodes(): Promise<IApiResponse> { |
||||
const url = generateUrl('/apps/twofactor_backupcodes/settings/create') |
||||
|
||||
const { data } = await axios.post<IApiResponse>(url) |
||||
return data |
||||
} |
||||
@ -1,16 +0,0 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
/** |
||||
* @param {any} data - |
||||
*/ |
||||
export function print(data) { |
||||
const name = OC.theme.name || 'Nextcloud' |
||||
const newTab = window.open('', t('twofactor_backupcodes', '{name} backup codes', { name })) |
||||
newTab.document.write('<h1>' + t('twofactor_backupcodes', '{name} backup codes', { name }) + '</h1>') |
||||
newTab.document.write('<pre>' + data + '</pre>') |
||||
newTab.print() |
||||
newTab.close() |
||||
} |
||||
@ -0,0 +1,39 @@ |
||||
/*! |
||||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import { getCapabilities } from '@nextcloud/capabilities' |
||||
import { showError } from '@nextcloud/dialogs' |
||||
import { t } from '@nextcloud/l10n' |
||||
|
||||
/** |
||||
* Open a new tab and print the given backup codes |
||||
* |
||||
* @param data - The backup codes to print |
||||
*/ |
||||
export function print(data: string[]): void { |
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const name = (getCapabilities() as any).theming.name || 'Nextcloud' |
||||
const newTab = window.open('', t('twofactor_backupcodes', '{name} backup codes', { name })) |
||||
if (!newTab) { |
||||
showError(t('twofactor_backupcodes', 'Unable to open a new tab for printing')) |
||||
throw new Error('Unable to open a new tab for printing') |
||||
} |
||||
|
||||
const heading = newTab.document.createElement('h1') |
||||
heading.textContent = t('twofactor_backupcodes', '{name} backup codes', { name }) |
||||
const pre = newTab.document.createElement('pre') |
||||
for (const code of data) { |
||||
const codeLine = newTab.document.createTextNode(code) |
||||
pre.appendChild(codeLine) |
||||
pre.appendChild(newTab.document.createElement('br')) |
||||
} |
||||
|
||||
newTab.document.body.innerHTML = '' |
||||
newTab.document.body.appendChild(heading) |
||||
newTab.document.body.appendChild(pre) |
||||
|
||||
newTab.print() |
||||
newTab.close() |
||||
} |
||||
@ -1,53 +0,0 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import Vue from 'vue' |
||||
import Vuex, { Store } from 'vuex' |
||||
import { generateCodes } from './service/BackupCodesService.js' |
||||
|
||||
Vue.use(Vuex) |
||||
|
||||
const state = { |
||||
enabled: false, |
||||
total: 0, |
||||
used: 0, |
||||
codes: [], |
||||
} |
||||
|
||||
const mutations = { |
||||
setEnabled(state, enabled) { |
||||
Vue.set(state, 'enabled', enabled) |
||||
}, |
||||
setTotal(state, total) { |
||||
Vue.set(state, 'total', total) |
||||
}, |
||||
setUsed(state, used) { |
||||
Vue.set(state, 'used', used) |
||||
}, |
||||
setCodes(state, codes) { |
||||
Vue.set(state, 'codes', codes) |
||||
}, |
||||
} |
||||
|
||||
const actions = { |
||||
generate({ commit }) { |
||||
commit('setEnabled', false) |
||||
|
||||
return generateCodes().then(({ codes, state }) => { |
||||
commit('setEnabled', state.enabled) |
||||
commit('setTotal', state.total) |
||||
commit('setUsed', state.used) |
||||
commit('setCodes', codes) |
||||
return true |
||||
}) |
||||
}, |
||||
} |
||||
|
||||
export default new Store({ |
||||
strict: !PRODUCTION, |
||||
state, |
||||
mutations, |
||||
actions, |
||||
}) |
||||
@ -0,0 +1,42 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import type { ITwoFactorBackupCodesState } from '../service/BackupCodesService.ts' |
||||
|
||||
import { loadState } from '@nextcloud/initial-state' |
||||
import { defineStore } from 'pinia' |
||||
import { ref } from 'vue' |
||||
import { generateCodes } from '../service/BackupCodesService.ts' |
||||
|
||||
const initialState = loadState<ITwoFactorBackupCodesState>('twofactor_backupcodes', 'state') |
||||
|
||||
export const useStore = defineStore('twofactor_backupcodes', () => { |
||||
const enabled = ref(initialState.enabled) |
||||
const total = ref(initialState.total) |
||||
const used = ref(initialState.used) |
||||
const codes = ref<string[]>([]) |
||||
|
||||
/** |
||||
* Generate new backup codes and update the store state |
||||
*/ |
||||
async function generate(): Promise<void> { |
||||
enabled.value = false |
||||
|
||||
const { codes: newCodes, state } = await generateCodes() |
||||
enabled.value = state.enabled |
||||
total.value = state.total |
||||
used.value = state.used |
||||
codes.value = newCodes |
||||
} |
||||
|
||||
return { |
||||
enabled, |
||||
total, |
||||
used, |
||||
codes, |
||||
|
||||
generate, |
||||
} |
||||
}) |
||||
Loading…
Reference in new issue