From ffa7b8d1bad5e9e0864da21d1a76ce48cff8a4f1 Mon Sep 17 00:00:00 2001 From: John Yong Date: Fri, 28 Jan 2022 16:07:30 +0800 Subject: [PATCH 1/2] feat: allow overriding `showLoginPage` config via request header --- README.md | 2 +- index.js | 7 ++++++- lib/express/oidc.js | 2 +- lib/express/saml.js | 2 +- lib/express/sgid.js | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e9924878..dd493140 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ $ export MOCKPASS_PORT=5156 $ export MOCKPASS_NRIC=S8979373D $ export MOCKPASS_UEN=123456789A -$ export SHOW_LOGIN_PAGE=true # Optional, defaults to `false` +$ export SHOW_LOGIN_PAGE=true # Optional, defaults to `false`; can be overridden per request using `X-Show-Login-Page` HTTP header # Disable signing/encryption (Optional, by default `true`) $ export SIGN_ASSERTION=false diff --git a/index.js b/index.js index 7f7af72e..0d7cb0c8 100755 --- a/index.js +++ b/index.js @@ -61,7 +61,12 @@ const options = { assertEndpoint: process.env.CORPPASS_ASSERT_ENDPOINT, }, }, - showLoginPage: process.env.SHOW_LOGIN_PAGE === 'true', + showLoginPage: (req) => { + if (req.header('X-Show-Login-Page')) { + return req.header('X-Show-Login-Page') === 'true' + } + return process.env.SHOW_LOGIN_PAGE === 'true' + }, encryptMyInfo: process.env.ENCRYPT_MYINFO === 'true', cryptoConfig, } diff --git a/lib/express/oidc.js b/lib/express/oidc.js index 45efcf23..d24a95c0 100644 --- a/lib/express/oidc.js +++ b/lib/express/oidc.js @@ -30,7 +30,7 @@ function config(app, { showLoginPage, idpConfig, serviceProvider }) { app.get(`/${idp.toLowerCase()}/authorize`, (req, res) => { const redirectURI = req.query.redirect_uri const state = encodeURIComponent(req.query.state) - if (showLoginPage) { + if (showLoginPage(req)) { const oidc = assertions.oidc[idp] const values = oidc.map((rawId, index) => { const code = encodeURIComponent( diff --git a/lib/express/saml.js b/lib/express/saml.js index fe29964f..38e56448 100644 --- a/lib/express/saml.js +++ b/lib/express/saml.js @@ -45,7 +45,7 @@ function config( : idpConfig[idp].assertEndpoint || req.query.PartnerId const relayState = req.query.Target const partnerId = idpConfig[idp].id - if (showLoginPage) { + if (showLoginPage(req)) { const saml = assertions.saml[idp] const values = saml.map((rawId, index) => { const samlArt = encodeURIComponent(samlArtifact(partnerId, index)) diff --git a/lib/express/sgid.js b/lib/express/sgid.js index f3682e26..418b2561 100644 --- a/lib/express/sgid.js +++ b/lib/express/sgid.js @@ -30,7 +30,7 @@ function config(app, { showLoginPage, idpConfig, serviceProvider }) { app.get(`${PATH_PREFIX}/authorize`, (req, res) => { const redirectURI = req.query.redirect_uri const state = encodeURIComponent(req.query.state) - if (showLoginPage) { + if (showLoginPage(req)) { const oidc = assertions.oidc.singPass const values = oidc .filter((rawId) => assertions.myinfo.v3.personas[rawId]) From 45508b73b148ab068efb88aa79d63e00cc7742ca Mon Sep 17 00:00:00 2001 From: LoneRifle Date: Thu, 3 Feb 2022 10:12:50 +0800 Subject: [PATCH 2/2] refactor: impl showLoginPage() as one-liner --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index 0d7cb0c8..418c315c 100755 --- a/index.js +++ b/index.js @@ -62,10 +62,7 @@ const options = { }, }, showLoginPage: (req) => { - if (req.header('X-Show-Login-Page')) { - return req.header('X-Show-Login-Page') === 'true' - } - return process.env.SHOW_LOGIN_PAGE === 'true' + return process.env.SHOW_LOGIN_PAGE === 'true' || req.header('X-Show-Login-Page') === 'true' }, encryptMyInfo: process.env.ENCRYPT_MYINFO === 'true', cryptoConfig,