mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-28 23:11:17 -07:00
Enhancement: support pyload API key, fix error message (#6558)
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
This commit is contained in:
@@ -13,4 +13,5 @@ widget:
|
|||||||
url: http://pyload.host.or.ip:port
|
url: http://pyload.host.or.ip:port
|
||||||
username: username
|
username: username
|
||||||
password: password # only needed if set
|
password: password # only needed if set
|
||||||
|
key: pyloadapikey # only needed if set, takes precedence over username/password
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -45,17 +45,20 @@ async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
|||||||
return [status, returnData, responseHeaders];
|
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 parsedUrl = new URL(url);
|
||||||
const isGetRequest = !params || Object.keys(params).length === 0;
|
const isGetRequest = !params || Object.keys(params).length === 0;
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
method: isGetRequest ? "GET" : "POST",
|
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 (isGetRequest) {
|
||||||
if (params) {
|
if (params) {
|
||||||
Object.keys(params).forEach((key) => parsedUrl.searchParams.append(key, params[key]));
|
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 url = new URL(formatApiCall(apiTemplate, { endpoint, ...widget }));
|
||||||
const ngUrl = ngEndpoint ? new URL(formatApiCall(apiTemplate, { endpoint: ngEndpoint, ...widget })) : url;
|
const ngUrl = ngEndpoint ? new URL(formatApiCall(apiTemplate, { endpoint: ngEndpoint, ...widget })) : url;
|
||||||
const loginUrl = `${widget.url}/api/login`;
|
const loginUrl = `${widget.url}/api/login`;
|
||||||
const hasCredentials = widget.username && widget.password;
|
const hasCredentials = widget.key || (widget.username && widget.password);
|
||||||
|
|
||||||
if (hasCredentials) {
|
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) {
|
if (status === 200 && !data?.error) {
|
||||||
cache.put(`${isNgCacheKey}.${service}`, true);
|
cache.put(`${isNgCacheKey}.${service}`, true);
|
||||||
@@ -117,9 +126,7 @@ export default async function pyloadProxyHandler(req, res, map = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (status === 401) {
|
if (status === 401) {
|
||||||
return res
|
return res.status(status).send({ error: "Invalid credentials communicating with Pyload API", data });
|
||||||
.status(status)
|
|
||||||
.send({ error: { message: "Invalid credentials communicating with Pyload API", data } });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,46 @@ describe("widgets/pyload/proxy", () => {
|
|||||||
expect(res.body).toEqual({ ok: true });
|
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 () => {
|
it("retries after 403 by clearing session and logging in again", async () => {
|
||||||
getServiceWidget.mockResolvedValue({
|
getServiceWidget.mockResolvedValue({
|
||||||
type: "pyload",
|
type: "pyload",
|
||||||
|
|||||||
Reference in New Issue
Block a user