NodeSSPI
NodeSSPI copied to clipboard
how to use this without Express
I know it is written that you can use this without Express. I tried doing it with Koa and it doesn't seem to work. Cay you also give an example in Koa and without any lib (just using Node req and res)
I guess this code may suit you...
To use native node middleware, the hack is to use ctx.respond = false
.
"use strict";
const nodeSSPI = require("node-sspi");
const Koa = require("koa");
const app = new Koa();
const nodeSSPIObj = new nodeSSPI({
retrieveGroups: true
});
app.use(async (ctx, next) => {
ctx.respond = false;
console.log("start");
nodeSSPIObj.authenticate(ctx.req, ctx.res, async err => {
console.log("authenticate finished", ctx.res.finished);
if (!ctx.res.finished) {
await next();
}
});
});
app.use(async ctx => {
console.log("second part start");
let out =
"Hello " +
ctx.req.connection.user +
"! Your sid is " +
ctx.req.connection.userSid +
" and you belong to following groups:<br/><ul>";
if (ctx.req.connection.userGroups) {
for (let i in ctx.req.connection.userGroups) {
out += "<li>" + ctx.req.connection.userGroups[i] + "</li><br/>\n";
}
}
out += "</ul>";
console.log("about to send");
ctx.res.setHeader('Content-Type', 'text/html');
ctx.res.setHeader('Content-Length', out.length);
ctx.res.write(out)
ctx.res.statusCode = 200;
ctx.res.end();
});
const port = 3000;
app.listen(port, () => console.log("Koa server listening on port %d", port));
Thank you I'll give it a try.
Thank you I'll give it a try.
@emahuni did the proposed solution work in your environment?
@LukasK07 @jlguenego Thank you it worked
Can you please put this in the examples.
Reopened to propose example in readme from PR