diff --git a/index.js b/index.js index 366692f..0c2fe70 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ import * as client from "openid-client"; import session from "express-session"; import morgan from "morgan"; import bodyParser from "body-parser"; +import { chain, isObject } from "lodash-es"; const port = parseInt(process.env.PORT, 10) || 3000; const app = express(); @@ -18,18 +19,12 @@ app.use( ); app.use(morgan("combined")); -const removeNullValues = (obj) => - Object.entries(obj).reduce((a, [k, v]) => (v ? ((a[k] = v), a) : a), {}); - const objToUrlParams = (obj) => new URLSearchParams( - Object.fromEntries( - Object.entries(obj).map(([k, v]) => [ - k, - // stringify objects - typeof v === "object" && v !== null ? JSON.stringify(v) : v, - ]), - ), + chain(obj) + .omitBy((v) => !v) + .mapValues((o) => (isObject(o) ? JSON.stringify(o) : o)) + .value(), ); const getCurrentUrl = (req) => @@ -90,14 +85,12 @@ const getAuthorizationControllerFactory = (extraParams) => { const redirectUrl = client.buildAuthorizationUrl( config, - objToUrlParams( - removeNullValues({ - nonce, - state, - ...AUTHORIZATION_DEFAULT_PARAMS, - ...extraParams, - }), - ), + objToUrlParams({ + nonce, + state, + ...AUTHORIZATION_DEFAULT_PARAMS, + ...extraParams, + }), ); res.redirect(redirectUrl); @@ -193,10 +186,10 @@ app.post("/logout", async (req, res, next) => { const config = await getProviderConfig(); const redirectUrl = client.buildEndSessionUrl( config, - objToUrlParams(removeNullValues({ + objToUrlParams({ post_logout_redirect_uri: `${process.env.HOST}/`, id_token_hint, - })), + }), ); res.redirect(redirectUrl); diff --git a/package-lock.json b/package-lock.json index f3698fe..b4b7530 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "ejs": "^3.1.10", "express": "^4.21.1", "express-session": "^1.18.1", + "lodash-es": "^4.17.21", "morgan": "^1.10.0", "openid-client": "^6.1.3" }, @@ -604,6 +605,11 @@ "url": "https://github.com/sponsors/panva" } }, + "node_modules/lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -1465,6 +1471,11 @@ "resolved": "https://registry.npmjs.org/jose/-/jose-5.9.6.tgz", "integrity": "sha512-AMlnetc9+CV9asI19zHmrgS/WYsWUwCn2R7RzlbJWD7F9eWYUTGyBmU9o6PxngtLGOiDGPRu+Uc4fhKzbpteZQ==" }, + "lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", diff --git a/package.json b/package.json index e70f908..9c794db 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "ejs": "^3.1.10", "express": "^4.21.1", "express-session": "^1.18.1", + "lodash-es": "^4.17.21", "morgan": "^1.10.0", "openid-client": "^6.1.3" },