feat: api keys

This commit is contained in:
Torsten Dittmann committed 2022-11-03 13:42:53 +01:00
1 parent 7bcd949d66
commit 2a1d08c3cc
12 files changed
+183 -60

No files matched your search

+11 -1
View File
@@ -1,3 +1,7 @@
<script lang="ts">
export let withIndentation = false;
</script>
<li class="collapsible-item">
<details class="collapsible-wrapper">
<summary class="collapsible-button">
@@ -7,8 +11,14 @@
<span class="icon-cheveron-down" aria-hidden="true" />
</div>
</summary>
<div class="collapsible-content">
<div class="collapsible-content" class:indentation={withIndentation}>
<slot />
</div>
</details>
</li>
<style>
.indentation {
padding-inline-start: 1.5rem;
}
</style>
+5 -1
View File
@@ -32,7 +32,11 @@ export enum Dependencies {
WEBHOOKS = 'webhooks'
}
export const scopes = [
export const scopes: {
scope: string;
description: string;
category: string;
}[] = [
{
scope: 'users.read',
description: "Access to read your project's users",
-1
View File
@@ -26,7 +26,6 @@ const setProviders = (project: Models.Project): Provider[] => {
switch (n.name.toLowerCase()) {
case 'amazon':
console.log(n);
docs = 'https://developer.amazon.com/apps-and-games/services-and-apis';
break;
case 'apple':
@@ -15,11 +15,9 @@
import { Dependencies } from '$lib/constants';
async function onFinish() {
console.log(2);
await invalidate(Dependencies.FUNCTIONS);
}
async function create() {
console.log(1);
try {
const response = await sdkForProject.functions.create(
$createFunction.id ?? 'unique()',
@@ -12,7 +12,6 @@
$: projectId = $page.params.project;
$: path = `/console/project-${projectId}/overview`;
$: console.log($stats.get(projectId));
let period: UsagePeriods = '30d';
let showPeriodBandwidth = false;
let showPeriodRequests = false;
@@ -10,16 +10,21 @@
TableRowLink
} from '$lib/elements/table';
import { toLocaleDateTime } from '$lib/helpers/date';
import { wizard } from '$lib/stores/wizard';
import type { PageData } from './$types';
import Create from './create.svelte';
import Wizard from './wizard.svelte';
export let data: PageData;
let show = false;
function create() {
wizard.start(Wizard);
}
</script>
<div class="common-section u-flex u-gap-12">
<Heading tag="h3" size="7">API Keys</Heading>
<span class="u-margin-inline-start-auto">
<Button on:click={() => (show = true)}>
<Button on:click={create}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Create API Key</span>
</Button>
@@ -32,7 +37,7 @@
<TableCellHead>Name</TableCellHead>
<TableCellHead>last accessed</TableCellHead>
<TableCellHead>expiration date</TableCellHead>
<TableCellHead>clients</TableCellHead>
<TableCellHead>scopes</TableCellHead>
</TableHeader>
<TableBody>
{#each data.keys.keys as key}
@@ -44,15 +49,17 @@
{key.accessedAt ? toLocaleDateTime(key.accessedAt) : 'never'}
</TableCellText>
<TableCellText title="Expiration Date">
{toLocaleDateTime(key.$updatedAt)}
{key.expire ? toLocaleDateTime(key.expire) : 'never'}
</TableCellText>
<TableCellText title="Expiration Date">
{key.scopes.length} Scopes
</TableCellText>
<TableCellText title="Clients" />
</TableRowLink>
{/each}
</TableBody>
</Table>
{:else}
<Empty isButton single on:click={() => (show = true)}>
<Empty isButton single on:click={create}>
<div class="u-text-center">
<p class="text u-line-height-1-5">Create your first API Key to get started</p>
<p class="text u-line-height-1-5">Need a hand? Check out our documentation.</p>
@@ -63,5 +70,3 @@
</div>
</Empty>
{/if}
<Create bind:show />
@@ -1,42 +0,0 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Modal } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { InputText, Button, FormList } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { project } from '../../.../../store';
import Scopes from './scopes.svelte';
export let show = false;
let name: string;
let scopes: string[] = [];
async function create() {
try {
await sdkForConsole.projects.createKey($project.$id, name, scopes);
name = null;
scopes = [];
invalidate(Dependencies.KEYS);
show = false;
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
}
</script>
<Modal bind:show on:submit={create} size="big">
<svelte:fragment slot="header">Create API Key</svelte:fragment>
<FormList>
<InputText id="name" label="Name" bind:value={name} autofocus required />
<Scopes bind:scopes />
</FormList>
<svelte:fragment slot="footer">
<Button submit>Register</Button>
<Button secondary on:click={() => (show = false)}>Cancel</Button>
</svelte:fragment>
</Modal>
@@ -7,6 +7,14 @@
export let scopes: string[];
enum Category {
Authentication = 'Authentication',
Database = 'Database',
Functions = 'Functions',
Storage = 'Storage',
Other = 'Other'
}
let mounted = false;
onMount(() => {
@@ -28,6 +36,29 @@
}
}
function categoryState(category: string, s: string[]): boolean | null {
const scopesByCategory = allScopes.filter((n) => n.category === category);
const filtered = scopesByCategory.filter((n) => s.includes(n.scope));
if (filtered.length === 0) {
return false;
} else if (filtered.length === scopesByCategory.length) {
return true;
} else {
return null;
}
}
function onCategoryChange(
event: Event & { currentTarget: EventTarget & HTMLInputElement },
category: Category
) {
allScopes.forEach((s) => {
if (s.category === category) {
activeScopes[s.scope] = event.currentTarget.checked;
}
});
}
const activeScopes = allScopes.reduce((prev, next) => {
prev[next.scope] = false;
@@ -54,9 +85,21 @@
<Button text on:click={selectAll}>Select all</Button>
</div>
<Collapsible>
{#each ['Authentication', 'Database', 'Functions', 'Storage', 'Other'] as category}
<CollapsibleItem>
<svelte:fragment slot="title">{category}</svelte:fragment>
{#each [Category.Authentication, Category.Database, Category.Functions, Category.Storage, Category.Other] as category}
<CollapsibleItem withIndentation>
<svelte:fragment slot="title">
{@const checked = categoryState(category, scopes)}
<input
type="checkbox"
{checked}
indeterminate={checked === null ? true : false}
on:change={(e) => onCategoryChange(e, category)} />
{category}
</svelte:fragment>
<svelte:fragment slot="subtitle">
({allScopes.filter((n) => n.category === category && scopes.includes(n.scope))
.length} Scopes)
</svelte:fragment>
<FormList>
{#each allScopes.filter((s) => s.category === category) as scope}
<InputChoice
@@ -0,0 +1,46 @@
<script lang="ts">
import { Wizard } from '$lib/layout';
import { beforeNavigate, goto } from '$app/navigation';
import { wizard } from '$lib/stores/wizard';
import type { WizardStepsType } from '$lib/layout/wizard.svelte';
import Step1 from './wizard/step1.svelte';
import Step2 from './wizard/step2.svelte';
import { key } from './wizard/store';
import { sdkForConsole } from '$lib/stores/sdk';
import { page } from '$app/stores';
import { addNotification } from '$lib/stores/notifications';
async function onFinish() {
try {
const { $id } = await sdkForConsole.projects.createKey(
$page.params.project,
$key.name,
$key.scopes,
$key.expire ?? undefined
);
key.reset();
goto(`/console/project-${$page.params.project}/overview/keys/${$id}`);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
}
beforeNavigate(() => {
wizard.hide();
});
const stepsComponents: WizardStepsType = new Map();
stepsComponents.set(1, {
label: 'Details',
component: Step1
});
stepsComponents.set(2, {
label: 'Scopes',
component: Step2
});
</script>
<Wizard title="Create an API Key" steps={stepsComponents} on:finish={onFinish} on:exit={onFinish} />
@@ -0,0 +1,19 @@
<script lang="ts">
import { FormList, InputText, InputDateTime } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import { key } from './store';
</script>
<WizardStep>
<svelte:fragment slot="title">Create an API Key</svelte:fragment>
<svelte:fragment slot="subtitle">Let's create an API Key.</svelte:fragment>
<FormList>
<InputText
id="name"
label="Name"
placeholder="API Key Name"
required
bind:value={$key.name} />
<InputDateTime id="expire" label="Expiration Date" bind:value={$key.expire} />
</FormList>
</WizardStep>
@@ -0,0 +1,17 @@
<script lang="ts">
import { FormList } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import Scopes from '../scopes.svelte';
import { key } from './store';
</script>
<WizardStep>
<svelte:fragment slot="title">Add Scopes</svelte:fragment>
<svelte:fragment slot="subtitle">
Choose which permission scopes to grant your application. It is best practice to allow only
the permissions you need to meet your project goals.
</svelte:fragment>
<FormList>
<Scopes bind:scopes={$key.scopes} />
</FormList>
</WizardStep>
@@ -0,0 +1,25 @@
import { writable } from 'svelte/store';
import type { Models } from '@aw-labs/appwrite-console';
function createKeyStore() {
const { subscribe, update, set } = writable<Partial<Models.Key>>({
name: null,
expire: null,
scopes: []
});
return {
subscribe,
update,
set,
reset() {
set({
name: null,
expire: null,
scopes: []
});
}
};
}
export const key = createKeyStore();