Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(myinfo): replace node-jose with jose for SSO #563

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/express/myinfo/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path')
const express = require('express')
const { pick, partition } = require('lodash')

const jose = require('node-jose')
const jose = require('jose')
const jwt = require('jsonwebtoken')

const assertions = require('../../assertions')
Expand All @@ -31,16 +31,27 @@ module.exports =
}

const encryptPersona = async (persona) => {
const signedPersona = jwt.sign(persona, MOCKPASS_PRIVATE_KEY, {
algorithm: 'RS256',
})
const serviceCertAsKey = await jose.JWK.asKey(serviceProvider.cert, 'pem')
const encryptedAndSignedPersona = await jose.JWE.createEncrypt(
{ format: 'compact' },
serviceCertAsKey,
/*
* We sign and encrypt the persona. It's important to note that although a signature is
* usually derived from the payload hash and is thus much smaller than the payload itself,
* we're specifically contructeding a JWT, which contains the original payload.
*
* We then construct a JWE and provide two headers specifying the encryption algorithms used.
* You can read about them here: https://www.rfc-editor.org/rfc/inline-errata/rfc7518.html
*
* These values weren't picked arbitrarily; they were the defaults used by a library we
* formerly used: node-jose. We opted to continue using them for backwards compatibility.
*/
const privateKey = await jose.importPKCS8(MOCKPASS_PRIVATE_KEY.toString())
const sign = await new jose.SignJWT(persona)
.setProtectedHeader({ alg: 'RS256' })
.sign(privateKey)
const publicKey = await jose.importX509(serviceProvider.cert.toString())
const encryptedAndSignedPersona = await new jose.CompactEncrypt(
Buffer.from(sign),
)
.update(JSON.stringify(signedPersona))
.final()
.setProtectedHeader({ alg: 'RSA-OAEP', enc: 'A128CBC-HS256' })
.encrypt(publicKey)
return encryptedAndSignedPersona
}

Expand Down Expand Up @@ -142,7 +153,6 @@ module.exports =
redirect_uri,
})
: {}

if (!tokenTemplate) {
res.status(400).send({
code: 400,
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dotenv": "^16.0.0",
"expiry-map": "^2.0.0",
"express": "^4.16.3",
"jose": "^4.14.4",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.11",
"morgan": "^1.9.1",
Expand Down