Update pep.js to handle log levels

This commit is contained in:
Antonio Calanducci 2024-05-30 18:20:00 +02:00
parent 1c033b0443
commit a33bce76e3
1 changed files with 27 additions and 21 deletions

View File

@ -7,7 +7,7 @@ function log(c, s) {
}
var _debug = defaultExport["config"]["debug"]
var _debug = true
// var _debug = true
njs.dump(_debug);
@ -17,6 +17,10 @@ function debug(c, s) {
}
}
function error(c, s) {
c.request.error(s)
}
function enforce(r) {
var context = {
@ -37,61 +41,63 @@ function enforce_legacy(r) {
request: r,
config: defaultExport["config"]
}
debug(context, JSON.stringify(context.config["accounting"], null, 2));
log(context, "Inside enforce_legacy for " + r.method + " @ " + r.headersIn.host + "/" + r.uri)
debug(context, "Accounting config:\n" + JSON.stringify(context.config["accounting"], null, 2));
var allowedcontexts = context.config["accounting"]["scopes"]
log(context, "Inside NJS enforce for " + r.method + " @ " + r.headersIn.host + "/" + r.uri)
debug(context, "debug is " + JSON.stringify(defaultExport["config"]))
debug(context, "Allowed Contexts: " + JSON.stringify(allowedcontexts));
debug(context, "PEP config:\n" + JSON.stringify(defaultExport["config"], null, 2))
const token = getGCubeToken(context)
debug(context, JSON.stringify(context, null, 2))
debug(context, "gcube token" + token)
if (token != null) {
debug(context, "[PEP] token is " + token)
debug(context, "gcube-tone is " + token)
exportVariable(context, "auth_token", token)
context.request.subrequest("/_gcube_user_info")
.then(reply => {
if (reply.status === 200) {
debug(context, "[Social Service] got response " + reply.responseText)
debug(context, "[Social Service] response " + reply.responseText)
// var response = JSON.parse(reply.responseBody);
var response = JSON.parse(reply.responseText);
if (allowedcontexts.indexOf(response.result.context) === -1) {
debug(context, "[PEP] Unathorized context " + response.result.context)
error(context, "[PEP] Unathorized context " + response.result.context)
throw new Error("Unauthorized")
}
log(context, "Authorization successful")
return response
} else {
log(context, "[Social Service] failed " + reply.status + ":" + reply.responseText)
error(context, "[Social Service] failed " + reply.status + ":" + reply.responseText)
throw new Error("Unauthorized")
}
}).then(userinfo => {
debug(context, "[Social Service] username is " + userinfo.result.username)
debug(context, njs.dump(context));
// debug(context, "Context again:\n" + njs.dump(context));
context.userinfo = userinfo
context.record = buildAccountingRecord_legacy(context)
return context.request.subrequest("/_backend", { method: context.request.method, args: JSON.stringify(context.request.args), headers: context.request.headersIn })
}).then(reply => {
debug(context, reply.responseText);
debug(context, "[SHINYPROXY] response status: " + reply.status)
debug(context, "response from backend\n" + reply.responseText);
debug(context, "response status: " + reply.status)
copyHeaders(context, reply.headersOut, r.headersOut)
closeAccountingRecord_legacy(context.record, (reply.status === 200 || reply.status === 201 || reply.status === 204))
context.request.subrequest("/_accounting_legacy", { detached: true, body: JSON.stringify([context.record]) })
// r.return(reply.status, reply.responseBody)
log(context, "Accounting record sent:\n" + JSON.stringify(context.record, null, 2))
debug(context, "Redirect URI: " + reply.headersOut["Location"])
if(reply.status === 301 || reply.status === 302){
debug(context, "sto per fare la redirect");
if (reply.status === 301 || reply.status === 302) {
debug(context, "Redirecting...");
r.return(reply.status, reply.headersOut["Location"])
}else{
} else {
r.return(reply.status, reply.responseText)
}
}).catch(e => { log(context, "Error .... " + njs.dump(e)); context.request.return(e.message === "Unauthorized" ? 403 : 500) })
}
}).catch(e => { error(context, "Error .... " + njs.dump(e)); context.request.return(e.message === "Unauthorized" ? 403 : 500) })
return
}
r.return(401, "Authorization required")
}
function copyHeaders(context, hin, hout){
function copyHeaders(context, hin, hout) {
for (var h in hin) {
if(h !== "Location") hout[h] = hin[h];
if (h !== "Location") hout[h] = hin[h];
}
}