mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-07-16 13:00:27 +00:00

macOS saves the download URL in the metadata of the downloaded file. This means that the downloaded file contains a metadata item with the JWT token of the user. If the user were to share this file with someone else, they would have access to their account using the JWT in the metadata during the validity of the JWT. The JWT has been removed from the URLs. Since the user is logged in, there is an authentication cookie set. A JWT in the URL is not necessary.
89 lines
1.9 KiB
TypeScript
89 lines
1.9 KiB
TypeScript
import { useAuthStore } from "@/stores/auth";
|
|
import { renew, logout } from "@/utils/auth";
|
|
import { baseURL } from "@/utils/constants";
|
|
import { encodePath } from "@/utils/url";
|
|
|
|
export class StatusError extends Error {
|
|
constructor(
|
|
message: any,
|
|
public status?: number
|
|
) {
|
|
super(message);
|
|
this.name = "StatusError";
|
|
}
|
|
}
|
|
|
|
export async function fetchURL(
|
|
url: string,
|
|
opts: ApiOpts,
|
|
auth = true
|
|
): Promise<Response> {
|
|
const authStore = useAuthStore();
|
|
|
|
opts = opts || {};
|
|
opts.headers = opts.headers || {};
|
|
|
|
const { headers, ...rest } = opts;
|
|
let res;
|
|
try {
|
|
res = await fetch(`${baseURL}${url}`, {
|
|
headers: {
|
|
"X-Auth": authStore.jwt,
|
|
...headers,
|
|
},
|
|
...rest,
|
|
});
|
|
} catch {
|
|
throw new StatusError("000 No connection", 0);
|
|
}
|
|
|
|
if (auth && res.headers.get("X-Renew-Token") === "true") {
|
|
await renew(authStore.jwt);
|
|
}
|
|
|
|
if (res.status < 200 || res.status > 299) {
|
|
const body = await res.text();
|
|
const error = new StatusError(
|
|
body || `${res.status} ${res.statusText}`,
|
|
res.status
|
|
);
|
|
|
|
if (auth && res.status == 401) {
|
|
logout();
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
export async function fetchJSON<T>(url: string, opts?: any): Promise<T> {
|
|
const res = await fetchURL(url, opts);
|
|
|
|
if (res.status === 200) {
|
|
return res.json() as Promise<T>;
|
|
}
|
|
|
|
throw new StatusError(`${res.status} ${res.statusText}`, res.status);
|
|
}
|
|
|
|
export function removePrefix(url: string): string {
|
|
url = url.split("/").splice(2).join("/");
|
|
|
|
if (url === "") url = "/";
|
|
if (url[0] !== "/") url = "/" + url;
|
|
return url;
|
|
}
|
|
|
|
export function createURL(endpoint: string, searchParams = {}): string {
|
|
let prefix = baseURL;
|
|
if (!prefix.endsWith("/")) {
|
|
prefix = prefix + "/";
|
|
}
|
|
const url = new URL(prefix + encodePath(endpoint), origin);
|
|
url.search = new URLSearchParams(searchParams).toString();
|
|
|
|
return url.toString();
|
|
}
|