Files
cloudron-box/dashboard/src/views/OpenIdView.vue
T

256 lines
8.3 KiB
Vue
Raw Normal View History

2025-01-19 19:53:29 +01:00
<script setup>
import { useI18n } from 'vue-i18n';
const i18n = useI18n();
const t = i18n.t;
2025-12-05 18:06:03 +01:00
import { ref, onMounted, useTemplateRef } from 'vue';
2025-12-20 08:39:37 +01:00
import { Button, ClipboardButton, SingleSelect, Dialog, TableView, FormGroup, TextInput, InputGroup, InputDialog } from '@cloudron/pankow';
import ActionBar from '../components/ActionBar.vue';
2025-04-21 12:48:22 +02:00
import Section from '../components/Section.vue';
2025-01-19 19:53:29 +01:00
import DashboardModel from '../models/DashboardModel.js';
import UserDirectoryModel from '../models/UserDirectoryModel.js';
2025-01-31 21:02:48 +01:00
const dashboardModel = DashboardModel.create();
const userDirectoryModel = UserDirectoryModel.create();
2025-01-19 19:53:29 +01:00
const columns = {
name: { label: 'Name', sort: true },
actions: {}
};
2025-12-20 08:39:37 +01:00
function createActionMenu(client) {
return [{
icon: 'fa-solid fa-pencil-alt',
label: t('main.action.edit'),
2025-12-20 08:39:37 +01:00
quickAction: true,
action: onEdit.bind(null, client),
}, {
icon: 'fa-solid fa-trash-alt',
label: t('main.action.remove'),
2025-12-20 08:39:37 +01:00
quickAction: true,
action: onRemove.bind(null, client),
}];
}
2025-01-19 19:53:29 +01:00
const inputDialog = useTemplateRef('inputDialog');
2025-01-20 16:53:31 +01:00
const editDialog = useTemplateRef('editDialog');
const newSetDialog = useTemplateRef('newSetDialog');
2025-01-19 19:53:29 +01:00
const clients = ref([]);
2025-09-12 16:07:39 +02:00
const discoveryUrl = ref('');
2025-01-19 19:53:29 +01:00
2025-01-20 16:53:31 +01:00
// edit or add
const submitBusy = ref(false);
const submitError = ref('');
const clientId = ref('');
const clientSecret = ref('');
const newClientId = ref('');
const newClientSecret = ref('');
2025-01-20 16:53:31 +01:00
const clientName = ref('');
const clientLoginRedirectUri = ref('');
const clientTokenSignatureAlgorithm = ref('RS256');
const signatureAlgorithms = [
{ name: 'RS256', value: 'RS256' },
{ name: 'EdDSA', value: 'EdDSA' },
];
2025-12-05 18:06:03 +01:00
const form = useTemplateRef('form');
const isFormValid = ref(false);
function checkValidity() {
isFormValid.value = form.value ? form.value.checkValidity() : false;
}
2025-01-20 16:53:31 +01:00
2025-01-19 19:53:29 +01:00
async function onAdd() {
2025-01-20 16:53:31 +01:00
submitBusy.value = false;
clientId.value = '';
clientSecret.value = '';
clientName.value = '';
clientLoginRedirectUri.value = '';
clientTokenSignatureAlgorithm.value = 'RS256';
editDialog.value.open();
2025-12-05 18:06:03 +01:00
setTimeout(checkValidity, 100); // update state of the confirm button
2025-01-19 19:53:29 +01:00
}
async function onEdit(client) {
2025-01-20 16:53:31 +01:00
submitBusy.value = false;
clientId.value = client.id;
clientSecret.value = client.secret;
clientName.value = client.name;
clientLoginRedirectUri.value = client.loginRedirectUri;
clientTokenSignatureAlgorithm.value = client.tokenSignatureAlgorithm;
editDialog.value.open();
2025-12-05 18:06:03 +01:00
setTimeout(checkValidity, 100); // update state of the confirm button
2025-01-20 16:53:31 +01:00
}
async function onSubmit() {
2025-12-05 18:06:03 +01:00
if (!form.value.reportValidity()) return;
2025-01-20 16:53:31 +01:00
submitBusy.value = true;
let error, client;
if (clientId.value) { // edit
[error] = await userDirectoryModel.updateOpenIdClient(clientId.value, clientName.value, clientLoginRedirectUri.value, clientTokenSignatureAlgorithm.value);
} else { // add
[error, client] = await userDirectoryModel.addOpenIdClient(clientName.value, clientLoginRedirectUri.value, clientTokenSignatureAlgorithm.value);
}
2025-01-20 16:53:31 +01:00
if (error) {
submitBusy.value = false;
submitError.value = error.body ? error.body.message : 'Internal error';
return console.error(error);
2025-01-20 16:53:31 +01:00
}
await refresh();
editDialog.value.close();
submitBusy.value = false;
// reopen to show the new client credentials
if (client) {
newClientId.value = client.id;
newClientSecret.value = client.secret;
newSetDialog.value.open();
}
2025-01-19 19:53:29 +01:00
}
async function onRemove(client) {
const yes = await inputDialog.value.confirm({
2025-11-07 10:45:21 +01:00
title: t('oidc.deleteClientDialog.title'),
message: t('oidc.deleteClientDialog.description', { clientName: client.name }),
2025-01-19 19:53:29 +01:00
confirmStyle: 'danger',
confirmLabel: t('main.dialog.delete'),
2025-11-07 10:45:21 +01:00
rejectLabel: t('main.dialog.cancel'),
rejectStyle: 'secondary'
2025-01-19 19:53:29 +01:00
});
if (!yes) return;
await userDirectoryModel.removeOpenIdClient(client.id);
await refresh();
}
async function refresh() {
const [error, result] = await userDirectoryModel.getOpenIdClients();
if (error) return console.error(error);
2025-01-19 19:53:29 +01:00
clients.value = result;
}
onMounted(async () => {
2025-03-12 13:41:07 +01:00
const [error, result] = await dashboardModel.config();
if (error) return console.error(error);
2025-01-24 14:00:33 +01:00
2025-09-12 16:07:39 +02:00
discoveryUrl.value = `https://${result.adminFqdn}/.well-known/openid-configuration`;
2025-01-19 19:53:29 +01:00
await refresh();
});
</script>
<template>
2025-04-21 12:48:22 +02:00
<div class="content">
2025-01-19 19:53:29 +01:00
<InputDialog ref="inputDialog" />
<Dialog ref="newSetDialog"
2025-11-07 10:45:21 +01:00
:title="$t('oidc.clientCredentials.title')"
:reject-label="$t('main.dialog.close')"
2025-11-07 10:45:21 +01:00
reject-style="secondary"
>
<div>
2025-11-07 10:45:21 +01:00
<div>{{ $t('oidc.clientCredentials.description', { clientName: clientName }) }}</div>
<br/>
<FormGroup>
<label for="clientIdInput">{{ $t('oidc.client.id') }}</label>
<InputGroup>
<TextInput id="clientIdInput" v-model="newClientId" readonly style="flex-grow: 1;"/>
<ClipboardButton :value="newClientId" />
</InputGroup>
</FormGroup>
<FormGroup>
<label for="clientSecretInput">{{ $t('oidc.client.secret') }}</label>
<InputGroup>
<TextInput id="clientSecretInput" v-model="newClientSecret" readonly style="flex-grow: 1;"/>
<ClipboardButton :value="newClientSecret" />
</InputGroup>
</FormGroup>
</div>
</Dialog>
2025-01-20 16:53:31 +01:00
<Dialog ref="editDialog"
2025-11-07 10:45:21 +01:00
:title="clientId ? $t('oidc.editClientDialog.title') : $t('oidc.newClientDialog.title')"
2025-12-05 18:06:03 +01:00
:confirm-active="!submitBusy && isFormValid"
2025-01-20 16:53:31 +01:00
:confirm-busy="submitBusy"
:confirm-label="clientId ? $t('main.dialog.save') : $t('oidc.newClientDialog.createAction')"
2025-11-07 10:45:21 +01:00
:reject-label="$t('main.dialog.cancel')"
2025-01-20 16:53:31 +01:00
reject-style="secondary"
@confirm="onSubmit()"
>
2025-12-05 18:06:03 +01:00
<form @submit.prevent="onSubmit()" autocomplete="off" ref="form" @input="checkValidity()">
<input style="display: none" type="submit" />
2025-01-20 16:53:31 +01:00
2025-10-09 10:58:50 +02:00
<div class="error-label" v-if="submitError">{{ submitError }}</div>
2025-06-12 01:00:25 +02:00
<FormGroup>
<label for="clientNameInput">{{ $t('oidc.client.name') }}</label>
<TextInput id="clientNameInput" v-model="clientName" required/>
</FormGroup>
2025-01-20 16:53:31 +01:00
<FormGroup v-show="clientId">
<label for="clientIdInput">{{ $t('oidc.client.id') }}</label>
<InputGroup>
<TextInput id="clientIdInput" v-model="clientId" readonly style="flex-grow: 1;"/>
<ClipboardButton :value="clientId" />
</InputGroup>
2025-01-20 16:53:31 +01:00
</FormGroup>
<FormGroup v-show="clientSecret">
<label for="clientSecretInput">{{ $t('oidc.client.secret') }}</label>
<InputGroup>
<TextInput id="clientSecretInput" v-model="clientSecret" readonly style="flex-grow: 1;"/>
<ClipboardButton :value="clientSecret" />
</InputGroup>
2025-01-20 16:53:31 +01:00
</FormGroup>
<FormGroup>
<label for="clientLoginRedirectUriInput">{{ $t('oidc.client.loginRedirectUri') }}</label>
<TextInput id="clientLoginRedirectUriInput" v-model="clientLoginRedirectUri" required/>
2025-11-07 10:45:21 +01:00
<small class="helper-text">{{ $t('oidc.client.loginRedirectUriPlaceholder') }}</small>
2025-01-20 16:53:31 +01:00
</FormGroup>
<FormGroup>
<label class="control-label">{{ $t('oidc.client.signingAlgorithm') }}</label>
2025-12-05 18:06:03 +01:00
<SingleSelect v-model="clientTokenSignatureAlgorithm" :options="signatureAlgorithms" option-key="value" option-label="name" required />
2025-01-20 16:53:31 +01:00
</FormGroup>
</form>
</Dialog>
2025-01-19 19:53:29 +01:00
2025-10-17 16:46:51 +02:00
<Section title="OpenID">
<div>{{ $t('oidc.description') }}</div>
<br/>
2025-01-20 16:53:31 +01:00
2025-09-12 16:07:39 +02:00
<FormGroup>
<label for="discoverUrlInput">{{ $t('oidc.env.discoveryUrl') }} <sup><a href="https://docs.cloudron.io/user-directory/#endpoints" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
<InputGroup>
<TextInput id="discoveryUrlInput" v-model="discoveryUrl" readonly style="flex-grow: 1;"/>
<ClipboardButton :value="discoveryUrl" />
2025-09-12 16:07:39 +02:00
</InputGroup>
</FormGroup>
2025-09-12 16:14:00 +02:00
</Section>
2025-01-19 19:53:29 +01:00
2025-09-12 16:14:00 +02:00
<Section :title="$t('oidc.clients.title')">
<template #header-buttons>
<Button @click="onAdd()">{{ $t('main.action.add') }}</Button>
2025-09-12 16:14:00 +02:00
</template>
2025-07-16 12:49:22 +02:00
<TableView :columns="columns" :model="clients" :placeholder="$t('oidc.clients.empty')">
<template #actions="client">
2025-12-20 08:39:37 +01:00
<ActionBar :actions="createActionMenu(client)"/>
2025-01-19 19:53:29 +01:00
</template>
</TableView>
</Section>
</div>
</template>