Skip to content

Commit

Permalink
refactor: extract injectExpressRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienjoly committed Sep 5, 2023
1 parent dc51b1d commit 23d6e6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
28 changes: 28 additions & 0 deletions app/lib/auth0/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check

/** @returns {import('../my-http-wrapper/http/AuthFeatures').AuthFeatures} */
exports.makeAuthFeatures = () => {
const { Auth0Wrapper } = require('.');

return {
/**
* Attach /login, /logout, and /signup routes to Express.js application server.
* @param {import('express').Express} app
* @param {string} urlPrefix
*/
injectExpressRoutes(app, urlPrefix) {
const auth0 = new Auth0Wrapper(process.env); // throws if required env vars are missing

// attach /login, /logout, and /callback routes to the baseURL
app.use(auth0.makeExpressAuthMiddleware(urlPrefix));

// redirects to Auth0's sign up dialog
app.get(
'/signup',
auth0.makeSignupRoute({
returnTo: '/register', // so we can create the user in our database too
}),

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
);
},
};
};
22 changes: 7 additions & 15 deletions app/lib/my-http-wrapper/http/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require('../../../infrastructure/mongodb/UserCollection');
const { ImageStorage } = require('../../../infrastructure/ImageStorage.js');
const { unsetPlaylist } = require('../../../models/post.js');
const { Auth0Wrapper } = require('../../auth0');
const { makeAuthFeatures } = require('../../auth0/features.js');

const LOG_THRESHOLD = parseInt(process.env.LOG_REQ_THRESHOLD_MS ?? '1000', 10);

Expand Down Expand Up @@ -127,11 +127,16 @@ exports.Application = class Application {
const releasePlaylistPosts = async (userId, playlistId) =>
new Promise((resolve) => unsetPlaylist(userId, playlistId, resolve));

/** @type {Features & Partial<{auth: import('./AuthFeatures.js').AuthFeatures}>} */
this._features = makeFeatures({
userRepository,
imageRepository,
releasePlaylistPosts,
});

if (process.appParams.useAuth0AsIdentityProvider) {
this._features.auth = makeAuthFeatures();
}
}

getExpressApp() {
Expand All @@ -147,20 +152,7 @@ exports.Application = class Application {
});
}

if (process.appParams.useAuth0AsIdentityProvider) {
const auth0 = new Auth0Wrapper(process.env); // throws if required env vars are missing

// attach /login, /logout, and /callback routes to the baseURL
app.use(auth0.makeExpressAuthMiddleware(this._urlPrefix));

// redirects to Auth0's sign up dialog
app.get(
'/signup',
auth0.makeSignupRoute({
returnTo: '/register', // so we can create the user in our database too
}),
);
}
this._features.auth?.injectExpressRoutes(app, this._urlPrefix);

// app.set('view engine', 'hogan'); // TODO: use hogan.js to render "mustache" templates when res.render() is called
app.use(noCache); // called on all requests
Expand Down
5 changes: 5 additions & 0 deletions app/lib/my-http-wrapper/http/AuthFeatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Express } from 'express';

export interface AuthFeatures {
injectExpressRoutes(app: Express, urlPrefix: string);
}

0 comments on commit 23d6e6b

Please sign in to comment.