freeCodeCamp/api-server/server/middlewares/request-authorization.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

import loopback from 'loopback';
2019-02-20 18:18:50 +00:00
import { isEmpty } from 'lodash';
import {
getAccessTokenFromRequest,
errorTypes,
authHeaderNS
} from '../utils/getSetAccessToken';
import { homeLocation } from '../../../config/env';
import { jwtSecret as _jwtSecret } from '../../../config/secrets';
2018-08-29 19:52:41 +00:00
import { wrapHandledError } from '../utils/create-handled-error';
2018-11-29 12:12:15 +00:00
// We need to tunnel through a proxy path set up within
// the gatsby app, at this time, that path is /internal
const apiProxyRE = /^\/internal\/|^\/external\//;
const newsShortLinksRE = /^\/internal\/n\/|^\/internal\/p\?/;
const loopbackAPIPathRE = /^\/internal\/api\//;
2019-02-16 13:51:46 +00:00
const _whiteListREs = [newsShortLinksRE, loopbackAPIPathRE];
2019-02-16 13:51:46 +00:00
export function isWhiteListedPath(path, whiteListREs = _whiteListREs) {
return whiteListREs.some(re => re.test(path));
}
2018-11-29 12:12:15 +00:00
export default ({ jwtSecret = _jwtSecret, getUserById = _getUserById } = {}) =>
2019-02-20 18:18:50 +00:00
function requestAuthorisation(req, res, next) {
2019-02-16 13:51:46 +00:00
const { path } = req;
if (apiProxyRE.test(path) && !isWhiteListedPath(path)) {
const { accessToken, error, jwt } = getAccessTokenFromRequest(
req,
jwtSecret
);
if (!accessToken && error === errorTypes.noTokenFound) {
2019-02-16 13:51:46 +00:00
throw wrapHandledError(
new Error('Access token is required for this request'),
{
type: 'info',
redirect: `${homeLocation}/signin`,
message: 'Access token is required for this request',
status: 403
}
);
}
if (!accessToken && error === errorTypes.invalidToken) {
throw wrapHandledError(new Error('Access token is invalid'), {
type: 'info',
2018-08-29 19:52:41 +00:00
redirect: `${homeLocation}/signin`,
message: 'Your access token is invalid',
status: 403
2019-02-16 13:51:46 +00:00
});
}
if (!accessToken && error === errorTypes.expiredToken) {
2019-02-16 13:51:46 +00:00
throw wrapHandledError(new Error('Access token is no longer vaild'), {
type: 'info',
2018-08-29 19:52:41 +00:00
redirect: `${homeLocation}/signin`,
message: 'Access token is no longer vaild',
status: 403
2019-02-16 13:51:46 +00:00
});
}
res.set(authHeaderNS, jwt);
2019-02-20 18:18:50 +00:00
if (isEmpty(req.user)) {
const { userId } = accessToken;
2019-02-20 18:18:50 +00:00
return getUserById(userId)
2019-02-16 13:51:46 +00:00
.then(user => {
if (user) {
user.points = user.progressTimestamps.length;
req.user = user;
}
return;
})
.then(next)
.catch(next);
} else {
2019-02-20 18:18:50 +00:00
return Promise.resolve(next());
2019-02-16 13:51:46 +00:00
}
}
2019-02-20 18:18:50 +00:00
return Promise.resolve(next());
2019-02-16 13:51:46 +00:00
};
2019-02-20 18:18:50 +00:00
export function _getUserById(id) {
const User = loopback.getModelByType('User');
return new Promise((resolve, reject) =>
User.findById(id, (err, instance) => {
if (err) {
return reject(err);
}
return resolve(instance);
})
);
}