chore(api): remove unused session handling (#55240)

This commit is contained in:
Oliver Eyton-Williams
2024-06-20 20:18:16 +02:00
committed by GitHub
parent 3557bdfee0
commit 4c412bff9a
7 changed files with 52 additions and 155 deletions
+1 -3
View File
@@ -8,7 +8,6 @@
"@fastify/cookie": "9.3.1",
"@fastify/csrf-protection": "6.4.1",
"@fastify/express": "^2.3.0",
"@fastify/session": "10.7.0",
"@fastify/swagger": "8.14.0",
"@fastify/swagger-ui": "1.10.2",
"@fastify/type-provider-typebox": "3.6.0",
@@ -16,7 +15,6 @@
"@prisma/client": "5.5.2",
"ajv": "8.12.0",
"ajv-formats": "2.1.1",
"connect-mongo": "4.6.0",
"date-fns": "2.30.0",
"dotenv": "16.4.5",
"express-rate-limit": "^6.7.0",
@@ -40,7 +38,7 @@
"description": "The freeCodeCamp.org open-source codebase and curriculum",
"devDependencies": {
"@total-typescript/ts-reset": "0.5.1",
"@types/express-session": "1.17.10",
"@types/express": "4.17.21",
"@types/jsonwebtoken": "9.0.5",
"@types/nodemailer": "6.4.14",
"@types/supertest": "2.0.16",
+2 -33
View File
@@ -1,13 +1,11 @@
import fastifyCsrfProtection from '@fastify/csrf-protection';
import express from '@fastify/express';
import fastifySession from '@fastify/session';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import fastifySentry from '@immobiliarelabs/fastify-sentry';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import MongoStore from 'connect-mongo';
import uriResolver from 'fast-uri';
import Fastify, {
FastifyBaseLogger,
@@ -26,7 +24,6 @@ import { SESProvider } from './plugins/mail-providers/ses';
import mailer from './plugins/mailer';
import redirectWithMessage from './plugins/redirect-with-message';
import security from './plugins/security';
import sessionAuth from './plugins/session-auth';
import codeFlowAuth from './plugins/code-flow-auth';
import { mobileAuth0Routes } from './routes/auth';
import { devAuthRoutes } from './routes/auth-dev';
@@ -49,9 +46,7 @@ import {
FCC_ENABLE_DEV_LOGIN_MODE,
FCC_ENABLE_SWAGGER_UI,
FREECODECAMP_NODE_ENV,
MONGOHQ_URL,
SENTRY_DSN,
SESSION_SECRET
SENTRY_DSN
} from './utils/env';
import { isObjectID } from './utils/validation';
@@ -153,21 +148,6 @@ export const build = async (
done();
});
// @ts-expect-error - @fastify/session's types are not, yet, compatible with
// express-session's types
await fastify.register(fastifySession, {
secret: SESSION_SECRET,
rolling: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60, // 1 hour
secure: FREECODECAMP_NODE_ENV !== 'development'
},
store: MongoStore.create({
mongoUrl: MONGOHQ_URL
})
});
const provider =
EMAIL_PROVIDER === 'ses' ? new SESProvider() : new NodemailerProvider();
void fastify.register(mailer, { provider });
@@ -179,17 +159,7 @@ export const build = async (
info: {
title: 'freeCodeCamp API',
version: '1.0.0' // API version
},
components: {
securitySchemes: {
session: {
type: 'apiKey',
name: 'sessionId',
in: 'cookie'
}
}
},
security: [{ session: [] }]
}
}
});
void fastify.register(fastifySwaggerUI, {
@@ -210,7 +180,6 @@ export const build = async (
fastify.log.info(`Swagger UI available at ${API_LOCATION}/documentation`);
}
void fastify.register(sessionAuth);
void fastify.register(codeFlowAuth);
void fastify.register(prismaPlugin);
void fastify.register(mobileAuth0Routes);
-25
View File
@@ -1,25 +0,0 @@
import { FastifyPluginCallback, onRequestHookHandler } from 'fastify';
import fp from 'fastify-plugin';
const sessionAuth: FastifyPluginCallback = (fastify, _opts, done) => {
const authenticateSession: onRequestHookHandler = (req, res, done) => {
if (!req.session.user) {
res.statusCode = 401;
void res.send({ msg: 'Unauthorized' });
} else {
done();
}
};
fastify.decorate('authenticateSession', authenticateSession);
done();
};
declare module 'fastify' {
interface FastifyInstance {
authenticateSession: onRequestHookHandler;
}
}
export default fp(sessionAuth);
+1 -11
View File
@@ -7,14 +7,6 @@ import MongoStoreRL from 'rate-limit-mongo';
import { AUTH0_DOMAIN, MONGOHQ_URL } from '../utils/env';
import { findOrCreateUser } from './helpers/auth-helpers';
declare module 'fastify' {
interface Session {
user: {
id: string;
};
}
}
const getEmailFromAuth0 = async (req: FastifyRequest) => {
const auth0Res = await fetch(`https://${AUTH0_DOMAIN}/userinfo`, {
headers: {
@@ -66,9 +58,7 @@ export const mobileAuth0Routes: FastifyPluginCallback = (
fastify.get('/mobile-login', async req => {
const email = await getEmailFromAuth0(req);
const { id } = await findOrCreateUser(fastify, email);
req.session.user = { id };
await req.session.save();
await findOrCreateUser(fastify, email);
});
done();
-1
View File
@@ -119,7 +119,6 @@ export const userRoutes: FastifyPluginCallbackTypebox = (
await fastify.prisma.user.delete({
where: { id: req.user!.id }
});
await req.session.destroy();
void reply.clearCookie('sessionId');
return {};
-7
View File
@@ -48,7 +48,6 @@ assert.ok(isAllowedProvider(process.env.EMAIL_PROVIDER));
assert.ok(process.env.AUTH0_DOMAIN);
assert.ok(process.env.AUTH0_AUDIENCE);
assert.ok(process.env.API_LOCATION);
assert.ok(process.env.SESSION_SECRET);
assert.ok(process.env.FCC_ENABLE_SWAGGER_UI);
assert.ok(process.env.FCC_ENABLE_DEV_LOGIN_MODE);
assert.ok(process.env.JWT_SECRET);
@@ -82,11 +81,6 @@ if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
'a_jwt_secret',
'The JWT secret should be changed from the default value.'
);
assert.notEqual(
process.env.SESSION_SECRET,
'a_thirty_two_plus_character_session_secret',
'The session secret should be changed from the default value.'
);
assert.ok(
process.env.FCC_ENABLE_DEV_LOGIN_MODE !== 'true',
'Dev login mode MUST be disabled in production.'
@@ -118,7 +112,6 @@ export const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN;
export const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE;
export const PORT = process.env.PORT || '3000';
export const API_LOCATION = process.env.API_LOCATION;
export const SESSION_SECRET = process.env.SESSION_SECRET;
export const FCC_ENABLE_SWAGGER_UI =
process.env.FCC_ENABLE_SWAGGER_UI === 'true';
export const FCC_ENABLE_DEV_LOGIN_MODE =
+48 -75
View File
@@ -162,9 +162,6 @@ importers:
'@fastify/express':
specifier: ^2.3.0
version: 2.3.0
'@fastify/session':
specifier: 10.7.0
version: 10.7.0
'@fastify/swagger':
specifier: 8.14.0
version: 8.14.0
@@ -186,9 +183,6 @@ importers:
ajv-formats:
specifier: 2.1.1
version: 2.1.1(ajv@8.12.0)
connect-mongo:
specifier: 4.6.0
version: 4.6.0(express-session@1.17.3)(mongodb@4.17.2)
date-fns:
specifier: 2.30.0
version: 2.30.0
@@ -250,9 +244,9 @@ importers:
'@total-typescript/ts-reset':
specifier: 0.5.1
version: 0.5.1
'@types/express-session':
specifier: 1.17.10
version: 1.17.10
'@types/express':
specifier: 4.17.21
version: 4.17.21
'@types/jsonwebtoken':
specifier: 9.0.5
version: 9.0.5
@@ -2995,9 +2989,6 @@ packages:
'@fastify/send@2.1.0':
resolution: {integrity: sha512-yNYiY6sDkexoJR0D8IDy3aRP3+L4wdqCpvx5WP+VtEU58sn7USmKynBzDQex5X42Zzvw2gNzzYgP90UfWShLFA==}
'@fastify/session@10.7.0':
resolution: {integrity: sha512-ECA75gnyaxcyIukgyO2NGT3XdbLReNl/pTKrrkRfDc6pVqNtdptwwfx9KXrIMOfsO4B3m84eF3wZ9GgnebiZ4w==}
'@fastify/static@6.11.2':
resolution: {integrity: sha512-EH7mh7q4MfNdT7N07ZVlwsX/ObngMvQ7KBP0FXAuPov99Fjn80KSJMdxQhhYKAKWW1jXiFdrk8X7d6uGWdZFxg==}
@@ -4108,9 +4099,6 @@ packages:
'@types/express-serve-static-core@4.17.37':
resolution: {integrity: sha512-ZohaCYTgGFcOP7u6aJOhY9uIZQgZ2vxC2yWoArY+FeDXlqeH66ZVBjgvg+RLVAS/DWNq4Ap9ZXu1+SUQiiWYMg==}
'@types/express-session@1.17.10':
resolution: {integrity: sha512-U32bC/s0ejXijw5MAzyaV4tuZopCh/K7fPoUDyNbsRXHvPSeymygYD1RFL99YOLhF5PNOkzswvOTRaVHdL1zMw==}
'@types/express@4.17.18':
resolution: {integrity: sha512-Sxv8BSLLgsBYmcnGdGjjEjqET2U+AKAdCRODmMiq02FgjwuV75Ut85DRpvFjyw/Mk0vgUOliGRU0UUmuuZHByQ==}
@@ -6090,13 +6078,6 @@ packages:
peerDependencies:
express-session: ^1.17.1
connect-mongo@4.6.0:
resolution: {integrity: sha512-8new4Z7NLP3CGP65Aw6ls3xDBeKVvHRSh39CXuDZTQsvpeeU9oNMzfFgvqmHqZ6gWpxIl663RyoVEmCAGf1yOg==}
engines: {node: '>=10'}
peerDependencies:
express-session: ^1.17.1
mongodb: ^4.1.0
connect@3.7.0:
resolution: {integrity: sha512-ZqRXc+tZukToSNmh5C2iWMSoV3X1YUcPbqEM4DkEG5tNQXrQUZCNVGGv3IuicnkMtPfGf3Xtp8WCXs295iQ1pQ==}
engines: {node: '>= 0.10.0'}
@@ -9279,10 +9260,6 @@ packages:
resolution: {integrity: sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA==}
engines: {node: '>= 8'}
kruptein@3.0.6:
resolution: {integrity: sha512-EQJjTwAJfQkC4NfdQdo3HXM2a9pmBm8oidzH270cYu1MbgXPNPMJuldN7OPX+qdhPO5rw4X3/iKz0BFBfkXGKA==}
engines: {node: '>8'}
labeled-stream-splicer@2.0.2:
resolution: {integrity: sha512-Ca4LSXFFZUjPScRaqOcFxneA0VpKZr4MMYCljyQr4LIewTLb3Y0IUTIsnBBsVubIeEfxeSZpSjSsRM8APEQaAw==}
@@ -14555,7 +14532,7 @@ snapshots:
'@babel/traverse': 7.23.7
'@babel/types': 7.23.9
convert-source-map: 2.0.0
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
gensync: 1.0.0-beta.2
json5: 2.2.3
semver: 6.3.1
@@ -14711,7 +14688,7 @@ snapshots:
'@babel/core': 7.23.7
'@babel/helper-compilation-targets': 7.23.6
'@babel/helper-plugin-utils': 7.22.5
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
lodash.debounce: 4.0.8
resolve: 1.22.8
transitivePeerDependencies:
@@ -16758,7 +16735,7 @@ snapshots:
'@babel/helper-split-export-declaration': 7.22.6
'@babel/parser': 7.23.6
'@babel/types': 7.23.9
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
globals: 11.12.0
transitivePeerDependencies:
- supports-color
@@ -16935,7 +16912,7 @@ snapshots:
'@eslint/eslintrc@2.1.4':
dependencies:
ajv: 6.12.6
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
espree: 9.6.1
globals: 13.22.0
ignore: 5.2.4
@@ -16992,11 +16969,6 @@ snapshots:
http-errors: 2.0.0
mime: 3.0.0
'@fastify/session@10.7.0':
dependencies:
fastify-plugin: 4.5.1
safe-stable-stringify: 2.4.3
'@fastify/static@6.11.2':
dependencies:
'@fastify/accept-negotiator': 1.1.0
@@ -17272,7 +17244,7 @@ snapshots:
'@humanwhocodes/config-array@0.11.14':
dependencies:
'@humanwhocodes/object-schema': 2.0.2
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
minimatch: 3.1.2
transitivePeerDependencies:
- supports-color
@@ -18446,10 +18418,6 @@ snapshots:
'@types/range-parser': 1.2.5
'@types/send': 0.17.2
'@types/express-session@1.17.10':
dependencies:
'@types/express': 4.17.21
'@types/express@4.17.18':
dependencies:
'@types/body-parser': 1.19.3
@@ -18826,7 +18794,7 @@ snapshots:
'@typescript-eslint/type-utils': 7.1.1(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/utils': 7.1.1(eslint@8.57.0)(typescript@5.4.5)
'@typescript-eslint/visitor-keys': 7.1.1
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
eslint: 8.57.0
graphemer: 1.4.0
ignore: 5.2.4
@@ -18881,7 +18849,7 @@ snapshots:
'@typescript-eslint/types': 7.1.1
'@typescript-eslint/typescript-estree': 7.1.1(typescript@5.4.5)
'@typescript-eslint/visitor-keys': 7.1.1
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
eslint: 8.57.0
optionalDependencies:
typescript: 5.4.5
@@ -18907,7 +18875,7 @@ snapshots:
dependencies:
'@typescript-eslint/typescript-estree': 7.1.1(typescript@5.4.5)
'@typescript-eslint/utils': 7.1.1(eslint@8.57.0)(typescript@5.4.5)
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
eslint: 8.57.0
ts-api-utils: 1.0.3(typescript@5.4.5)
optionalDependencies:
@@ -18956,7 +18924,7 @@ snapshots:
dependencies:
'@typescript-eslint/types': 5.62.0
'@typescript-eslint/visitor-keys': 5.62.0
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
globby: 11.1.0
is-glob: 4.0.3
semver: 7.6.0
@@ -18970,7 +18938,7 @@ snapshots:
dependencies:
'@typescript-eslint/types': 7.1.1
'@typescript-eslint/visitor-keys': 7.1.1
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
globby: 11.1.0
is-glob: 4.0.3
minimatch: 9.0.3
@@ -19216,7 +19184,7 @@ snapshots:
agent-base@6.0.2:
dependencies:
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
transitivePeerDependencies:
- supports-color
@@ -19542,7 +19510,7 @@ snapshots:
dependencies:
'@fastify/error': 3.4.1
archy: 1.0.0
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
fastq: 1.17.1
transitivePeerDependencies:
- supports-color
@@ -21202,15 +21170,6 @@ snapshots:
- mongodb-extjson
- snappy
connect-mongo@4.6.0(express-session@1.17.3)(mongodb@4.17.2):
dependencies:
debug: 4.3.4(supports-color@8.1.1)
express-session: 1.17.3
kruptein: 3.0.6
mongodb: 4.17.2
transitivePeerDependencies:
- supports-color
connect@3.7.0:
dependencies:
debug: 2.6.9
@@ -21642,6 +21601,10 @@ snapshots:
dependencies:
ms: 2.0.0
debug@3.2.7:
dependencies:
ms: 2.1.3
debug@3.2.7(supports-color@5.5.0):
dependencies:
ms: 2.1.3
@@ -21652,6 +21615,10 @@ snapshots:
dependencies:
ms: 2.1.2
debug@4.3.4:
dependencies:
ms: 2.1.2
debug@4.3.4(supports-color@8.1.1):
dependencies:
ms: 2.1.2
@@ -22371,7 +22338,7 @@ snapshots:
eslint-import-resolver-node@0.3.9:
dependencies:
debug: 3.2.7(supports-color@5.5.0)
debug: 3.2.7
is-core-module: 2.13.1
resolve: 1.22.8
transitivePeerDependencies:
@@ -22379,10 +22346,10 @@ snapshots:
eslint-import-resolver-typescript@3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0):
dependencies:
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
enhanced-resolve: 5.15.0
eslint: 8.57.0
eslint-module-utils: 2.8.0(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0)
eslint-module-utils: 2.8.0(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0)
eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.5.5)(eslint@8.57.0)
get-tsconfig: 4.7.2
globby: 13.2.2
@@ -22408,7 +22375,7 @@ snapshots:
eslint-module-utils@2.8.0(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0):
dependencies:
debug: 3.2.7(supports-color@5.5.0)
debug: 3.2.7
optionalDependencies:
'@typescript-eslint/parser': 7.1.1(eslint@8.57.0)(typescript@5.4.5)
eslint: 8.57.0
@@ -22417,6 +22384,16 @@ snapshots:
transitivePeerDependencies:
- supports-color
eslint-module-utils@2.8.0(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-import-resolver-typescript@3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0):
dependencies:
debug: 3.2.7
optionalDependencies:
'@typescript-eslint/parser': 7.1.1(eslint@8.57.0)(typescript@5.4.5)
eslint: 8.57.0
eslint-import-resolver-typescript: 3.5.5(@typescript-eslint/parser@7.1.1(eslint@8.57.0)(typescript@5.4.5))(eslint-plugin-import@2.29.1)(eslint@8.57.0)
transitivePeerDependencies:
- supports-color
eslint-plugin-filenames-simple@0.9.0(eslint@8.57.0):
dependencies:
eslint: 8.57.0
@@ -22475,7 +22452,7 @@ snapshots:
array.prototype.findlastindex: 1.2.3
array.prototype.flat: 1.3.2
array.prototype.flatmap: 1.3.2
debug: 3.2.7(supports-color@5.5.0)
debug: 3.2.7
doctrine: 2.1.0
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
@@ -22509,7 +22486,7 @@ snapshots:
'@es-joy/jsdoccomment': 0.42.0
are-docs-informative: 0.0.2
comment-parser: 1.4.1
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
escape-string-regexp: 4.0.0
eslint: 8.57.0
esquery: 1.5.0
@@ -22719,7 +22696,7 @@ snapshots:
ajv: 6.12.6
chalk: 4.1.2
cross-spawn: 7.0.3
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
doctrine: 3.0.0
escape-string-regexp: 4.0.0
eslint-scope: 7.2.2
@@ -23257,7 +23234,7 @@ snapshots:
follow-redirects@1.15.5(debug@4.3.4):
optionalDependencies:
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
for-each@0.3.3:
dependencies:
@@ -24541,7 +24518,7 @@ snapshots:
https-proxy-agent@5.0.1:
dependencies:
agent-base: 6.0.2
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
transitivePeerDependencies:
- supports-color
@@ -25049,7 +25026,7 @@ snapshots:
istanbul-lib-source-maps@4.0.1:
dependencies:
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
istanbul-lib-coverage: 3.2.0
source-map: 0.6.1
transitivePeerDependencies:
@@ -25595,7 +25572,7 @@ snapshots:
json-schema-resolver@2.0.0:
dependencies:
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
rfdc: 1.3.0
uri-js: 4.4.1
transitivePeerDependencies:
@@ -25706,10 +25683,6 @@ snapshots:
klona@2.0.6: {}
kruptein@3.0.6:
dependencies:
asn1.js: 5.4.1
labeled-stream-splicer@2.0.2:
dependencies:
inherits: 2.0.4
@@ -25763,7 +25736,7 @@ snapshots:
cli-truncate: 3.1.0
colorette: 2.0.20
commander: 9.5.0
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
execa: 6.1.0
lilconfig: 2.0.6
listr2: 5.0.8(enquirer@2.4.1)
@@ -29609,7 +29582,7 @@ snapshots:
arg: 5.0.2
bluebird: 3.7.2
check-more-types: 2.24.0
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
execa: 5.1.1
lazy-ass: 1.6.0
ps-tree: 1.2.0
@@ -29935,7 +29908,7 @@ snapshots:
dependencies:
component-emitter: 1.3.0
cookiejar: 2.1.4
debug: 4.3.4(supports-color@8.1.1)
debug: 4.3.4
fast-safe-stringify: 2.1.1
form-data: 4.0.0
formidable: 2.1.2
@@ -30257,7 +30230,7 @@ snapshots:
'@tsconfig/node14': 1.0.3
'@tsconfig/node16': 1.0.4
'@types/node': 20.12.8
acorn: 8.10.0
acorn: 8.11.3
acorn-walk: 8.2.0
arg: 4.1.3
create-require: 1.1.1