Skip to content

Commit

Permalink
refactor: allow export of router
Browse files Browse the repository at this point in the history
Package the express router in MockPass to allow export as npm pkg

- Shard initialisation of express router into `./app`
- Specify package entrypoint as `./app`
  - Change `npm start` to explicitly invoke `nodemon index`
  • Loading branch information
LoneRifle committed Feb 7, 2024
1 parent b74cc20 commit c62c7f9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
59 changes: 59 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env node
const fs = require('fs')
const express = require('express')
const morgan = require('morgan')
const path = require('path')
require('dotenv').config()

const {
configOIDC,
configOIDCv2,
configMyInfo,
configSGID,
} = require('./lib/express')

const serviceProvider = {
cert: fs.readFileSync(
path.resolve(
__dirname,
process.env.SERVICE_PROVIDER_CERT_PATH || './static/certs/server.crt',
),
),
pubKey: fs.readFileSync(
path.resolve(
__dirname,
process.env.SERVICE_PROVIDER_PUB_KEY || './static/certs/key.pub',
),
),
}

const cryptoConfig = {
signAssertion: process.env.SIGN_ASSERTION !== 'false', // default to true to be backward compatable
signResponse: process.env.SIGN_RESPONSE !== 'false',
encryptAssertion: process.env.ENCRYPT_ASSERTION !== 'false',
resolveArtifactRequestSigned:
process.env.RESOLVE_ARTIFACT_REQUEST_SIGNED !== 'false',
}

const options = {
serviceProvider,
showLoginPage: (req) =>
(req.header('X-Show-Login-Page') || process.env.SHOW_LOGIN_PAGE) === 'true',
encryptMyInfo: process.env.ENCRYPT_MYINFO === 'true',
cryptoConfig,
}

const app = express()
app.use(morgan('combined'))

configOIDC(app, options)
configOIDCv2(app, options)
configSGID(app, options)

configMyInfo.consent(app)
configMyInfo.v3(app, options)

app.enable('trust proxy')
app.use(express.static(path.join(__dirname, 'public')))

exports.app = app
57 changes: 1 addition & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,8 @@
#!/usr/bin/env node
const fs = require('fs')
const express = require('express')
const morgan = require('morgan')
const path = require('path')
require('dotenv').config()

const {
configOIDC,
configOIDCv2,
configMyInfo,
configSGID,
} = require('./lib/express')
const { app } = require('./app')

const PORT = process.env.MOCKPASS_PORT || process.env.PORT || 5156

const serviceProvider = {
cert: fs.readFileSync(
path.resolve(
__dirname,
process.env.SERVICE_PROVIDER_CERT_PATH || './static/certs/server.crt',
),
),
pubKey: fs.readFileSync(
path.resolve(
__dirname,
process.env.SERVICE_PROVIDER_PUB_KEY || './static/certs/key.pub',
),
),
}

const cryptoConfig = {
signAssertion: process.env.SIGN_ASSERTION !== 'false', // default to true to be backward compatable
signResponse: process.env.SIGN_RESPONSE !== 'false',
encryptAssertion: process.env.ENCRYPT_ASSERTION !== 'false',
resolveArtifactRequestSigned:
process.env.RESOLVE_ARTIFACT_REQUEST_SIGNED !== 'false',
}

const options = {
serviceProvider,
showLoginPage: (req) =>
(req.header('X-Show-Login-Page') || process.env.SHOW_LOGIN_PAGE) === 'true',
encryptMyInfo: process.env.ENCRYPT_MYINFO === 'true',
cryptoConfig,
}

const app = express()
app.use(morgan('combined'))

configOIDC(app, options)
configOIDCv2(app, options)
configSGID(app, options)

configMyInfo.consent(app)
configMyInfo.v3(app, options)

app.enable('trust proxy')
app.use(express.static(path.join(__dirname, 'public')))

app.listen(PORT, (err) =>
err
? console.error('Unable to start MockPass', err)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "@opengovsg/mockpass",
"version": "4.0.11",
"description": "A mock SingPass/CorpPass server for dev purposes",
"main": "index.js",
"main": "app.js",
"bin": {
"mockpass": "index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon",
"start": "nodemon index",
"cz": "git-cz",
"lint": "eslint lib",
"lint-fix": "eslint --fix lib",
Expand Down

0 comments on commit c62c7f9

Please sign in to comment.