@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
118 lines • 5.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonWebTokenClaims = void 0;
const util_1 = require("util");
const expired_jsonwebtoken_exception_1 = require("../exceptions/expired-jsonwebtoken.exception");
const invalid_jsonwebtoken_claim_exception_1 = require("../exceptions/invalid-jsonwebtoken-claim.exception");
const jsonwebtoken_not_valid_yet_exception_1 = require("../exceptions/jsonwebtoken-not-valid-yet.exception");
/**
* Implementation of the JSON Web Token Claims Object.
*
* @see https://www.rfc-editor.org/rfc/rfc7519.html#section-4
*/
class JsonWebTokenClaims {
/**
* Instantiates a new JSON Web Token Claims for usage with JSON Web Tokens.
*
* @param claims Defines the Claims of the JSON Web Token.
* @param options Validation options for the JSON Web Token Claims.
*/
constructor(claims, options = {}) {
if (claims instanceof JsonWebTokenClaims) {
return claims;
}
this.validateDefaultClaims(claims);
this.validateCustomClaims?.(claims);
this.validateClaimsOptions(claims, options);
Object.assign(this, claims);
}
/**
* Validates the Default JSON Web Token Claims based on the rules of
* {@link https://www.rfc-editor.org/rfc/rfc7519.html#section-4 RFC 7519 Section 4}.
*
* @param claims JSON Web Token Claims.
*/
validateDefaultClaims(claims) {
const now = Math.floor(Date.now() / 1000);
if (claims.iss !== undefined && typeof claims.iss !== 'string') {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "iss".');
}
if (claims.sub !== undefined && typeof claims.sub !== 'string') {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "sub".');
}
if (claims.aud !== undefined) {
if (typeof claims.aud !== 'string' && !Array.isArray(claims.aud)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "aud".');
}
if (Array.isArray(claims.aud) && claims.aud.some((aud) => typeof aud !== 'string')) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "aud".');
}
}
if (claims.exp !== undefined) {
if (typeof claims.exp !== 'number' || !Number.isInteger(claims.exp)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "exp".');
}
if (now > claims.exp) {
throw new expired_jsonwebtoken_exception_1.ExpiredJsonWebTokenException();
}
}
if (claims.nbf !== undefined) {
if (typeof claims.nbf !== 'number' || !Number.isInteger(claims.nbf)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "nbf".');
}
if (now < claims.nbf) {
throw new jsonwebtoken_not_valid_yet_exception_1.JsonWebTokenNotValidYetException();
}
}
if (claims.iat !== undefined && (typeof claims.iat !== 'number' || !Number.isInteger(claims.iat))) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "iat".');
}
if (claims.jti !== undefined && typeof claims.jti !== 'string') {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Invalid claim "jti".');
}
}
/**
* Validates the provided JSON Web Token Claims based on the provided Options.
*
* @param claims JSON Web Token Claims.
* @param options Dictionary used to validate the provided JSON Web Token Claims.
*/
validateClaimsOptions(claims, options) {
Object.entries(options).forEach(([claim, option]) => {
const claimValue = claims[claim];
if (option.essential === true && claimValue === undefined) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException(`Missing required claim "${claim}".`);
}
if (option.value !== undefined && !(0, util_1.isDeepStrictEqual)(claimValue, option.value)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException(`Mismatching expected value for claim "${claim}".`);
}
if (option.values !== undefined) {
if (!Array.isArray(option.values)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException('Expected an array for the option "values".');
}
if (option.values.length === 0) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException(`Mismatching expected value for claim "${claim}".`);
}
option.values.forEach((value) => {
if (!(0, util_1.isDeepStrictEqual)(value, claimValue)) {
throw new invalid_jsonwebtoken_claim_exception_1.InvalidJsonWebTokenClaimException(`Mismatching expected value for claim "${claim}". Received "${claimValue}".`);
}
});
}
});
}
/**
* Returns the Stringified JSON representation of the JSON Web Token Claims.
*/
toString() {
return JSON.stringify(this);
}
/**
* Returns the Buffer version of the Stringified JSON Web Token Claims.
*/
toBuffer() {
return Buffer.from(this.toString(), 'utf8');
}
}
exports.JsonWebTokenClaims = JsonWebTokenClaims;
//# sourceMappingURL=jsonwebtoken.claims.js.map