Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use this without Express #80

Open
emahuni opened this issue Feb 3, 2020 · 5 comments · May be fixed by #86
Open

how to use this without Express #80

emahuni opened this issue Feb 3, 2020 · 5 comments · May be fixed by #86

Comments

@emahuni
Copy link

emahuni commented Feb 3, 2020

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)

@jlguenego
Copy link

jlguenego commented Mar 27, 2020

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

@emahuni
Copy link
Author

emahuni commented Mar 27, 2020

Thank you I'll give it a try.

@LukasK07
Copy link

Thank you I'll give it a try.

@emahuni did the proposed solution work in your environment?

@emahuni
Copy link
Author

emahuni commented Oct 30, 2020

@LukasK07 @jlguenego Thank you it worked

Can you please put this in the examples.

@emahuni emahuni closed this as completed Oct 30, 2020
@emahuni emahuni mentioned this issue Oct 31, 2020
@emahuni emahuni reopened this Oct 31, 2020
@emahuni
Copy link
Author

emahuni commented Oct 31, 2020

Reopened to propose example in readme from PR

@emahuni emahuni linked a pull request Oct 31, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants