Skip to content

Commit

Permalink
fix(myinfo): generate base string straight from params
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Nov 8, 2022
1 parent cd7ea43 commit d87b5a3
Showing 1 changed file with 22 additions and 55 deletions.
77 changes: 22 additions & 55 deletions lib/crypto/myinfo-signature.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,39 @@
const _ = require('lodash')
const qs = require('node:querystring')

const pki = function pki(authHeader, req, context = {}) {
const pki = function pki(authHeader, req) {
const authHeaderFieldPairs = _(authHeader)
.replace(/"/g, '')
.split(',')
.map((v) => v.replace('=', '~').split('~'))

const authHeaderFields = _(authHeaderFieldPairs)
.fromPairs()
.mapKeys((_v, k) => _.camelCase(k))
.value()
const authHeaderFields = Object.fromEntries(authHeaderFieldPairs)

const url = `${req.protocol}://${req.get('host')}${req.baseUrl}${req.path}`

const { clientSecret, redirectURI } = context
const { method: httpMethod, query } = req

const {
method: httpMethod,
query: { attributes, sp_esvcId },
} = req
const { signature, app_id, nonce, timestamp } = authHeaderFields

const { code } = req.body || {}

const {
signature,
appId,
appId: clientId,
const params = Object.assign({}, query, {
nonce,
app_id,
signature_method: 'RS256',
timestamp,
} = authHeaderFields
return {
signature,
baseString: req.path.endsWith('/token')
? httpMethod.toUpperCase() +
'&' +
url +
'&app_id=' +
appId +
'&client_id=' +
clientId +
'&client_secret=' +
clientSecret +
'&code=' +
code +
'&grant_type=authorization_code' +
'&nonce=' +
nonce +
'&redirect_uri=' +
redirectURI +
'&signature_method=RS256' +
'&timestamp=' +
timestamp
: httpMethod.toUpperCase() +
'&' +
url +
'&app_id=' +
appId +
'&attributes=' +
attributes +
'&client_id=' +
clientId +
'&nonce=' +
nonce +
'&signature_method=RS256' +
(req.path.includes('/person-basic') ? '&sp_esvcId=' + sp_esvcId : '') +
'&timestamp=' +
timestamp,
}
})

const sortedParams = Object.fromEntries(
Object.entries(params).sort(([k1], [k2]) => k1.localeCompare(k2)),
)

const baseString =
httpMethod.toUpperCase() +
'&' +
url +
'&' +
qs.unescape(qs.stringify(sortedParams))

return { signature, baseString }
}

module.exports = { pki }

0 comments on commit d87b5a3

Please sign in to comment.