creddy/src/views/Approve.svelte

114 lines
4.0 KiB
Svelte
Raw Normal View History

2022-08-14 13:27:41 -07:00
<script>
2023-05-01 16:53:24 -07:00
import { onMount } from 'svelte';
import { invoke } from '@tauri-apps/api/tauri';
2023-04-24 12:05:11 -07:00
import { navigate } from '../lib/routing.js';
2023-05-01 16:53:24 -07:00
import { appState, completeRequest } from '../lib/state.js';
import ErrorAlert from '../ui/ErrorAlert.svelte';
2023-04-24 22:16:25 -07:00
import Link from '../ui/Link.svelte';
2022-12-22 19:53:14 -08:00
2022-11-23 17:11:44 -08:00
2023-05-01 16:53:24 -07:00
// Send response to backend, display error if applicable
let error, alert;
async function respond() {
let {id, approval} = $appState.currentRequest;
try {
await invoke('respond', {response: {id, approval}});
navigate('ShowResponse');
}
catch (e) {
if (error) {
alert.shake();
}
error = e;
}
}
// Approval has one of several outcomes depending on current credential state
async function approve() {
$appState.currentRequest.approval = 'Approved';
2022-12-13 21:50:34 -08:00
let status = await invoke('get_session_status');
2022-12-13 16:50:44 -08:00
if (status === 'unlocked') {
2023-05-01 16:53:24 -07:00
await respond();
}
2022-12-13 16:50:44 -08:00
else if (status === 'locked') {
2023-04-24 12:05:11 -07:00
navigate('Unlock');
2022-12-13 16:50:44 -08:00
}
else {
2023-04-24 12:05:11 -07:00
navigate('EnterCredentials');
}
2022-11-23 17:11:44 -08:00
}
2023-05-01 16:53:24 -07:00
// Denial has only one
async function deny() {
$appState.currentRequest.approval = 'Denied';
2023-05-01 16:53:24 -07:00
await respond();
}
2023-05-01 16:53:24 -07:00
// Extract executable name from full path
let appName = null;
2023-04-25 08:49:00 -07:00
if ($appState.currentRequest.clients.length === 1) {
let path = $appState.currentRequest.clients[0].exe;
2023-04-23 22:29:12 -07:00
let m = path.match(/\/([^/]+?$)|\\([^\\]+?$)/);
appName = m[1] || m[2];
}
2023-05-01 16:53:24 -07:00
// Executable paths can be long, so ensure they only break on \ or /
function breakPath(client) {
return client.exe.replace(/(\\|\/)/g, '$1<wbr>');
}
// if the request has already been approved/denied, send response immediately
onMount(async () => {
if ($appState.currentRequest.approval) {
await respond();
}
})
2023-04-23 22:29:12 -07:00
</script>
2022-12-19 15:26:44 -08:00
2022-08-14 13:27:41 -07:00
2023-05-01 16:53:24 -07:00
<!-- Don't render at all if we're just going to immediately proceed to the next screen -->
{#if !$appState.currentRequest.approval}
<div class="flex flex-col space-y-4 p-4 m-auto max-w-xl h-screen items-center justify-center">
{#if error}
<ErrorAlert bind:this={alert}>
{error}
<svelte:fragment slot="buttons">
<button class="btn btn-sm btn-alert-error" on:click={completeRequest}>Cancel</button>
<button class="btn btn-sm btn-alert-error" on:click={respond}>Retry</button>
</svelte:fragment>
</ErrorAlert>
{/if}
2023-04-23 22:29:12 -07:00
<div class="space-y-1 mb-4">
<h2 class="text-xl font-bold">{appName ? `"${appName}"` : 'An appplication'} would like to access your AWS credentials.</h2>
2023-05-01 16:53:24 -07:00
<div class="grid grid-cols-[auto_1fr] gap-x-3">
{#each $appState.currentRequest.clients as client}
<div class="text-right">Path:</div>
<code class="">{@html client ? breakPath(client) : 'Unknown'}</code>
<div class="text-right">PID:</div>
<code>{client ? client.pid : 'Unknown'}</code>
{/each}
</div>
2023-04-23 22:29:12 -07:00
</div>
2023-05-01 16:53:24 -07:00
<div class="w-full flex justify-between">
<Link target={deny} hotkey="Escape">
2023-04-24 22:16:25 -07:00
<button class="btn btn-error justify-self-start">
Deny
<kbd class="ml-2 normal-case px-1 py-0.5 rounded border border-neutral">Esc</kbd>
</button>
</Link>
2023-05-01 16:53:24 -07:00
<Link target={approve} hotkey="Enter" shift="{true}">
2023-04-24 22:16:25 -07:00
<button class="btn btn-success justify-self-end">
Approve
<kbd class="ml-2 normal-case px-1 py-0.5 rounded border border-neutral">Shift</kbd>
<span class="mx-0.5">+</span>
<kbd class="normal-case px-1 py-0.5 rounded border border-neutral">Enter</kbd>
</button>
</Link>
2023-04-23 22:29:12 -07:00
</div>
2023-05-01 16:53:24 -07:00
</div>
{/if}