jose/lib/jwt/shared_validations.js
Filip Skokan fd69d7f509 refactor: move JWT profile specifics outside of generic JWT
BREAKING CHANGE: the `JWT.verify` profile option was removed, use e.g.
`JWT.IdToken.verify` instead.

BREAKING CHANGE: removed the `maxAuthAge` `JWT.verify` option, this
option is now only present at the specific JWT profile APIs where the
`auth_time` property applies.

BREAKING CHANGE: removed the `nonce` `JWT.verify` option, this
option is now only present at the specific JWT profile APIs where the
`nonce` property applies.

BREAKING CHANGE: the `acr`, `amr`, `nonce` and `azp` claim value types
will only be checked when verifying a specific JWT profile using its
dedicated API.

BREAKING CHANGE: using the draft implementing APIs will emit a one-time
warning per process using `process.emitWarning`
2020-09-08 14:12:04 +02:00

45 lines
1.4 KiB
JavaScript

const { JWTClaimInvalid } = require('../errors')
const isNotString = val => typeof val !== 'string' || val.length === 0
const isNotArrayOfStrings = val => !Array.isArray(val) || val.length === 0 || val.some(isNotString)
const isRequired = (Err, value, label, claim) => {
if (value === undefined) {
throw new Err(`${label} is missing`, claim, 'missing')
}
}
const isString = (Err, value, label, claim, required = false) => {
if (required) {
isRequired(Err, value, label, claim)
}
if (value !== undefined && isNotString(value)) {
throw new Err(`${label} must be a string`, claim, 'invalid')
}
}
const isTimestamp = (value, label, required = false) => {
if (required && value === undefined) {
throw new JWTClaimInvalid(`"${label}" claim is missing`, label, 'missing')
}
if (value !== undefined && (typeof value !== 'number')) {
throw new JWTClaimInvalid(`"${label}" claim must be a JSON numeric value`, label, 'invalid')
}
}
const isStringOrArrayOfStrings = (value, label, required = false) => {
if (required && value === undefined) {
throw new JWTClaimInvalid(`"${label}" claim is missing`, label, 'missing')
}
if (value !== undefined && (isNotString(value) && isNotArrayOfStrings(value))) {
throw new JWTClaimInvalid(`"${label}" claim must be a string or array of strings`, label, 'invalid')
}
}
module.exports = {
isNotArrayOfStrings,
isRequired,
isNotString,
isString,
isTimestamp,
isStringOrArrayOfStrings
}