improved code around cookie handling, added to docs

This commit is contained in:
Wyatt Johnson
2017-08-10 10:46:11 +10:00
parent b9243938bd
commit 6d70a7b20e
2 changed files with 20 additions and 6 deletions
+8
View File
@@ -123,6 +123,14 @@ will be used:
}
```
When our passport middleware checks for JWT tokens, it searches in the following
order:
1. Custom cookies named from the list in `TALK_JWT_COOKIE_NAMES`.
2. Default cookies named `TALK_JWT_COOKIE_NAME` then `TALK_JWT_SIGNING_COOKIE_NAME`.
3. Query parameter `?access_token={TOKEN}`.
4. Header: `Authorization: Bearer {TOKEN}`.
### Email
- `TALK_SMTP_EMAIL` (*required for email*) - the address to send emails from
+12 -6
View File
@@ -210,14 +210,20 @@ const CheckBlacklisted = async (jwt) => {
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
let cookieExtractor = (cookieName) => (req) => {
let token = null;
let cookieExtractor = (req) => {
if (req && req.cookies) {
token = req.cookies[cookieName];
// Walk over all the cookie names in JWT_COOKIE_NAMES.
for (const cookieName of JWT_COOKIE_NAMES) {
// Check to see if that cookie is set.
if (cookieName in req.cookies && req.cookies[cookieName] !== null && req.cookies[cookieName].length > 0) {
return req.cookies[cookieName];
}
}
}
return token;
return null;
};
// Override the JwtVerifier method on the JwtStrategy so we can pack the
@@ -238,7 +244,7 @@ passport.use(new JwtStrategy({
// Prepare the extractor from the header.
jwtFromRequest: ExtractJwt.fromExtractors([
...JWT_COOKIE_NAMES.map(cookieExtractor),
cookieExtractor,
ExtractJwt.fromUrlQueryParameter('access_token'),
ExtractJwt.fromAuthHeaderWithScheme('Bearer')
]),