initial commit
This commit is contained in:
85
server/node_modules/jose/dist/node/cjs/jws/flattened/sign.js
generated
vendored
Normal file
85
server/node_modules/jose/dist/node/cjs/jws/flattened/sign.js
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FlattenedSign = void 0;
|
||||
const base64url_js_1 = require("../../runtime/base64url.js");
|
||||
const sign_js_1 = require("../../runtime/sign.js");
|
||||
const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
|
||||
const errors_js_1 = require("../../util/errors.js");
|
||||
const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
|
||||
const check_key_type_js_1 = require("../../lib/check_key_type.js");
|
||||
const validate_crit_js_1 = require("../../lib/validate_crit.js");
|
||||
class FlattenedSign {
|
||||
constructor(payload) {
|
||||
if (!(payload instanceof Uint8Array)) {
|
||||
throw new TypeError('payload must be an instance of Uint8Array');
|
||||
}
|
||||
this._payload = payload;
|
||||
}
|
||||
setProtectedHeader(protectedHeader) {
|
||||
if (this._protectedHeader) {
|
||||
throw new TypeError('setProtectedHeader can only be called once');
|
||||
}
|
||||
this._protectedHeader = protectedHeader;
|
||||
return this;
|
||||
}
|
||||
setUnprotectedHeader(unprotectedHeader) {
|
||||
if (this._unprotectedHeader) {
|
||||
throw new TypeError('setUnprotectedHeader can only be called once');
|
||||
}
|
||||
this._unprotectedHeader = unprotectedHeader;
|
||||
return this;
|
||||
}
|
||||
async sign(key, options) {
|
||||
if (!this._protectedHeader && !this._unprotectedHeader) {
|
||||
throw new errors_js_1.JWSInvalid('either setProtectedHeader or setUnprotectedHeader must be called before #sign()');
|
||||
}
|
||||
if (!(0, is_disjoint_js_1.default)(this._protectedHeader, this._unprotectedHeader)) {
|
||||
throw new errors_js_1.JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
||||
}
|
||||
const joseHeader = {
|
||||
...this._protectedHeader,
|
||||
...this._unprotectedHeader,
|
||||
};
|
||||
const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
|
||||
let b64 = true;
|
||||
if (extensions.has('b64')) {
|
||||
b64 = this._protectedHeader.b64;
|
||||
if (typeof b64 !== 'boolean') {
|
||||
throw new errors_js_1.JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
||||
}
|
||||
}
|
||||
const { alg } = joseHeader;
|
||||
if (typeof alg !== 'string' || !alg) {
|
||||
throw new errors_js_1.JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
||||
}
|
||||
(0, check_key_type_js_1.default)(alg, key, 'sign');
|
||||
let payload = this._payload;
|
||||
if (b64) {
|
||||
payload = buffer_utils_js_1.encoder.encode((0, base64url_js_1.encode)(payload));
|
||||
}
|
||||
let protectedHeader;
|
||||
if (this._protectedHeader) {
|
||||
protectedHeader = buffer_utils_js_1.encoder.encode((0, base64url_js_1.encode)(JSON.stringify(this._protectedHeader)));
|
||||
}
|
||||
else {
|
||||
protectedHeader = buffer_utils_js_1.encoder.encode('');
|
||||
}
|
||||
const data = (0, buffer_utils_js_1.concat)(protectedHeader, buffer_utils_js_1.encoder.encode('.'), payload);
|
||||
const signature = await (0, sign_js_1.default)(alg, key, data);
|
||||
const jws = {
|
||||
signature: (0, base64url_js_1.encode)(signature),
|
||||
payload: '',
|
||||
};
|
||||
if (b64) {
|
||||
jws.payload = buffer_utils_js_1.decoder.decode(payload);
|
||||
}
|
||||
if (this._unprotectedHeader) {
|
||||
jws.header = this._unprotectedHeader;
|
||||
}
|
||||
if (this._protectedHeader) {
|
||||
jws.protected = buffer_utils_js_1.decoder.decode(protectedHeader);
|
||||
}
|
||||
return jws;
|
||||
}
|
||||
}
|
||||
exports.FlattenedSign = FlattenedSign;
|
||||
119
server/node_modules/jose/dist/node/cjs/jws/flattened/verify.js
generated
vendored
Normal file
119
server/node_modules/jose/dist/node/cjs/jws/flattened/verify.js
generated
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.flattenedVerify = void 0;
|
||||
const base64url_js_1 = require("../../runtime/base64url.js");
|
||||
const verify_js_1 = require("../../runtime/verify.js");
|
||||
const errors_js_1 = require("../../util/errors.js");
|
||||
const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
|
||||
const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
|
||||
const is_object_js_1 = require("../../lib/is_object.js");
|
||||
const check_key_type_js_1 = require("../../lib/check_key_type.js");
|
||||
const validate_crit_js_1 = require("../../lib/validate_crit.js");
|
||||
const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
|
||||
async function flattenedVerify(jws, key, options) {
|
||||
var _a;
|
||||
if (!(0, is_object_js_1.default)(jws)) {
|
||||
throw new errors_js_1.JWSInvalid('Flattened JWS must be an object');
|
||||
}
|
||||
if (jws.protected === undefined && jws.header === undefined) {
|
||||
throw new errors_js_1.JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
|
||||
}
|
||||
if (jws.protected !== undefined && typeof jws.protected !== 'string') {
|
||||
throw new errors_js_1.JWSInvalid('JWS Protected Header incorrect type');
|
||||
}
|
||||
if (jws.payload === undefined) {
|
||||
throw new errors_js_1.JWSInvalid('JWS Payload missing');
|
||||
}
|
||||
if (typeof jws.signature !== 'string') {
|
||||
throw new errors_js_1.JWSInvalid('JWS Signature missing or incorrect type');
|
||||
}
|
||||
if (jws.header !== undefined && !(0, is_object_js_1.default)(jws.header)) {
|
||||
throw new errors_js_1.JWSInvalid('JWS Unprotected Header incorrect type');
|
||||
}
|
||||
let parsedProt = {};
|
||||
if (jws.protected) {
|
||||
try {
|
||||
const protectedHeader = (0, base64url_js_1.decode)(jws.protected);
|
||||
parsedProt = JSON.parse(buffer_utils_js_1.decoder.decode(protectedHeader));
|
||||
}
|
||||
catch {
|
||||
throw new errors_js_1.JWSInvalid('JWS Protected Header is invalid');
|
||||
}
|
||||
}
|
||||
if (!(0, is_disjoint_js_1.default)(parsedProt, jws.header)) {
|
||||
throw new errors_js_1.JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
||||
}
|
||||
const joseHeader = {
|
||||
...parsedProt,
|
||||
...jws.header,
|
||||
};
|
||||
const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
|
||||
let b64 = true;
|
||||
if (extensions.has('b64')) {
|
||||
b64 = parsedProt.b64;
|
||||
if (typeof b64 !== 'boolean') {
|
||||
throw new errors_js_1.JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
||||
}
|
||||
}
|
||||
const { alg } = joseHeader;
|
||||
if (typeof alg !== 'string' || !alg) {
|
||||
throw new errors_js_1.JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
||||
}
|
||||
const algorithms = options && (0, validate_algorithms_js_1.default)('algorithms', options.algorithms);
|
||||
if (algorithms && !algorithms.has(alg)) {
|
||||
throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
|
||||
}
|
||||
if (b64) {
|
||||
if (typeof jws.payload !== 'string') {
|
||||
throw new errors_js_1.JWSInvalid('JWS Payload must be a string');
|
||||
}
|
||||
}
|
||||
else if (typeof jws.payload !== 'string' && !(jws.payload instanceof Uint8Array)) {
|
||||
throw new errors_js_1.JWSInvalid('JWS Payload must be a string or an Uint8Array instance');
|
||||
}
|
||||
let resolvedKey = false;
|
||||
if (typeof key === 'function') {
|
||||
key = await key(parsedProt, jws);
|
||||
resolvedKey = true;
|
||||
}
|
||||
(0, check_key_type_js_1.default)(alg, key, 'verify');
|
||||
const data = (0, buffer_utils_js_1.concat)(buffer_utils_js_1.encoder.encode((_a = jws.protected) !== null && _a !== void 0 ? _a : ''), buffer_utils_js_1.encoder.encode('.'), typeof jws.payload === 'string' ? buffer_utils_js_1.encoder.encode(jws.payload) : jws.payload);
|
||||
let signature;
|
||||
try {
|
||||
signature = (0, base64url_js_1.decode)(jws.signature);
|
||||
}
|
||||
catch {
|
||||
throw new errors_js_1.JWSInvalid('Failed to base64url decode the signature');
|
||||
}
|
||||
const verified = await (0, verify_js_1.default)(alg, key, signature, data);
|
||||
if (!verified) {
|
||||
throw new errors_js_1.JWSSignatureVerificationFailed();
|
||||
}
|
||||
let payload;
|
||||
if (b64) {
|
||||
try {
|
||||
payload = (0, base64url_js_1.decode)(jws.payload);
|
||||
}
|
||||
catch {
|
||||
throw new errors_js_1.JWSInvalid('Failed to base64url decode the payload');
|
||||
}
|
||||
}
|
||||
else if (typeof jws.payload === 'string') {
|
||||
payload = buffer_utils_js_1.encoder.encode(jws.payload);
|
||||
}
|
||||
else {
|
||||
payload = jws.payload;
|
||||
}
|
||||
const result = { payload };
|
||||
if (jws.protected !== undefined) {
|
||||
result.protectedHeader = parsedProt;
|
||||
}
|
||||
if (jws.header !== undefined) {
|
||||
result.unprotectedHeader = jws.header;
|
||||
}
|
||||
if (resolvedKey) {
|
||||
return { ...result, key };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
exports.flattenedVerify = flattenedVerify;
|
||||
Reference in New Issue
Block a user