mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-20 13:02:19 -04:00
* feat: initial button setup client * feat: rename walletsButton to .tsx * chore: typescriptize wallet component * chore: re-add keys to config, env, etc + check in gatsby-node * feat: refactor donate form and wallet component * feat(client): set labels correctly * chore: add stripe package back to server * chore: add stripe back to allowed paths * chore: copy donate.js code from PR #41924 * feat: attempt to make back end work * feat: make redux work * feat: clean up * feat: hokify * feat: add error handling * fix: back-end should be working * fix: type errors * fix: clean up back-end * feat:addd styles * feat: connect the client to the api * feat: display wallets button everywhere * test: add stripe key for cypress action * test: fix for cypress tests * test: cypress tests again * test: maybe? * test: more * test: more * test: more * test * askdfjasklfj * fix: tests finally? * revert: remove space from cypress yaml action * remove logs Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
110 lines
3.2 KiB
JavaScript
110 lines
3.2 KiB
JavaScript
import { isEmpty } from 'lodash';
|
|
|
|
import { jwtSecret as _jwtSecret } from '../../../../config/secrets';
|
|
|
|
import { wrapHandledError } from '../utils/create-handled-error';
|
|
import {
|
|
getAccessTokenFromRequest,
|
|
errorTypes,
|
|
authHeaderNS
|
|
} from '../utils/getSetAccessToken';
|
|
import { getRedirectParams } from '../utils/redirection';
|
|
import { getUserById as _getUserById } from '../utils/user-stats';
|
|
|
|
const authRE = /^\/auth\//;
|
|
const confirmEmailRE = /^\/confirm-email$/;
|
|
const newsShortLinksRE = /^\/n\/|^\/p\//;
|
|
const publicUserRE = /^\/api\/users\/get-public-profile$/;
|
|
const publicUsernameRE = /^\/api\/users\/exists$/;
|
|
const resubscribeRE = /^\/resubscribe\//;
|
|
const showCertRE = /^\/certificate\/showCert\//;
|
|
// note: signin may not have a trailing slash
|
|
const signinRE = /^\/signin/;
|
|
const statusRE = /^\/status\/ping$/;
|
|
const unsubscribedRE = /^\/unsubscribed\//;
|
|
const unsubscribeRE = /^\/u\/|^\/unsubscribe\/|^\/ue\//;
|
|
const updateHooksRE = /^\/hooks\/update-paypal$|^\/hooks\/update-stripe$/;
|
|
const createStripeSession = /^\/donate\/create-stripe-session/;
|
|
// note: this would be replaced by webhooks later
|
|
const donateRE = /^\/donate\/charge-stripe$/;
|
|
|
|
const _pathsAllowedREs = [
|
|
authRE,
|
|
confirmEmailRE,
|
|
newsShortLinksRE,
|
|
publicUserRE,
|
|
publicUsernameRE,
|
|
resubscribeRE,
|
|
showCertRE,
|
|
signinRE,
|
|
statusRE,
|
|
unsubscribedRE,
|
|
unsubscribeRE,
|
|
updateHooksRE,
|
|
donateRE,
|
|
createStripeSession
|
|
];
|
|
|
|
export function isAllowedPath(path, pathsAllowedREs = _pathsAllowedREs) {
|
|
return pathsAllowedREs.some(re => re.test(path));
|
|
}
|
|
|
|
export default function getRequestAuthorisation({
|
|
jwtSecret = _jwtSecret,
|
|
getUserById = _getUserById
|
|
} = {}) {
|
|
return function requestAuthorisation(req, res, next) {
|
|
const { origin } = getRedirectParams(req);
|
|
const { path } = req;
|
|
if (!isAllowedPath(path)) {
|
|
const { accessToken, error, jwt } = getAccessTokenFromRequest(
|
|
req,
|
|
jwtSecret
|
|
);
|
|
if (!accessToken && error === errorTypes.noTokenFound) {
|
|
throw wrapHandledError(
|
|
new Error('Access token is required for this request'),
|
|
{
|
|
type: 'info',
|
|
redirect: `${origin}/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',
|
|
redirect: `${origin}/signin`,
|
|
message: 'Your access token is invalid',
|
|
status: 403
|
|
});
|
|
}
|
|
if (!accessToken && error === errorTypes.expiredToken) {
|
|
throw wrapHandledError(new Error('Access token is no longer valid'), {
|
|
type: 'info',
|
|
redirect: `${origin}/signin`,
|
|
message: 'Access token is no longer valid',
|
|
status: 403
|
|
});
|
|
}
|
|
res.set(authHeaderNS, jwt);
|
|
if (isEmpty(req.user)) {
|
|
const { userId } = accessToken;
|
|
return getUserById(userId)
|
|
.then(user => {
|
|
if (user) {
|
|
req.user = user;
|
|
}
|
|
return;
|
|
})
|
|
.then(next)
|
|
.catch(next);
|
|
} else {
|
|
return Promise.resolve(next());
|
|
}
|
|
}
|
|
return Promise.resolve(next());
|
|
};
|
|
}
|