NodeSSPI icon indicating copy to clipboard operation
NodeSSPI copied to clipboard

how to use this without Express

Open emahuni opened this issue 5 years ago • 5 comments

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)

emahuni avatar Feb 03 '20 02:02 emahuni

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));

jlguenego avatar Mar 27 '20 15:03 jlguenego

Thank you I'll give it a try.

emahuni avatar Mar 27 '20 18:03 emahuni

Thank you I'll give it a try.

@emahuni did the proposed solution work in your environment?

LukasK07 avatar Jun 30 '20 06:06 LukasK07

@LukasK07 @jlguenego Thank you it worked

Can you please put this in the examples.

emahuni avatar Oct 30 '20 16:10 emahuni

Reopened to propose example in readme from PR

emahuni avatar Oct 31 '20 01:10 emahuni