@opengovsg/mockpass 2.7.6 → 2.7.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -56,13 +56,13 @@ function config(app) {
56
56
  const rawArtifact = req.query.SAMLart || req.query.code
57
57
  const artifact = rawArtifact.replace(/ /g, '+')
58
58
  const artifactBuffer = Buffer.from(artifact, 'base64')
59
+ const artifactMessage = artifactBuffer.toString('utf8', 24)
59
60
  let index = artifactBuffer.readInt8(artifactBuffer.length - 1)
60
61
 
61
62
  const state = req.query.RelayState || req.query.state
62
63
  let id
63
- const isRawNRIC = rawArtifact.length === 9
64
- if (isRawNRIC) {
65
- id = rawArtifact
64
+ if (artifactMessage.startsWith('customNric:')) {
65
+ id = artifactMessage.slice('customNric:'.length)
66
66
  } else {
67
67
  const assertionType = req.query.code ? 'oidc' : 'saml'
68
68
 
@@ -6,7 +6,7 @@ const path = require('path')
6
6
  const ExpiryMap = require('expiry-map')
7
7
 
8
8
  const assertions = require('../assertions')
9
- const samlArtifact = require('../saml-artifact')
9
+ const { samlArtifact } = require('../saml-artifact')
10
10
 
11
11
  const LOGIN_TEMPLATE = fs.readFileSync(
12
12
  path.resolve(__dirname, '../../static/html/login-page.html'),
@@ -2,13 +2,13 @@ const express = require('express')
2
2
  const fs = require('fs')
3
3
  const { render } = require('mustache')
4
4
  const path = require('path')
5
- const { DOMParser } = require('xmldom')
5
+ const { DOMParser } = require('@xmldom/xmldom')
6
6
  const xpath = require('xpath')
7
7
  const moment = require('moment')
8
8
 
9
9
  const assertions = require('../assertions')
10
10
  const crypto = require('../crypto')
11
- const samlArtifact = require('../saml-artifact')
11
+ const { samlArtifact, hashPartnerId } = require('../saml-artifact')
12
12
 
13
13
  const domParser = new DOMParser()
14
14
  const dom = (xmlString) => domParser.parseFromString(xmlString)
@@ -44,12 +44,11 @@ function config(
44
44
  ? MYINFO_ASSERT_ENDPOINT
45
45
  : idpConfig[idp].assertEndpoint || req.query.PartnerId
46
46
  const relayState = req.query.Target
47
+ const partnerId = idpConfig[idp].id
47
48
  if (showLoginPage) {
48
49
  const saml = assertions.saml[idp]
49
50
  const values = saml.map((rawId, index) => {
50
- const samlArt = encodeURIComponent(
51
- samlArtifact(idpConfig[idp].id, index),
52
- )
51
+ const samlArt = encodeURIComponent(samlArtifact(partnerId, index))
53
52
  let assertURL = `${assertEndpoint}?SAMLart=${samlArt}`
54
53
  if (relayState !== undefined) {
55
54
  assertURL += `&RelayState=${encodeURIComponent(relayState)}`
@@ -57,14 +56,16 @@ function config(
57
56
  const id = idGenerator[idp](rawId)
58
57
  return { id, assertURL }
59
58
  })
59
+ const hashedPartnerId = hashPartnerId(partnerId)
60
60
  const response = render(LOGIN_TEMPLATE, {
61
61
  values,
62
62
  assertEndpoint,
63
63
  relayState,
64
+ hashedPartnerId,
64
65
  })
65
66
  res.send(response)
66
67
  } else {
67
- const samlArt = encodeURIComponent(samlArtifact(idpConfig[idp].id))
68
+ const samlArt = encodeURIComponent(samlArtifact(partnerId))
68
69
  let assertURL = `${assertEndpoint}?SAMLart=${samlArt}`
69
70
  if (relayState !== undefined) {
70
71
  assertURL += `&RelayState=${encodeURIComponent(relayState)}`
@@ -98,13 +99,16 @@ function config(
98
99
  xml,
99
100
  )
100
101
  console.warn(`Received SAML Artifact ${samlArtifact}`)
101
- let nric = samlArtifact
102
- const isRawNRIC = samlArtifact.length === 9
103
- if (!isRawNRIC) {
104
- // Handle encoded base64 Artifact
105
- // Take the template and plug in the typical SingPass/CorpPass response
106
- // Sign and encrypt the assertion
107
- const samlArtifactBuffer = Buffer.from(samlArtifact, 'base64')
102
+ // Handle encoded base64 Artifact
103
+ // Take the template and plug in the typical SingPass/CorpPass response
104
+ // Sign and encrypt the assertion
105
+ const samlArtifactBuffer = Buffer.from(samlArtifact, 'base64')
106
+ const samlArtifactMessage = samlArtifactBuffer.toString('utf8', 24)
107
+
108
+ let nric
109
+ if (samlArtifactMessage.startsWith('customNric:')) {
110
+ nric = samlArtifactMessage.slice('customNric:'.length)
111
+ } else {
108
112
  let index = samlArtifactBuffer.readInt8(
109
113
  samlArtifactBuffer.length - 1,
110
114
  )
@@ -6,7 +6,7 @@ const path = require('path')
6
6
  const ExpiryMap = require('expiry-map')
7
7
 
8
8
  const assertions = require('../assertions')
9
- const samlArtifact = require('../saml-artifact')
9
+ const { samlArtifact } = require('../saml-artifact')
10
10
 
11
11
  const LOGIN_TEMPLATE = fs.readFileSync(
12
12
  path.resolve(__dirname, '../../static/html/login-page.html'),
@@ -14,10 +14,7 @@ const crypto = require('crypto')
14
14
  * the hash of the partner id, followed by 20 random bytes
15
15
  */
16
16
  function samlArtifact(partnerId, index) {
17
- let hashedPartnerId = crypto
18
- .createHash('sha1')
19
- .update(partnerId, 'utf8')
20
- .digest('hex')
17
+ const hashedPartnerId = hashPartnerId(partnerId)
21
18
  const randomBytes = crypto.randomBytes(19).toString('hex')
22
19
  const indexBuffer = Buffer.alloc(1)
23
20
  indexBuffer.writeInt8(index || index === 0 ? index : -1)
@@ -28,4 +25,8 @@ function samlArtifact(partnerId, index) {
28
25
  ).toString('base64')
29
26
  }
30
27
 
31
- module.exports = samlArtifact
28
+ function hashPartnerId(partnerId) {
29
+ return crypto.createHash('sha1').update(partnerId, 'utf8').digest('hex')
30
+ }
31
+
32
+ module.exports = { samlArtifact, hashPartnerId }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengovsg/mockpass",
3
- "version": "2.7.6",
3
+ "version": "2.7.10",
4
4
  "description": "A mock SingPass/CorpPass server for dev purposes",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -36,6 +36,7 @@
36
36
  "node": ">=8.0.0"
37
37
  },
38
38
  "dependencies": {
39
+ "@xmldom/xmldom": "^0.8.0",
39
40
  "base-64": "^1.0.0",
40
41
  "cookie-parser": "^1.4.3",
41
42
  "dotenv": "^10.0.0",
@@ -50,20 +51,19 @@
50
51
  "uuid": "^8.0.0",
51
52
  "xml-crypto": "^2.1.2",
52
53
  "xml-encryption": "^1.2.4",
53
- "xmldom": "^0.6.0",
54
54
  "xpath": "0.0.32"
55
55
  },
56
56
  "devDependencies": {
57
- "@commitlint/cli": "^12.0.0",
58
- "@commitlint/config-conventional": "^12.0.0",
59
- "@commitlint/travis-cli": "^12.0.0",
57
+ "@commitlint/cli": "^16.0.1",
58
+ "@commitlint/config-conventional": "^16.0.0",
59
+ "@commitlint/travis-cli": "^16.0.1",
60
60
  "commitizen": "^4.2.4",
61
61
  "cz-conventional-changelog": "^3.2.0",
62
- "eslint": "^7.25.0",
62
+ "eslint": "^8.0.0",
63
63
  "eslint-config-prettier": "^8.3.0",
64
- "eslint-plugin-prettier": "^3.4.0",
65
- "husky": "^6.0.0",
66
- "lint-staged": "^11.0.0",
64
+ "eslint-plugin-prettier": "^4.0.0",
65
+ "husky": "^7.0.0",
66
+ "lint-staged": "^12.0.2",
67
67
  "nodemon": "^2.0.4",
68
68
  "pinst": "^2.1.6",
69
69
  "prettier": "^2.0.5"
@@ -768,6 +768,31 @@ function invalidLoginAction(errorMessage, captchaVal) {
768
768
  }
769
769
  }
770
770
 
771
+ function hexEncode(str) {
772
+ var result = '';
773
+ for (var i = 0; i < str.length; i++) {
774
+ result += str.charCodeAt(i).toString(16);
775
+ }
776
+ return result;
777
+ }
778
+
779
+ function hexToBase64(hexString) {
780
+ return btoa(hexString.match(/\w{2}/g).map(function(a) {
781
+ return String.fromCharCode(parseInt(a, 16));
782
+ }).join(''));
783
+ }
784
+
785
+ function generateSamlArtFromCustomNric() {
786
+ var customNric = document.getElementById('customNric').value;
787
+ if (customNric.length !== 9) {
788
+ return false;
789
+ }
790
+ var hashedPartnerId = document.getElementById('hashedPartnerId').value;
791
+ var artifactDataHex = '00040000' + hashedPartnerId + hexEncode('customNric:' + customNric);
792
+ document.getElementById('customNricSamlArt').value = hexToBase64(artifactDataHex);
793
+ return true;
794
+ }
795
+
771
796
  /*******************************************************************************
772
797
  * WOGAA RELATED METHODS STARTS
773
798
  ******************************************************************************/
@@ -181,13 +181,15 @@
181
181
  <input type="hidden" name="CSRFToken" value="null" />
182
182
  </div>
183
183
  </form>
184
- <form action="{{assertEndpoint}}" method="get">
184
+ <form action="{{assertEndpoint}}" method="get" onsubmit="return generateSamlArtFromCustomNric()">
185
185
  <br>
186
186
  {{#assertEndpoint}}
187
187
  <h6>or with your own user</h6>
188
188
  <br>
189
189
  <input type="hidden" name="RelayState" value="{{ relayState }}" />
190
- <input maxlength="9" name="SAMLart" placeholder="NRIC" value="S1234567A" style="width: 100%; border: 2px solid #ccc; border-radius: 5px; background: white; color: rgb(42, 45, 51); text-align: left;">
190
+ <input type="hidden" id="hashedPartnerId" value="{{ hashedPartnerId }}" />
191
+ <input minlength="9" maxlength="9" id="customNric" placeholder="NRIC" value="S1234567A" style="width: 100%; border: 2px solid #ccc; border-radius: 5px; background: white; color: rgb(42, 45, 51); text-align: left;">
192
+ <input type="hidden" id="customNricSamlArt" name="SAMLart" />
191
193
  <button autofocus="" type="submit">Login</button>
192
194
  <br>
193
195
  <br>