Skip to content

Commit

Permalink
chore: use set to check supported algorithms (#336)
Browse files Browse the repository at this point in the history
* feat: ignore pnpm

* fix: use set

* chore: reduce
  • Loading branch information
ilteoood authored May 17, 2023
1 parent 8083362 commit b950a84
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package-lock.json
yarn.lock
pnpm-lock.yaml

.clinic
.nyc_output
Expand Down
21 changes: 10 additions & 11 deletions src/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const { TokenError } = require('./error')
const { getAsyncKey, ensurePromiseCallback } = require('./utils')
const { createPrivateKey, createSecretKey } = require('crypto')

const supportedAlgorithms = Array.from(
new Set([...hsAlgorithms, ...esAlgorithms, ...rsaAlgorithms, ...edAlgorithms, 'none'])
).join(', ')
const supportedAlgorithms = new Set([...hsAlgorithms, ...esAlgorithms, ...rsaAlgorithms, ...edAlgorithms, 'none'])

const supportedAlgorithmsList = Array.from(supportedAlgorithms).join(', ')

function checkIsCompatibleAlgorithm(expected, actual) {
const expectedType = expected.slice(0, 2)
Expand Down Expand Up @@ -193,15 +193,11 @@ module.exports = function createSigner(options) {
// Validate options
if (
algorithm &&
algorithm !== 'none' &&
!hsAlgorithms.includes(algorithm) &&
!esAlgorithms.includes(algorithm) &&
!rsaAlgorithms.includes(algorithm) &&
!edAlgorithms.includes(algorithm)
!supportedAlgorithms.has(algorithm)
) {
throw new TokenError(
TokenError.codes.invalidOption,
`The algorithm option must be one of the following values: ${supportedAlgorithms}.`
`The algorithm option must be one of the following values: ${supportedAlgorithmsList}.`
)
}

Expand Down Expand Up @@ -285,8 +281,11 @@ module.exports = function createSigner(options) {
}

const fpo = { jti, aud, iss, sub, nonce }
const fixedPayload = Object.keys(fpo).reduce((obj, key) => {
return fpo[key] !== undefined ? Object.assign(obj, { [key]: fpo[key] }) : obj
const fixedPayload = Object.entries(fpo).reduce((obj, [key, value]) => {
if (value !== undefined) {
obj[key] = value
}
return obj
}, {})

// Return the signer
Expand Down

0 comments on commit b950a84

Please sign in to comment.