-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from nhsuk/replace-basic-auth-with-a-password…
…-page Replace basic authentication with a password page and a cookie.
- Loading branch information
Showing
8 changed files
with
206 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{% extends 'layout.html' %} | ||
|
||
{% block pageTitle %} | ||
{% if error == "wrong-password" %} | ||
Error: | ||
{% endif %} | ||
Enter password - NHS prototype kit | ||
{% endblock %} | ||
|
||
{% block content %} | ||
|
||
<div class="nhsuk-grid-row"> | ||
<div class="nhsuk-grid-column-two-thirds"> | ||
<form method="post" action="/prototype-admin/password"> | ||
|
||
{% if error == "wrong-password" %} | ||
{{ errorSummary({ | ||
titleText: "There is a problem", | ||
errorList: [ | ||
{ | ||
text: "The password is not correct", | ||
href: "#password" | ||
} | ||
] | ||
})}} | ||
{% endif %} | ||
|
||
<h1 class="nhsuk-heading-l"> | ||
This is a prototype used for research | ||
</h1> | ||
|
||
<p> | ||
It is not a real service. You should only continue if you have been invited to test this prototype. | ||
</p> | ||
|
||
{{ input({ | ||
classes: "nhsuk-input--width-10", | ||
name: "password", | ||
id: "password", | ||
type: "password", | ||
errorMessage: { | ||
text: "The password is not correct" | ||
} if error == "wrong-password", | ||
label:{ | ||
text: "Password" | ||
} | ||
}) }} | ||
|
||
<input type="hidden" name="returnURL" value="{{returnURL}}"> | ||
|
||
{{ button({ | ||
text: "Continue" | ||
}) }} | ||
|
||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,51 @@ | ||
/** | ||
* Simple basic auth middleware for use with Express 4.x. | ||
* | ||
* Based on template found at: http://www.danielstjules.com/2014/08/03/basic-auth-with-express-4/ | ||
* | ||
* @example | ||
* const authentication = required('authentication'); | ||
* app.use(authentication); | ||
* | ||
* @param {string} req Express Request object | ||
* @param {string} res Express Response object | ||
* @returns {function} Express 4 middleware requiring the given credentials | ||
*/ | ||
// External dependencies | ||
const basicAuth = require('basic-auth'); | ||
const url = require('url'); | ||
const { encryptPassword } = require('../lib/utils'); | ||
|
||
module.exports = function (req, res, next) { /* eslint-disable-line func-names,consistent-return */ | ||
// Set configuration variables | ||
const env = (process.env.NODE_ENV || 'development').toLowerCase(); | ||
const username = process.env.PROTOTYPE_USERNAME; | ||
const password = process.env.PROTOTYPE_PASSWORD; | ||
const allowedPathsWhenUnauthenticated = [ | ||
'/prototype-admin/password', | ||
'/css/main.css', | ||
'/nhsuk-frontend/nhsuk.min.js', | ||
'/js/auto-store-data.js', | ||
'/js/jquery-3.5.1.min.js', | ||
'/js/main.js', | ||
]; | ||
|
||
if (env === 'production' || env === 'staging') { | ||
if (!username || !password) { | ||
return res.send('<p>Username or password not set in environment variables.</p>'); | ||
} | ||
const encryptedPassword = encryptPassword(process.env.PROTOTYPE_PASSWORD); | ||
const nodeEnv = process.env.NODE_ENV || 'development'; | ||
|
||
const user = basicAuth(req); | ||
// Redirect the user to the password page, with | ||
// the current page path set as the returnURL in a query | ||
// string so the user can be redirected back after successfully | ||
// entering a password | ||
function sendUserToPasswordPage(req, res) { | ||
const returnURL = url.format({ | ||
pathname: req.path, | ||
query: req.query, | ||
}); | ||
const passwordPageURL = url.format({ | ||
pathname: '/prototype-admin/password', | ||
query: { returnURL }, | ||
}); | ||
res.redirect(passwordPageURL); | ||
} | ||
|
||
if (!user || user.name !== username || user.pass !== password) { | ||
res.set('WWW-Authenticate', 'Basic realm=Authorization Required'); | ||
return res.sendStatus(401); | ||
} | ||
// Give the user some instructions on how to set a password | ||
function showNoPasswordError(res) { | ||
return res.send('<h1>Error:</h1><p>Password not set. <a href="https://nhsuk-prototype-kit.azurewebsites.net/docs/how-tos/publish-your-prototype-online">See guidance for setting a password</a>.</p>'); | ||
} | ||
|
||
function authentication(req, res, next) { | ||
if (nodeEnv !== 'production') { | ||
next(); | ||
} else if (!process.env.PROTOTYPE_PASSWORD) { | ||
showNoPasswordError(res); | ||
} else if (allowedPathsWhenUnauthenticated.includes(req.path)) { | ||
next(); | ||
} else if (req.cookies.authentication === encryptedPassword) { | ||
next(); | ||
} else { | ||
sendUserToPasswordPage(req, res); | ||
} | ||
next(); | ||
}; | ||
} | ||
|
||
module.exports = authentication; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const express = require('express'); | ||
|
||
const router = express.Router(); | ||
|
||
const password = process.env.PROTOTYPE_PASSWORD; | ||
|
||
const { encryptPassword } = require('../lib/utils'); | ||
|
||
router.get('/password', (req, res) => { | ||
const returnURL = req.query.returnURL || '/'; | ||
const { error } = req.query; | ||
res.render('password', { | ||
returnURL, | ||
error, | ||
}); | ||
}); | ||
|
||
// Check authentication password | ||
router.post('/password', (req, res) => { | ||
const submittedPassword = req.body.password; | ||
const { returnURL } = req.body; | ||
|
||
if (submittedPassword === password) { | ||
// see lib/middleware/authentication.js for explanation | ||
res.cookie('authentication', encryptPassword(password), { | ||
maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days | ||
sameSite: 'None', // Allows GET and POST requests from other domains | ||
httpOnly: true, | ||
secure: true, | ||
}); | ||
res.redirect(returnURL); | ||
} else { | ||
res.redirect(`/prototype-admin/password?error=wrong-password&returnURL=${encodeURIComponent(returnURL)}`); | ||
} | ||
}); | ||
|
||
module.exports = router; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters