Enhancement: support pyload API key, fix error message (#6558)
Some checks are pending
Docker CI / Docker Build & Push (push) Waiting to run
Lint / Linting Checks (push) Waiting to run
Release Drafter / Update Release Draft (push) Waiting to run
Release Drafter / Auto Label PR (push) Waiting to run
Tests / vitest (1) (push) Waiting to run
Tests / vitest (2) (push) Waiting to run
Tests / vitest (3) (push) Waiting to run
Tests / vitest (4) (push) Waiting to run

This commit is contained in:
shamoon
2026-04-12 11:00:54 -07:00
committed by GitHub
parent 1accddf29a
commit a7bab17f97
3 changed files with 57 additions and 9 deletions

View File

@@ -13,4 +13,5 @@ widget:
url: http://pyload.host.or.ip:port
username: username
password: password # only needed if set
key: pyloadapikey # only needed if set, takes precedence over username/password
```

View File

@@ -45,17 +45,20 @@ async function fetchFromPyloadAPI(url, sessionId, params, service) {
return [status, returnData, responseHeaders];
}
async function fetchFromPyloadAPIBasic(url, params, username, password) {
async function fetchFromPyloadAPIWithCredentials(url, params, username, password, key) {
const parsedUrl = new URL(url);
const isGetRequest = !params || Object.keys(params).length === 0;
const options = {
method: isGetRequest ? "GET" : "POST",
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`,
},
};
if (key) {
options.headers = { "X-API-Key": key };
} else {
options.headers = { Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}` };
}
if (isGetRequest) {
if (params) {
Object.keys(params).forEach((key) => parsedUrl.searchParams.append(key, params[key]));
@@ -106,10 +109,16 @@ export default async function pyloadProxyHandler(req, res, map = {}) {
const url = new URL(formatApiCall(apiTemplate, { endpoint, ...widget }));
const ngUrl = ngEndpoint ? new URL(formatApiCall(apiTemplate, { endpoint: ngEndpoint, ...widget })) : url;
const loginUrl = `${widget.url}/api/login`;
const hasCredentials = widget.username && widget.password;
const hasCredentials = widget.key || (widget.username && widget.password);
if (hasCredentials) {
const [status, data] = await fetchFromPyloadAPIBasic(ngUrl, null, widget.username, widget.password);
const [status, data] = await fetchFromPyloadAPIWithCredentials(
ngUrl,
null,
widget.username,
widget.password,
widget.key,
);
if (status === 200 && !data?.error) {
cache.put(`${isNgCacheKey}.${service}`, true);
@@ -117,9 +126,7 @@ export default async function pyloadProxyHandler(req, res, map = {}) {
}
if (status === 401) {
return res
.status(status)
.send({ error: { message: "Invalid credentials communicating with Pyload API", data } });
return res.status(status).send({ error: "Invalid credentials communicating with Pyload API", data });
}
}

View File

@@ -75,6 +75,46 @@ describe("widgets/pyload/proxy", () => {
expect(res.body).toEqual({ ok: true });
});
it("uses api key auth and returns data", async () => {
getServiceWidget.mockResolvedValue({
type: "pyload",
url: "http://pyload",
key: "apikey",
});
httpProxy.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ ok: true })), {}]);
const req = { query: { group: "g", service: "svc", endpoint: "status", index: "0" } };
const res = createMockRes();
await pyloadProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(1);
expect(httpProxy.mock.calls[0][1].headers["X-API-Key"]).toBe("apikey");
expect(cache.put).toHaveBeenCalledWith("pyloadProxyHandler__isNg.svc", true);
expect(res.body).toEqual({ ok: true });
});
it("returns error if login fails", async () => {
getServiceWidget.mockResolvedValue({
type: "pyload",
url: "http://pyload",
username: "u",
password: "p",
});
httpProxy.mockResolvedValueOnce([401, "application/json", Buffer.from(JSON.stringify({ error: "bad" })), {}]);
const req = { query: { group: "g", service: "svc", endpoint: "status", index: "0" } };
const res = createMockRes();
await pyloadProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(1);
expect(res.statusCode).toBe(401);
expect(res.body).toMatchObject({ error: "Invalid credentials communicating with Pyload API" });
});
it("retries after 403 by clearing session and logging in again", async () => {
getServiceWidget.mockResolvedValue({
type: "pyload",