Code fore v2 API is not working but V1 code is.
This commit is contained in:
parent
ea8e297e84
commit
bcc2864ee2
1 changed files with 95 additions and 32 deletions
|
@ -5,13 +5,17 @@ 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) {
|
||||||
|
|
||||||
|
if (legacy) {
|
||||||
|
console.log("Legacy");
|
||||||
const authResponse = await httpProxy(loginUrl,
|
const authResponse = await httpProxy(loginUrl,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -25,8 +29,9 @@ async function login(loginUrl, username, password) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
let data;
|
||||||
const status = authResponse[0];
|
const status = authResponse[0];
|
||||||
const data = JSON.parse(authResponse[2]);
|
data = JSON.parse(authResponse[2]);
|
||||||
const {token} = data.result;
|
const {token} = data.result;
|
||||||
try {
|
try {
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
|
@ -36,6 +41,25 @@ async function login(loginUrl, username, password) {
|
||||||
logger.error(`Error ${status} logging into Omada`, authResponse[2]);
|
logger.error(`Error ${status} logging into Omada`, authResponse[2]);
|
||||||
}
|
}
|
||||||
return [status, token ?? data];
|
return [status, token ?? data];
|
||||||
|
} 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 [null, null];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,16 +74,14 @@ export default async function omadaProxyHandler(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (widget) {
|
if (widget) {
|
||||||
|
if (widget.legacy) {
|
||||||
const loginUrl = `${widget.url}/api/user/login?ajax`;
|
const loginUrl = `${widget.url}/api/user/login?ajax`;
|
||||||
|
|
||||||
let status;
|
let status;
|
||||||
let contentType;
|
let contentType;
|
||||||
let data;
|
let data;
|
||||||
let result;
|
let result;
|
||||||
let token;
|
let token;
|
||||||
if (widget.legacy) {
|
[status, token] = await login(loginUrl, widget.username, widget.password, widget.legacy);
|
||||||
[status, token] = await login(loginUrl, widget.username, widget.password);
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue