-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(myinfo): generate base string straight from params
- Loading branch information
Showing
1 changed file
with
22 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' + | ||
'×tamp=' + | ||
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 : '') + | ||
'×tamp=' + | ||
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 } |