web: add support for custom api keys & improve turnstile states
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user