Skip to content

Commit

Permalink
Merge 10bde79 into c5713bc
Browse files Browse the repository at this point in the history
  • Loading branch information
nwalters512 committed Apr 5, 2022
2 parents c5713bc + 10bde79 commit 6342599
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/dev-guide.md
Expand Up @@ -811,7 +811,7 @@ function foo(p, callback) {
1. We first redirect to a remote authentication service (either Shibboleth or Google OAuth2 servers). For Shibboleth this happens by the “Login to PL” button linking to `/pl/shibcallback` for which Apache handles the Shibboleth redirections. For Google the “Login to PL” button links to `/pl/auth2login` which sets up the authentication data and redirects to Google.
2. The remote authentication service redirects back to `/pl/shibcallback` (for Shibboleth) or `/pl/auth2callback` (for Google). These endpoints confirm authentication, create the user in the `users` table if necessary, set a signed `pl_authn` cookie in the browser with the authenticated `user_id`, and then redirect to the main PL homepage.
2. The remote authentication service redirects back to `/pl/shibcallback` (for Shibboleth) or `/pl/auth2callback` (for Google). These endpoints confirm authentication, create the user in the `users` table if necessary, set a signed `pl_authn` cookie in the browser with the authenticated `user_id`, and then redirect to the main PL homepage. This cookie is set with the `HttpOnly` attribute, which prevents client-side JavaScript from reading the cookie.
3. Every other page authenticates using the signed browser `pl_authn` cookie. This is read by [`middlewares/authn.js`](https://github.com/PrairieLearn/PrairieLearn/blob/master/middlewares/authn.js) which checks the signature and then loads the user data from the DB using the `user_id`, storing it as `res.locals.authn_user`.
Expand Down
2 changes: 2 additions & 0 deletions middlewares/authn.js
Expand Up @@ -175,6 +175,8 @@ module.exports = function (req, res, next) {
var pl_authn = csrf.generateToken(tokenData, config.secretKey);
res.cookie('pl_authn', pl_authn, {
maxAge: config.authnCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});

next();
Expand Down
2 changes: 2 additions & 0 deletions middlewares/authzWorkspaceCookieSet.js
Expand Up @@ -16,6 +16,8 @@ module.exports = (req, res, next) => {
const cookieData = csrf.generateToken(tokenData, config.secretKey);
res.cookie(cookieName, cookieData, {
maxAge: config.workspaceAuthzCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});
next();
};
2 changes: 2 additions & 0 deletions pages/authCallbackAzure/authCallbackAzure.js
Expand Up @@ -34,6 +34,8 @@ router.all('/', function (req, res, next) {
var pl_authn = csrf.generateToken(tokenData, config.secretKey);
res.cookie('pl_authn', pl_authn, {
maxAge: config.authnCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});
var redirUrl = res.locals.homeUrl;
if ('preAuthUrl' in req.cookies) {
Expand Down
2 changes: 2 additions & 0 deletions pages/authCallbackLti/authCallbackLti.js
Expand Up @@ -130,6 +130,8 @@ router.post('/', function (req, res, next) {
var pl_authn = csrf.generateToken(tokenData, config.secretKey);
res.cookie('pl_authn', pl_authn, {
maxAge: config.authnCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});

const params = {
Expand Down
2 changes: 2 additions & 0 deletions pages/authCallbackOAuth2/authCallbackOAuth2.js
Expand Up @@ -62,6 +62,8 @@ router.get('/', function (req, res, next) {
const pl_authn = csrf.generateToken(tokenData, config.secretKey);
res.cookie('pl_authn', pl_authn, {
maxAge: config.authnCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});
let redirUrl = res.locals.homeUrl;
if ('preAuthUrl' in req.cookies) {
Expand Down
2 changes: 2 additions & 0 deletions pages/authCallbackShib/authCallbackShib.js
Expand Up @@ -31,6 +31,8 @@ router.get('/:action?/:target(*)?', function (req, res, next) {
var pl_authn = csrf.generateToken(tokenData, config.secretKey);
res.cookie('pl_authn', pl_authn, {
maxAge: config.authnCookieMaxAgeMilliseconds,
httpOnly: true,
secure: true,
});
if (req.params.action === 'redirect') return res.redirect('/' + req.params.target);
var redirUrl = res.locals.homeUrl;
Expand Down
7 changes: 2 additions & 5 deletions pages/authPassword/authPassword.js
Expand Up @@ -15,11 +15,8 @@ router.post('/', function (req, res) {
var pl_pw_origUrl = req.cookies.pl_pw_origUrl;
var maxAge = 1000 * 60 * 60 * 12; // 12 hours

var pwCookie = csrf.generateToken(
{ password: req.body.password, maxAge: maxAge },
config.secretKey
);
res.cookie('pl_assessmentpw', pwCookie, { maxAge: maxAge });
var pwCookie = csrf.generateToken({ password: req.body.password, maxAge }, config.secretKey);
res.cookie('pl_assessmentpw', pwCookie, { maxAge, httpOnly: true, secure: true });
res.clearCookie('pl_pw_origUrl');
return res.redirect(pl_pw_origUrl);
}
Expand Down

0 comments on commit 6342599

Please sign in to comment.