Skip to content

Commit c1eb23a

Browse files
[mirotalkc2c] - fix: cache OIDC auth() middleware to avoid re-creation on every request
1 parent bdb36dd commit c1eb23a

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

.env.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ====================================================
2-
# MiroTalk C2C v.1.2.78 - Environment Configuration
2+
# MiroTalk C2C v.1.2.79 - Environment Configuration
33
# ====================================================
44

55
# App environment

backend/server.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @license For private project or commercial purposes contact us at: license.mirotalk@gmail.com or purchase it directly via Code Canyon:
1010
* @license https://codecanyon.net/item/mirotalk-c2c-webrtc-real-time-cam-2-cam-video-conferences-and-screen-sharing/43383005
1111
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
12-
* @version 1.2.78
12+
* @version 1.2.79
1313
*/
1414

1515
require('dotenv').config();
@@ -211,28 +211,32 @@ app.use((err, req, res, next) => {
211211

212212
// OpenID Connect - Dynamically set baseURL based on incoming host and protocol
213213
if (OIDC.enabled) {
214-
const getDynamicConfig = (host, protocol) => {
215-
const baseURL = `${protocol}://${host}`;
214+
const authMiddlewareCache = new Map();
216215

217-
const config = OIDC.baseUrlDynamic
218-
? {
219-
...OIDC.config,
220-
baseURL,
221-
}
222-
: OIDC.config;
223-
224-
log.debug('OIDC baseURL', config.baseURL);
216+
const getAuthMiddleware = (host, protocol) => {
217+
if (!OIDC.baseUrlDynamic) {
218+
if (!authMiddlewareCache.has('static')) {
219+
log.debug('OIDC baseURL', OIDC.config.baseURL);
220+
authMiddlewareCache.set('static', auth(OIDC.config));
221+
}
222+
return authMiddlewareCache.get('static');
223+
}
225224

226-
return config;
225+
const key = `${protocol}://${host}`;
226+
if (!authMiddlewareCache.has(key)) {
227+
const config = { ...OIDC.config, baseURL: key };
228+
log.debug('OIDC baseURL', config.baseURL);
229+
authMiddlewareCache.set(key, auth(config));
230+
}
231+
return authMiddlewareCache.get(key);
227232
};
228233

229-
// Apply the authentication middleware using dynamic baseURL configuration
234+
// Apply the cached authentication middleware
230235
app.use((req, res, next) => {
231236
const host = req.headers.host;
232237
const protocol = req.protocol === 'https' ? 'https' : 'http';
233-
const dynamicOIDCConfig = getDynamicConfig(host, protocol);
234238
try {
235-
auth(dynamicOIDCConfig)(req, res, next);
239+
getAuthMiddleware(host, protocol)(req, res, next);
236240
} catch (err) {
237241
log.error('OIDC Auth Middleware Error', err);
238242
process.exit(1);

frontend/js/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @license For private project or commercial purposes contact us at: license.mirotalk@gmail.com or purchase it directly via Code Canyon:
1010
* @license https://codecanyon.net/item/mirotalk-c2c-webrtc-real-time-cam-2-cam-video-conferences-and-screen-sharing/43383005
1111
* @author Miroslav Pejic - miroslav.pejic.85@gmail.com
12-
* @version 1.2.78
12+
* @version 1.2.79
1313
*/
1414

1515
const roomId = new URLSearchParams(window.location.search).get('room');

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mirotalkc2c",
3-
"version": "1.2.78",
3+
"version": "1.2.79",
44
"description": "A free WebRTC Cam-2-Cam browser-based video calls",
55
"main": "server.js",
66
"scripts": {

0 commit comments

Comments
 (0)