web: add support for custom api keys & improve turnstile states

This commit is contained in:
wukko
2024-11-23 19:13:23 +06:00
parent 7c7cefe89b
commit 601597eb15
6 changed files with 101 additions and 40 deletions

View File

@@ -5,12 +5,45 @@ import lazySettingGetter from "$lib/settings/lazy-get";
import { getSession } from "$lib/api/session";
import { currentApiURL } from "$lib/api/api-url";
import { turnstileSolved } from "$lib/state/turnstile";
import { turnstileEnabled, turnstileSolved } from "$lib/state/turnstile";
import { cachedInfo, getServerInfo } from "$lib/api/server-info";
import type { Optional } from "$lib/types/generic";
import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api";
const getAuthorization = async () => {
const processing = get(settings).processing;
if (get(turnstileEnabled)) {
if (!get(turnstileSolved)) {
return {
status: "error",
error: {
code: "error.captcha_ongoing"
}
} as CobaltErrorResponse;
}
const session = await getSession();
if (session) {
if ("error" in session) {
if (session.error.code !== "error.api.auth.not_configured") {
return session;
}
} else {
return `Bearer ${session.token}`;
}
}
}
if (processing.enableCustomApiKey && processing.customApiKey.length > 0) {
return `Api-Key ${processing.customApiKey}`;
}
return false;
}
const request = async (url: string) => {
const getSetting = lazySettingGetter(get(settings));
@@ -49,31 +82,14 @@ const request = async (url: string) => {
} as CobaltErrorResponse;
}
if (getCachedInfo?.info?.cobalt?.turnstileSitekey && !get(turnstileSolved)) {
return {
status: "error",
error: {
code: "error.captcha_ongoing"
}
} as CobaltErrorResponse;
}
const api = currentApiURL();
const session = getCachedInfo?.info?.cobalt?.turnstileSitekey
? await getSession() : undefined;
const authorization = await getAuthorization();
let extraHeaders = {}
if (session) {
if ("error" in session) {
if (session.error.code !== "error.api.auth.not_configured") {
return session;
}
} else {
extraHeaders = {
"Authorization": `Bearer ${session.token}`,
};
if (authorization) {
extraHeaders = {
"Authorization": authorization
}
}

View File

@@ -1,4 +1,17 @@
import { writable } from "svelte/store";
import settings from "$lib/state/settings";
import { cachedInfo } from "$lib/api/server-info";
import { derived, writable } from "svelte/store";
export const turnstileSolved = writable(false);
export const turnstileCreated = writable(false);
export const turnstileEnabled = derived(
[settings, cachedInfo],
([$settings, $cachedInfo]) => {
return !!$cachedInfo?.info?.cobalt?.turnstileSitekey &&
!(
$settings.processing.enableCustomApiKey &&
$settings.processing.customApiKey.length > 0
)
}
)