From 98ce2a9855d43d4515a460ec6c36b0f834f23160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Michaud?= Date: Sat, 5 Jan 2019 19:13:44 +0100 Subject: [PATCH] Require authentication again when first attempt failed. (#4) * Require authentication again when first attempt failed. In firefox, upon bad authentication (with wrong credentials), MesosTerm was then always returning a 401 error with a page containing the message "Unauthorized". From this time on, ao authentication request was pushed to the user anymore and therefore there was no way for him/her to fix the credentials. The only workaround was to clear the browser cache. * Remove bad tsc dependency. --- package.json | 1 - src/authentication.ts | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 62ccead2..3458baa8 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "selenium-webdriver": "^4.0.0-alpha.1", "sinon": "^5.0.7", "ts-node": "^6.0.3", - "tsc": "^1.20150623.0", "tslint": "^5.10.0", "tslint-eslint-rules": "^5.3.1", "typescript": "^2.8.3" diff --git a/src/authentication.ts b/src/authentication.ts index bc6ed6a5..ada5d5c2 100644 --- a/src/authentication.ts +++ b/src/authentication.ts @@ -45,7 +45,23 @@ export default function(app: Express.Application) { app.use(passport.initialize()); app.use(protectWithBasicAuth); - app.use(passport.authenticate('ldapauth', {session: true})); + app.use((req, res, next) => { + passport.authenticate('ldapauth', {session: true}, (err: Error, user: any, info: any) => { + if (err) { + return next(err); + } + + if (!user) { + res.status(401); + res.header('WWW-Authenticate', 'Basic realm="must be authenticated"'); + res.send('Unauthenticated'); + return; + } + + req.user = user; + next(); + })(req, res, next); + }); passport.use(new LdapStrategy(options)); }