Add OAuth authorization page

This commit is contained in:
Pixeldrain-Server committed 2026-10-05 13:46:24 +02:00
1 parent a513e731c4
commit 93d5583940
10 files changed
+288 -35

No files matched your search

+121 -4
View File
@@ -2,7 +2,8 @@
import { onMount } from "svelte";
import { loading_run } from "lib/Loading";
import { check_response, dict_to_form, get_endpoint, type UserSession } from "lib/NovaAPI";
import { fs_get_node } from "lib/FilesystemAPI.svelte";
import { fs_get_node, type FSNode } from "lib/FilesystemAPI.svelte";
import FilePicker from "filesystem/filemanager/FilePicker.svelte";
import { formatDate } from "util/Formatting";
import CopyButton from "layout/CopyButton.svelte";
import NovaLogo from "util/NovaLogo.svelte";
@@ -26,6 +27,52 @@ let dir_names: {[id: string]: string} = $state({})
// The keys of which the whole user agent is shown
let agent_shown: {[key: string]: boolean} = $state({})
// The key which is being edited, and what its permissions and directories are
// going to be. Nothing changes until it is saved
let editing = $state("")
let edit_perms: {[name: string]: boolean} = $state({})
let edit_dirs: string[] = $state([])
let picker: FilePicker = $state()
const edit_key = (key: UserSession) => {
editing = key.auth_key
edit_dirs = [...key.filesystem_dirs]
edit_perms = {}
for (const [, , read, write] of components) {
edit_perms[read] = key[read] as boolean
edit_perms[write] = key[write] as boolean
}
}
const pick_dir = (nodes: FSNode[]) => {
const dir = nodes[0]
dir_names[dir.id] = dir.name
if (!edit_dirs.includes(dir.id)) {
edit_dirs.push(dir.id)
}
}
const save_key = async () => {
try {
await loading_run(async () => check_response(
await fetch(get_endpoint() + "/user/session", {
method: "PUT",
body: dict_to_form({
auth_key: editing,
...edit_perms,
filesystem_dirs: edit_dirs,
}),
})
))
} catch (err) {
alert("Failed to change key: " + (err.message ?? err))
return
}
editing = ""
await load_keys()
}
// Intl.RelativeTimeFormat is the browser's formatter for "5 minutes ago", it
// takes care of the plurals
const relative = new Intl.RelativeTimeFormat("en", {numeric: "always", style: "short"})
@@ -70,10 +117,16 @@ const load_keys = async () => {
const create_key = async () => {
try {
// Keys are made to be put in other programs, where they can leak. With
// account write a key can change the password and the e-mail address,
// which is enough to take the account over, so these don't get it
const key: UserSession = await loading_run(async () => check_response(
await fetch(get_endpoint() + "/user/session", {
method: "POST",
body: dict_to_form({app_name: "website keys page"}),
body: dict_to_form({
app_name: "website keys page",
account_write: false,
}),
})
))
shown[key.auth_key] = true
@@ -149,6 +202,13 @@ onMount(load_keys)
<span class="badge">This session</span>
{/if}
<div class="toolbar_spacer"></div>
<!-- Not the session which is in use. Without its permissions
this page and the rest of the website stop working -->
{#if !key.current && editing !== key.auth_key}
<button onclick={() => edit_key(key)}>
<i class="icon">edit</i> Edit
</button>
{/if}
<button class="button_red" onclick={() => delete_key(key)}>
<i class="icon">delete</i> Delete
</button>
@@ -168,13 +228,61 @@ onMount(load_keys)
<CopyButton text={key.auth_key}>Copy</CopyButton>
</div>
{#if editing === key.auth_key}
<div class="edit">
{#each components as [name, icon, read, write]}
<div class="row">
<span class="name"><i class="icon small">{icon}</i> {name}</span>
<label>
<input type="checkbox" bind:checked={edit_perms[read]}/> read
</label>
<!-- Account write can be taken away from a key, but
not given to one -->
{#if write !== "account_write" || key.account_write}
<label>
<input type="checkbox" bind:checked={edit_perms[write]}/> write
</label>
{/if}
</div>
{/each}
<div class="row">
{#if edit_dirs.length === 0}
<span>Files access is not limited to directories</span>
{:else}
<span>Files access is limited to</span>
{#each edit_dirs as id, index (id)}
<span>
<i class="icon small">folder</i>{dir_names[id] ?? id}
<button onclick={() => edit_dirs.splice(index, 1)} title="Remove">
<i class="icon small">close</i>
</button>
</span>
{/each}
{/if}
<button onclick={() => picker.open("/me")}>
<i class="icon">folder</i>
{edit_dirs.length === 0 ? "Limit to a directory" : "Add a directory"}
</button>
</div>
<div class="row">
<button class="button_highlight" onclick={save_key}>
<i class="icon">save</i> Save
</button>
<button onclick={() => editing = ""}>
<i class="icon">close</i> Cancel
</button>
</div>
</div>
{:else}
<div class="row permissions">
{#each components as [name, icon, read, write]}
<span class:none={!key[read] && !key[write]}>
<i class="icon small">{icon}</i>
{name}:
{#if key[read] && key[write]}
read and write
read / write
{:else if key[read]}
read only
{:else if key[write]}
@@ -193,6 +301,7 @@ onMount(load_keys)
{/each}
</div>
{/if}
{/if}
<div class="details">
<div>
@@ -223,6 +332,8 @@ onMount(load_keys)
{/each}
</section>
<FilePicker bind:this={picker} callback={pick_dir} directories/>
<style>
.toolbar {
display: flex;
@@ -253,11 +364,17 @@ onMount(load_keys)
layout would put the icon on the baseline of the text */
.app,
.permissions > span,
.row > a {
.row > a,
.edit label,
.edit span {
display: inline-flex;
align-items: center;
gap: 0.3em;
}
/* The name of a permission, in front of its checkboxes */
.name {
min-width: 6em;
}
.app {
font-size: 1.1em;
}
@@ -15,7 +15,7 @@ let billed = $derived(billable && $user.usage_based_billing)
let included = $derived($user === null ? 0 : $user.subscription.monthly_transfer_cap)
</script>
<ProgressBar total={total} used={used}></ProgressBar>
<ProgressBar total={total} used={used} warn></ProgressBar>
<div class="gauge_labels">
<div>
{formatDataVolume(used, 3)}
+1 -1
View File
@@ -122,7 +122,7 @@ onMount(async () => {
Bandwidth used in the last 30 days: {formatDataVolume(transfer_used, 3)},
new limit: {formatDataVolume(transfer_cap*1e12, 3)}
</p>
<ProgressBar used={transfer_used} total={transfer_cap*1e12}></ProgressBar>
<ProgressBar used={transfer_used} total={transfer_cap*1e12} warn></ProgressBar>
<p>
The billshock limit limits how much bandwidth your account can use in a
30 day window. When this limit is reached hotlinking will be disabled
@@ -13,7 +13,7 @@ let billable = $derived($user !== null && $user.subscription.price_per_tb_storag
let billed = $derived(billable && $user.usage_based_billing)
</script>
<ProgressBar total={total} used={used}></ProgressBar>
<ProgressBar total={total} used={used} warn></ProgressBar>
<div class="gauge_labels">
<div>
{formatDataVolume(used, 3)}