Code fore v2 API is not working but V1 code is.

This commit is contained in:
Benoit 2022-12-07 10:01:26 +01:00
parent ea8e297e84
commit bcc2864ee2

View file

@ -5,37 +5,61 @@ import { httpProxy } from "utils/proxy/http";
import getServiceWidget from "utils/config/service-helpers"; import getServiceWidget from "utils/config/service-helpers";
import createLogger from "utils/logger"; import createLogger from "utils/logger";
import widgets from "widgets/widgets"; import widgets from "widgets/widgets";
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
const proxyName = "omadaProxyHandler"; const proxyName = "omadaProxyHandler";
const tokenCacheKey = `${proxyName}__token`; const tokenCacheKey = `${proxyName}__token`;
const logger = createLogger(proxyName); const logger = createLogger(proxyName);
async function login(loginUrl, username, password) { async function login(loginUrl, username, password, legacy) {
const authResponse = await httpProxy(loginUrl,
{ if (legacy) {
method: "POST", console.log("Legacy");
body: JSON.stringify({ "method": "login", const authResponse = await httpProxy(loginUrl,
"params": { {
"name": username, method: "POST",
"password": password body: JSON.stringify({ "method": "login",
} }), "params": {
headers: { "name": username,
"Content-Type": "application/json", "password": password
}, } }),
} headers: {
"Content-Type": "application/json",
},
}
); );
const status = authResponse[0]; let data;
const data = JSON.parse(authResponse[2]); const status = authResponse[0];
const {token} = data.result; data = JSON.parse(authResponse[2]);
try { const {token} = data.result;
if (status === 200) { try {
cache.put(tokenCacheKey, token); // expiration -5 minutes if (status === 200) {
cache.put(tokenCacheKey, token); // expiration -5 minutes
}
} catch (e) {
logger.error(`Error ${status} logging into Omada`, authResponse[2]);
} }
} catch (e) { return [status, token ?? data];
logger.error(`Error ${status} logging into Omada`, authResponse[2]); } else {
setCookieHeader(loginUrl, );
const authResponse = await httpProxy(loginUrl,
{
method: "POST",
body: JSON.stringify({
"name": username,
"password": password
}),
headers: {
"Content-Type": "application/json",
},
}
);
let data;
const status = authResponse[0];
console.log("Status: ", status);
} }
return [status, token ?? data]; return [null, null];
} }
@ -50,16 +74,14 @@ export default async function omadaProxyHandler(req, res) {
} }
if (widget) { if (widget) {
const loginUrl = `${widget.url}/api/user/login?ajax`;
let status;
let contentType;
let data;
let result;
let token;
if (widget.legacy) { if (widget.legacy) {
[status, token] = await login(loginUrl, widget.username, widget.password); const loginUrl = `${widget.url}/api/user/login?ajax`;
let status;
let contentType;
let data;
let result;
let token;
[status, token] = await login(loginUrl, widget.username, widget.password, widget.legacy);
if (status !== 200) { if (status !== 200) {
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`); logger.debug(`HTTTP ${status} logging into Omada api: ${token}`);
return res.status(status).send(token); return res.status(status).send(token);
@ -136,7 +158,48 @@ export default async function omadaProxyHandler(req, res) {
return res.send(data.result); return res.send(data.result);
} else { } else {
// Working on it but I can't test it // Working on it but I can't test it
logger.debug(`unsupported for now but I'm working on it`);
const controlleridurl = `${widget.url}/api/info`;
let cidstatus, cidcontentType, cidresult;
[cidstatus, cidcontentType, cidresult] = await httpProxy(controlleridurl, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const cid = JSON.parse(cidresult).result.omadacId;
const loginUrl = `${widget.url}/${cid}/login`;
let status;
let token;
const params = {
method: "POST",
body: JSON.stringify({
"name": widget.username,
"password": widget.password
}),
headers: {"Content-Type": "application/json"} };
// setCookieHeader(loginUrl, params);
const authResponse = await httpProxy(loginUrl,
params,
);
status = authResponse[0];
const data = JSON.parse(authResponse[2]);
console.log("Data: ", data);
// addCookieToJar(loginUrl, authResponse[3]);
// setCookieHeader(loginUrl, params);
console.log("Status: ", status);
console.log("Token: ", token);
if (status !== 200) {
logger.debug(`HTTTP ${status} logging into Omada api: ${token}`);
return res.status(status).send(token);
}
} }
} }
} }