Added Fido API

This commit is contained in:
Spitfireap
2024-06-22 19:45:21 +02:00
parent 37556a03c6
commit 5c5bd47630
24 changed files with 428 additions and 204 deletions
+3
View File
@@ -13,4 +13,7 @@ export default {
changePassword(data) {
return repository.post('/reset_confirm/', data)
},
beginFidoRegistration() {
return repository.post('/fido/registration/begin/')
},
}
@@ -0,0 +1,206 @@
<template>
<div>
<v-card flat>
<v-card-title>
<span class="text-subtitle-1">
{{ $gettext('WebAuthn') }}
</span>
</v-card-title>
<v-card-text>
<div tag="p" class="my-4">
{{
$gettext(
"You don't have any WebAuthN registered as a second authentication method."
)
}}
</div>
<template v-if="browserCapable">
<v-text-field v-model="name" />
<v-btn color="success" :loading="registrationLoading" @click="startFidoRegistration">
{{ $gettext('Add WebAuthN device') }}
</v-btn>
</template>
<template v-else>
<v-alert type="error">
{{ $gettext('Your browser does not seem compatible with WebAuthN') }}
</v-alert>
</template>
</v-card-text>
</v-card>
</div>
</template>
<script setup lang="js">
import { ref, onMounted } from 'vue'
import authApi from '@/api/auth.js'
import { useGettext } from 'vue3-gettext'
const { $gettext } = useGettext()
const name = ref()
const registrationLoading = ref(false)
const creationOption = ref()
const browserCapable = !!(navigator.credentials && navigator.credentials.create && navigator.credentials.get && window.PublicKeyCredential)
async function startFidoRegistration() {
if (creationOption.value) {
navigator.credentials.create({...creationOption.value})
.then(function (attestation) {
const result = createResponseToJSON(attestation)
console.log(result)
})
}
}
onMounted(() => {
registrationLoading.value = true
authApi.beginFidoRegistration().then((resp) => {
creationOption.value = createRequestFromJSON(resp.data)
}).finally(() => registrationLoading.value = false)
})
// Taken from the example of python-fido2 repo
// src/webauthn-json/base64url.ts
function base64urlToBuffer(baseurl64String) {
const padding = "==".slice(0, (4 - baseurl64String.length % 4) % 4);
const base64String = baseurl64String.replace(/-/g, "+").replace(/_/g, "/") + padding;
const str = atob(base64String);
const buffer = new ArrayBuffer(str.length);
const byteView = new Uint8Array(buffer);
for (let i = 0; i < str.length; i++) {
byteView[i] = str.charCodeAt(i);
}
return buffer;
}
function bufferToBase64url(buffer) {
const byteView = new Uint8Array(buffer);
let str = "";
for (const charCode of byteView) {
str += String.fromCharCode(charCode);
}
const base64String = btoa(str);
const base64urlString = base64String.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
return base64urlString;
}
// src/webauthn-json/convert.ts
const copyValue = "copy";
const convertValue = "convert";
function convert(conversionFn, schema, input) {
if (schema === copyValue) {
return input;
}
if (schema === convertValue) {
return conversionFn(input);
}
if (schema instanceof Array) {
return input.map((v) => convert(conversionFn, schema[0], v));
}
if (schema instanceof Object) {
const output = {};
for (const [key, schemaField] of Object.entries(schema)) {
if (schemaField.derive) {
const v = schemaField.derive(input);
if (v !== void 0) {
input[key] = v;
}
}
if (!(key in input)) {
if (schemaField.required) {
throw new Error(`Missing key: ${key}`);
}
continue;
}
if (input[key] == null) {
output[key] = null;
continue;
}
output[key] = convert(conversionFn, schemaField.schema, input[key]);
}
return output;
}
}
function derived(schema, derive) {
return {
required: true,
schema,
derive
};
}
function required(schema) {
return {
required: true,
schema
};
}
function optional(schema) {
return {
required: false,
schema
};
}
const publicKeyCredentialDescriptorSchema = {
type: required(copyValue),
id: required(convertValue),
transports: optional(copyValue)
}
const simplifiedExtensionsSchema = {
appid: optional(copyValue),
appidExclude: optional(copyValue),
credProps: optional(copyValue)
}
const simplifiedClientExtensionResultsSchema = {
appid: optional(copyValue),
appidExclude: optional(copyValue),
credProps: optional(copyValue)
}
const credentialCreationOptions = {
publicKey: required({
rp: required(copyValue),
user: required({
id: required(convertValue),
name: required(copyValue),
displayName: required(copyValue)
}),
challenge: required(convertValue),
pubKeyCredParams: required(copyValue),
timeout: optional(copyValue),
excludeCredentials: optional([publicKeyCredentialDescriptorSchema]),
authenticatorSelection: optional(copyValue),
attestation: optional(copyValue),
extensions: optional(simplifiedExtensionsSchema)
}),
signal: optional(copyValue)
}
const publicKeyCredentialWithAttestation = {
type: required(copyValue),
id: required(copyValue),
rawId: required(convertValue),
authenticatorAttachment: optional(copyValue),
response: required({
clientDataJSON: required(convertValue),
attestationObject: required(convertValue),
transports: derived(copyValue, (response) => {
var _a;
return ((_a = response.getTransports) == null ? void 0 : _a.call(response)) || [];
})
}),
clientExtensionResults: derived(simplifiedClientExtensionResultsSchema, (pkc) => pkc.getClientExtensionResults())
}
function createRequestFromJSON(requestJSON) {
return convert(base64urlToBuffer, credentialCreationOptions, requestJSON);
}
function createResponseToJSON(credential) {
return convert(bufferToBase64url, publicKeyCredentialWithAttestation, credential);
}
</script>
@@ -0,0 +1,54 @@
<template>
<v-card>
<v-card-title>
<span class="headline"> {{ $gettext('WebAuthN registration') }} </span>
</v-card-title>
<v-card-text>
<v-btn color="success" :loading="registrationLoading" @click="startFidoRegistration">{{ $gettext('Register device') }}</v-btn>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="grey darken-1" text @click="close">
{{ $gettext('Close') }}
</v-btn>
</v-card-actions>
</v-card>
</template>
<script setup lang="js">
import { useGettext } from 'vue3-gettext'
import { ref, onMounted } from 'vue'
const { $gettext } = useGettext()
const name = ref()
const registrationLoading = ref(false)
const publicKey = ref()
async function startFidoRegistration() {
if (publicKey.value) {
navigator.credentials.create(publicKey.value)
.then(function (attestation) {
// Send new credential info to server for verification and registration.
console.log(attestation)
})
.catch(function (err) {
// No acceptable authenticator or user refused consent. Handle appropriately.
console.log(err)
})
}
}
onMounted(() => {
registrationLoading.value = true
authApi.beginFidoRegistration().then((resp) => {
publicKey.value = resp.data.publicKey
}).finally(() => registrationLoading.value = false)
})
const emit = defineEmits(['close'])
function close() {
emit('close')
}
</script>
-131
View File
@@ -1,131 +0,0 @@
<template>
<div>
<v-toolbar flat>
<v-toolbar-title>{{ $gettext('API access') }}</v-toolbar-title>
</v-toolbar>
<div class="pa-4">
<v-card v-if="!token" flat>
<v-card-text>
{{ $gettext('Your API token has not been generated yet.') }}
</v-card-text>
<v-card-actions>
<v-btn
color="success"
variant="flat"
:loading="loading"
@click="generateKey"
>
{{ $gettext('Generate') }}
</v-btn>
</v-card-actions>
</v-card>
<v-card v-else flat>
<v-card-text>
<label class="m-label">
{{ $gettext('Your API token is:') }}
</label>
<code class="ml-5 mr-5">
{{ token }}
</code>
<v-btn
icon="mdi-clipboard-plus"
density="compact"
:title="$gettext('Copy token to clipboard')"
variant="text"
@click="copyToClipboard()"
>
</v-btn>
<v-btn
icon="mdi-delete"
density="compact"
color="error"
variant="text"
:title="$gettext('Delete token')"
@click="openDeletionDialog"
>
</v-btn>
</v-card-text>
</v-card>
<v-alert type="info" variant="tonal" border="start" class="mt-4">
{{ $gettext('A documentation of the API is available') }}
<a :href="apiDocUrl" target="_blank">{{ $gettext('here') }}</a
>.
</v-alert>
</div>
<ConfirmDialog ref="confirm" @agree="deleteToken" />
</div>
</template>
<script setup lang="js">
import accountApi from '@/api/account'
import { useBusStore } from '@/stores'
import ConfirmDialog from '@/components/tools/ConfirmDialog.vue'
import { ref, computed, inject, onMounted } from 'vue'
import { useGettext } from 'vue3-gettext'
const { $gettext } = useGettext()
const busStore = useBusStore()
const $config = inject('$config')
const apiDocUrl = computed(() => $config.API_DOC_URL)
const loading = ref(false)
const token = ref(null)
const confirm = ref()
function copyToClipboard() {
navigator.clipboard.writeText(token.value)
busStore.displayNotification({
msg: $gettext('API token copied to your clipboard'),
type: 'success',
})
}
function generateKey() {
loading.value = true
accountApi
.createAPIToken()
.then((resp) => {
token.value = resp.data.token
busStore.displayNotification({
msg: $gettext('API token created'),
type: 'success',
})
})
.finally(() => (loading.value = false))
}
function openDeletionDialog() {
confirm.value.open(
$gettext('Warning'),
$gettext('You are about to delete your API access token'),
{
color: 'error',
cancelLabel: $gettext('Cancel'),
agreeLabel: $gettext('Proceed'),
}
)
}
async function deleteToken() {
await accountApi.deleteAPIToken()
busStore.displayNotification({
msg: $gettext('API token deleted'),
type: 'success',
})
token.value = null
}
onMounted(() => {
accountApi.getAPIToken().then((resp) => {
token.value = resp.data.token
})
})
</script>
<style scoped>
.v-toolbar {
background-color: #f7f8fa !important;
}
</style>
@@ -1,18 +0,0 @@
<template>
<v-toolbar flat>
<v-toolbar-title>{{ $gettext('Forward') }}</v-toolbar-title>
</v-toolbar>
<div class="pa-4">
<ForwardForm />
</div>
</template>
<script setup lang="js">
import ForwardForm from '@/components/account/ForwardForm.vue'
</script>
<style scoped>
.v-toolbar {
background-color: #f7f8fa !important;
}
</style>
@@ -1,23 +0,0 @@
<template>
<div>
<v-toolbar flat>
<v-toolbar-title>{{ $gettext('Profile') }}</v-toolbar-title>
</v-toolbar>
<div class="pa-4">
<ProfileForm />
</div>
</div>
</template>
<script setup lang="js">
import ProfileForm from '@/components/account/ProfileForm.vue'
import { useGettext } from 'vue3-gettext'
const { $gettext } = useGettext()
</script>
<style scoped>
.v-toolbar {
background-color: #f7f8fa !important;
}
</style>
@@ -1,23 +0,0 @@
<template>
<div>
<v-toolbar flat>
<v-toolbar-title>{{ $gettext('Security') }}</v-toolbar-title>
</v-toolbar>
<div class="pa-4">
<TwoFactorAuthForm />
</div>
</div>
</template>
<script setup lang="js">
import TwoFactorAuthForm from '@/components/account/TwoFactorAuthForm.vue'
import { useGettext } from 'vue3-gettext'
const { $gettext } = useGettext()
</script>
<style scoped>
.v-toolbar {
background-color: #f7f8fa !important;
}
</style>
@@ -30,6 +30,7 @@
</v-window-item>
<v-window-item value="security">
<TwoFactorAuthForm />
<FidoAuthForm />
</v-window-item>
<v-window-item value="forward" v-if="authStore.userHasMailbox">
<ForwardForm />
@@ -51,6 +52,7 @@ import TwoFactorAuthForm from '@/components/account/TwoFactorAuthForm.vue'
import ForwardForm from '@/components/account/ForwardForm.vue'
import AutoReplyForm from '@/components/account/AutoReplyForm.vue'
import APISetupForm from '@/components/account/APISetupForm.vue'
import FidoAuthForm from '@/components/account/FidoAuthForm.vue'
import { useAuthStore } from '@/stores'
const authStore = useAuthStore()