import { c as createCommonjsModule, a as commonjsGlobal } from './_commonjsHelpers-7b8ed50b.js';

var TextEncoder_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
class TextEncoder {
    constructor() {
        this.encoding = "utf-8";
    }
    encode(data) {
        return Uint8Array.from(unescape(encodeURIComponent(data)).split(""), c => c.charCodeAt(0));
    }
}
exports.TextEncoder = TextEncoder;

});

var Base64 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

const tables = {
    standard: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    url: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
};
function encode(value, standard = "standard", padding = "") {
    if (typeof value == "string")
        value = new TextEncoder_1.TextEncoder().encode(value);
    const table = tables[standard];
    const result = [];
    for (let c = 0; c < value.length; c += 3) {
        const c0 = value[c];
        const c1 = c + 1 < value.length ? value[c + 1] : 0;
        const c2 = c + 2 < value.length ? value[c + 2] : 0;
        result.push(table[c0 >>> 2]);
        result.push(table[((c0 & 3) << 4) | (c1 >>> 4)]);
        result.push(table[((c1 & 15) << 2) | (c2 >>> 6)]);
        result.push(table[c2 & 63]);
    }
    const length = Math.ceil((value.length / 3) * 4);
    return result.join("").substr(0, length) + padding.repeat(result.length - length);
}
exports.encode = encode;
function decode(value, standard = "standard") {
    while (value.endsWith("=") && value.length > 0)
        value = value.substr(0, value.length - 1);
    const table = tables[standard];
    const data = value.split("").map(c => table.indexOf(c));
    const result = new Uint8Array(Math.floor((data.length / 4) * 3));
    for (let c = 0; c < result.length; c += 3) {
        const d0 = data.shift() || 0;
        const d1 = data.shift() || 0;
        const d2 = data.shift() || 0;
        const d3 = data.shift() || 0;
        result[c] = (d0 << 2) | (d1 >>> 4);
        result[c + 1] = ((d1 & 15) << 4) | (d2 >>> 2);
        result[c + 2] = ((d2 & 3) << 6) | d3;
    }
    return result;
}
exports.decode = decode;

});

var crypto_browser = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.crypto = (window || self).crypto || (window || self).msCrypto;

});

var TextDecoder_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
class TextDecoder {
    constructor() {
        this.encoding = "utf-8";
    }
    decode(view, options) {
        return !view
            ? ""
            : decodeURIComponent(escape(Array.from(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), c => String.fromCharCode(c)).join("")));
    }
}
exports.TextDecoder = TextDecoder;

});

var Algorithm_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });




class Algorithm {
    constructor(key) {
        this.key = key;
    }
    encrypt(data, salt) {
        return __awaiter(this, void 0, void 0, function* () {
            const iv = salt ? Base64.decode(salt, "url") : crypto_browser.crypto.getRandomValues(new Uint8Array(16));
            return {
                salt: salt || Base64.encode(iv, "url"),
                value: Base64.encode(new Uint8Array(yield crypto_browser.crypto.subtle.encrypt({ name: (yield this.key).algorithm.name, iv }, yield this.key, new TextEncoder_1.TextEncoder().encode(data))), "url"),
            };
        });
    }
    decrypt(encrypted) {
        return __awaiter(this, void 0, void 0, function* () {
            return new TextDecoder_1.TextDecoder().decode(new Uint8Array(yield crypto_browser.crypto.subtle.decrypt({ name: (yield this.key).algorithm.name, iv: Base64.decode(encrypted.salt, "url") }, yield this.key, Base64.decode(encrypted.value, "url"))));
        });
    }
    export(parts = 1) {
        return __awaiter(this, void 0, void 0, function* () {
            const key = new Uint8Array(yield crypto_browser.crypto.subtle.exportKey("raw", yield this.key));
            let result = Algorithm.generateRandomKeys(key.length, parts - 1);
            result = [Algorithm.reduceKeys([key, ...result]), ...result];
            return result.length == 1 ? Base64.encode(result[0], "url") : result.map(r => Base64.encode(r, "url"));
        });
    }
    static aesCbc(key) {
        return new Algorithm(typeof key == "number"
            ? crypto_browser.crypto.subtle.generateKey({ name: "AES-CBC", length: key }, true, ["encrypt", "decrypt"])
            : crypto_browser.crypto.subtle.importKey("raw", Array.isArray(key)
                ? Algorithm.reduceKeys(key.map(k => Base64.decode(k, "url")))
                : Base64.decode(key, "url"), "AES-CBC", true, ["encrypt", "decrypt"]));
    }
    static generateRandomKeys(length, parts) {
        return parts > 0
            ? [crypto_browser.crypto.getRandomValues(new Uint8Array(length)), ...this.generateRandomKeys(length, parts - 1)]
            : [];
    }
    static reduceKeys(keys) {
        const result = new Uint8Array(keys[0].length);
        for (let index = 0; index < keys[0].length; index++)
            result[index] = keys.reduce((p, c) => p ^ c[index], 0);
        return result;
    }
}
exports.Algorithm = Algorithm;

});

var Base16 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

function encode(value, length) {
    const data = typeof value == "string" ? new TextEncoder_1.TextEncoder().encode(value) : value;
    let result = [];
    for (const d of data)
        result.push(Math.floor(d / 16).toString(16), (d % 16).toString(16));
    if (length)
        result = result.slice(0, length);
    return result.join("");
}
exports.encode = encode;
function decode(value) {
    if (value.length % 2 == 1)
        value += "0";
    const result = new Uint8Array(value.length / 2);
    for (let index = 0; index < result.length; index++)
        result[index] = Number.parseInt(value[index * 2], 16) * 16 + Number.parseInt(value[index * 2 + 1], 16);
    return result;
}
exports.decode = decode;

});

var Digest_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });




class Digest {
    constructor(algorithm) {
        this.algorithm = algorithm;
    }
    get length() {
        return Digest.lengths[this.algorithm];
    }
    digest(data, base = "standard") {
        return __awaiter(this, void 0, void 0, function* () {
            let result;
            if (typeof data == "string") {
                const digest = yield this.digest(new TextEncoder_1.TextEncoder().encode(data));
                result = base == 16 ? Base16.encode(digest) : Base64.encode(digest, base);
            }
            else
                result = new Uint8Array(yield crypto_browser.crypto.subtle.digest(this.algorithm, data));
            return result;
        });
    }
}
exports.Digest = Digest;
Digest.lengths = {
    "SHA-1": 128,
    "SHA-256": 256,
    "SHA-384": 384,
    "SHA-512": 512,
};

});

var Encrypted_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
var Encrypted;
(function (Encrypted) {
    function is(value) {
        return (typeof value == "object" &&
            (value.key == undefined || typeof value.key == "string") &&
            typeof value.value == "string" &&
            typeof value.salt == "string");
    }
    Encrypted.is = is;
})(Encrypted = exports.Encrypted || (exports.Encrypted = {}));

});

var Identifier_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });


var Identifier;
(function (Identifier) {
    function is(value, length) {
        return (typeof value == "string" &&
            (length == undefined || value.length == length) &&
            Array.from(value).every(c => (c >= "0" && c <= "9") || (c >= "A" && c <= "Z") || (c >= "a" && c <= "z") || c == "-" || c == "_"));
    }
    Identifier.is = is;
    function fromBinary(identifier) {
        return Base64.encode(identifier, "url");
    }
    Identifier.fromBinary = fromBinary;
    function toBinary(identifier) {
        return Base64.decode(identifier, "url");
    }
    Identifier.toBinary = toBinary;
    function generate(length) {
        return fromBinary(crypto_browser.crypto.getRandomValues(new Uint8Array((length / 4) * 3)));
    }
    Identifier.generate = generate;
    function fromHexadecimal(identifier) {
        if (identifier.length % 2 == 1)
            identifier += "0";
        const result = new Uint8Array(identifier.length / 2);
        for (let index = 0; index < result.length; index++)
            result[index] = Number.parseInt(identifier[index * 2], 16) * 16 + Number.parseInt(identifier[index * 2 + 1], 16);
        return fromBinary(result);
    }
    Identifier.fromHexadecimal = fromHexadecimal;
    function toHexadecimal(identifier, length) {
        const data = Base64.decode(identifier, "url");
        let result = [];
        for (const d of data)
            result.push(Math.floor(d / 16).toString(16), (d % 16).toString(16));
        if (length)
            result = result.slice(0, length);
        return result.join("");
    }
    Identifier.toHexadecimal = toHexadecimal;
    Identifier.length = [
        4,
        8,
        12,
        16,
        20,
        24,
        28,
        32,
        36,
        40,
        44,
        48,
        52,
        56,
        60,
        64,
        68,
        72,
        76,
        80,
        84,
        88,
        92,
        96,
        100,
        104,
        108,
        112,
        116,
        120,
        124,
        128,
    ];
    let Length;
    (function (Length) {
        function is(value) {
            return typeof value == "number" && value >= 4 && value <= 128 && (value & 252) == value;
        }
        Length.is = is;
    })(Length = Identifier.Length || (Identifier.Length = {}));
})(Identifier = exports.Identifier || (exports.Identifier = {}));

});

var Password_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });


var Password;
(function (Password) {
    function is(value) {
        return (typeof value == "string" ||
            (typeof value == "object" && typeof value.hash == "string" && typeof value.salt == "string"));
    }
    Password.is = is;
    function hash(algorithm, password, salt) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!salt)
                salt = Base64.encode(crypto_browser.crypto.getRandomValues(new Uint8Array(64)));
            return {
                hash: yield algorithm.sign(salt + password),
                salt,
            };
        });
    }
    Password.hash = hash;
    function verify(algorithm, hash, password) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield Password.hash(algorithm, password, hash.salt)).hash == hash.hash;
        });
    }
    Password.verify = verify;
    let Hashed;
    (function (Hashed) {
        function is(value) {
            return typeof value == "object" && typeof value.hash == "string" && typeof value.salt == "string";
        }
        Hashed.is = is;
    })(Hashed = Password.Hashed || (Password.Hashed = {}));
})(Password = exports.Password || (exports.Password = {}));

});

var RandomValue_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

var RandomValue;
(function (RandomValue) {
    function generate(array) {
        return crypto_browser.crypto.getRandomValues(array);
    }
    RandomValue.generate = generate;
})(RandomValue = exports.RandomValue || (exports.RandomValue = {}));

});

var Algorithm_1$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
var Algorithm;
(function (Algorithm) {
    function is(value) {
        return value == "HMAC" || value == "RSA" || value == "ECDSA" || value == "RSA-PSS";
    }
    Algorithm.is = is;
})(Algorithm = exports.Algorithm || (exports.Algorithm = {}));

});

var Base_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });


class Base {
    sign(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return typeof data == "string"
                ? Base64.encode(yield this.signBinary(new TextEncoder_1.TextEncoder().encode(data)), "url")
                : this.signBinary(data);
        });
    }
    verify(data, signature) {
        if (typeof signature == "string")
            signature = Base64.decode(signature, "url");
        return typeof data == "string"
            ? this.verifyBinary(new TextEncoder_1.TextEncoder().encode(data), signature)
            : this.verifyBinary(data, signature);
    }
}
exports.Base = Base;

});

var ECDSA_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });



class ECDSA extends Base_1.Base {
    constructor(hash, publicKey, privateKey) {
        super();
        this.hash = hash;
        if (publicKey) {
            if (typeof publicKey == "string")
                publicKey = Base64.decode(publicKey);
            this.publicKey = crypto_browser.crypto.subtle.importKey("spki", publicKey, { name: "ECDSA", namedCurve: hash.replace("SHA", "P") == "P-512" ? "P-521" : hash.replace("SHA", "P") }, false, ["verify"]);
        }
        if (privateKey) {
            if (typeof privateKey == "string")
                privateKey = Base64.decode(privateKey);
            this.privateKey = crypto_browser.crypto.subtle.importKey("pkcs8", privateKey, { name: "ECDSA", namedCurve: hash.replace("SHA", "P") == "P-512" ? "P-521" : hash.replace("SHA", "P") }, true, ["sign", "verify"]);
        }
    }
    signBinary(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Uint8Array(yield crypto_browser.crypto.subtle.sign({ name: "ECDSA", hash: { name: this.hash } }, yield this.privateKey, data));
        });
    }
    verifyBinary(data, signature) {
        return __awaiter(this, void 0, void 0, function* () {
            return crypto_browser.crypto.subtle.verify({ name: "ECDSA", hash: { name: this.hash } }, yield this.publicKey, signature, data);
        });
    }
}
exports.ECDSA = ECDSA;

});

var Hash_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
var Hash;
(function (Hash) {
    function is(value) {
        return value == "SHA-1" || value == "SHA-256" || value == "SHA-384" || value == "SHA-512";
    }
    Hash.is = is;
})(Hash = exports.Hash || (exports.Hash = {}));

});

var Symmetric_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });


class Symmetric extends Base_1.Base {
    verifyBinary(data, signature) {
        return __awaiter(this, void 0, void 0, function* () {
            return Base64.encode(yield this.signBinary(data), "url") == Base64.encode(signature, "url");
        });
    }
}
exports.Symmetric = Symmetric;

});

var HMAC_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });



class HMAC extends Symmetric_1.Symmetric {
    constructor(hash, key) {
        super();
        this.hash = hash;
        if (typeof key == "string")
            key = Base64.decode(key, "url");
        this.key = crypto_browser.crypto.subtle.importKey("raw", key, { name: "HMAC", hash: { name: hash } }, false, ["sign", "verify"]);
    }
    signBinary(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Uint8Array(yield crypto_browser.crypto.subtle.sign("HMAC", yield this.key, data));
        });
    }
}
exports.HMAC = HMAC;

});

var None_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

class None extends Symmetric_1.Symmetric {
    signBinary(_) {
        return Promise.resolve(new Uint8Array(0));
    }
}
exports.None = None;

});

var RSA_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });



class RSA extends Base_1.Base {
    constructor(hash, publicKey, privateKey) {
        super();
        this.hash = hash;
        if (publicKey) {
            if (typeof publicKey == "string")
                publicKey = Base64.decode(publicKey);
            this.publicKey = crypto_browser.crypto.subtle.importKey("spki", publicKey, { name: "RSASSA-PKCS1-v1_5", hash: { name: hash } }, false, ["verify"]);
        }
        if (privateKey) {
            if (typeof privateKey == "string")
                privateKey = Base64.decode(privateKey);
            this.privateKey = crypto_browser.crypto.subtle.importKey("pkcs8", privateKey, { name: "RSASSA-PKCS1-v1_5", hash: { name: hash } }, true, ["sign", "verify"]);
        }
    }
    signBinary(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Uint8Array(yield crypto_browser.crypto.subtle.sign("RSASSA-PKCS1-v1_5", yield this.privateKey, data));
        });
    }
    verifyBinary(data, signature) {
        return __awaiter(this, void 0, void 0, function* () {
            return crypto_browser.crypto.subtle.verify("RSASSA-PKCS1-v1_5", yield this.publicKey, signature, data);
        });
    }
}
exports.RSA = RSA;

});

var RSAPSS_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });



class RSAPSS extends Base_1.Base {
    constructor(hash, publicKey, privateKey) {
        super();
        this.hash = hash;
        if (publicKey) {
            if (typeof publicKey == "string")
                publicKey = Base64.decode(publicKey);
            this.publicKey = crypto_browser.crypto.subtle.importKey("spki", publicKey, { name: "RSA-PSS", hash: { name: hash } }, false, [
                "verify",
            ]);
        }
        if (privateKey) {
            if (typeof privateKey == "string")
                privateKey = Base64.decode(privateKey);
            this.privateKey = crypto_browser.crypto.subtle.importKey("pkcs8", privateKey, { name: "RSA-PSS", hash: { name: hash } }, true, [
                "sign",
                "verify",
            ]);
        }
    }
    signBinary(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Uint8Array(yield crypto_browser.crypto.subtle.sign({ name: "RSA-PSS", saltLength: 128 }, yield this.privateKey, data));
        });
    }
    verifyBinary(data, signature) {
        return __awaiter(this, void 0, void 0, function* () {
            return crypto_browser.crypto.subtle.verify({ name: "RSA-PSS", saltLength: 128 }, yield this.publicKey, signature, data);
        });
    }
}
exports.RSAPSS = RSAPSS;

});

var Signer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });







var Signer;
(function (Signer) {
    let Algorithm;
    (function (Algorithm) {
        Algorithm.is = Algorithm_1$1.Algorithm.is;
    })(Algorithm = Signer.Algorithm || (Signer.Algorithm = {}));
    let Hash;
    (function (Hash) {
        Hash.is = Hash_1.Hash.is;
    })(Hash = Signer.Hash || (Signer.Hash = {}));
    function create(algorithm, hash, ...keys) {
        let result;
        switch (algorithm) {
            case "HMAC":
                if (hash != undefined)
                    result = new HMAC_1.HMAC(hash, keys[0]);
                break;
            case "RSA":
                if (hash != undefined)
                    result = new RSA_1.RSA(hash, keys[0], keys[1]);
                break;
            case "RSA-PSS":
                if (hash != undefined)
                    result = new RSAPSS_1.RSAPSS(hash, keys[0], keys[1]);
                break;
            case "ECDSA":
                if (hash != undefined)
                    result = new ECDSA_1.ECDSA(hash, keys[0], keys[1]);
                break;
            case "None":
                result = new None_1.None();
        }
        return result;
    }
    Signer.create = create;
})(Signer = exports.Signer || (exports.Signer = {}));

});

var dist = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

exports.Algorithm = Algorithm_1.Algorithm;

exports.Base64 = Base64;

exports.Base16 = Base16;

exports.Digest = Digest_1.Digest;

exports.Encrypted = Encrypted_1.Encrypted;

exports.Identifier = Identifier_1.Identifier;

exports.Password = Password_1.Password;

exports.RandomValue = RandomValue_1.RandomValue;

exports.Signer = Signer_1.Signer;

exports.TextDecoder = TextDecoder_1.TextDecoder;

exports.TextEncoder = TextEncoder_1.TextEncoder;

});

if (!Uint8Array.__proto__.from) {
    (() => {
        Uint8Array.__proto__.from = function (object, theFunction, thisObject) {
            const typedArrayClass = Uint8Array.__proto__;
            if (typeof this !== "function") {
                throw new TypeError("# is not a constructor");
            }
            if (this.__proto__ !== typedArrayClass) {
                throw new TypeError("this is not a typed array.");
            }
            theFunction = theFunction || (element => element);
            if (typeof theFunction !== "function") {
                throw new TypeError("specified argument is not a function");
            }
            object = Object(object);
            if (!object.length) {
                return new this(0);
            }
            let copyData = [];
            for (const data of copyData) {
                copyData.push(data);
            }
            copyData = copyData.map(theFunction, thisObject);
            const typedArray = new this(copyData.length);
            for (let i = 0; i < typedArray.length; i++) {
                typedArray[i] = copyData[i];
            }
            return typedArray;
        };
    })();
}

var shim = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });


});

var Converter_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Converter = void 0;
class Converter {
    constructor(conversionMap) {
        this.conversionMap = conversionMap;
    }
    apply(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return payload && this.convert(payload, true);
        });
    }
    reverse(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return payload && this.convert(payload, false);
        });
    }
    convert(payload, forward) {
        return __awaiter(this, void 0, void 0, function* () {
            for (const key in this.conversionMap) {
                const fnction = forward ? this.conversionMap[key].forward : this.conversionMap[key].backward;
                const property = key.split(".");
                payload = yield this.convertProperty(payload, property, fnction);
            }
            return payload;
        });
    }
    convertProperty(payload, property, fnction) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = Object.assign({}, payload);
            if (result[property[0]])
                result[property[0]] =
                    property.length == 1
                        ? yield fnction(result[property[0]])
                        : yield this.convertProperty(result[property[0]], property.slice(1), fnction);
            return result;
        });
    }
}
exports.Converter = Converter;

});

var Converter_1$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Converter = void 0;
var Converter;
(function (Converter) {
    function is(value) {
        return typeof value == "object" && Object.entries(value).every(v => typeof v == "function");
    }
    Converter.is = is;
})(Converter = exports.Converter || (exports.Converter = {}));

});

var Crypto_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Crypto = void 0;
var Crypto;
(function (Crypto) {
    function is(value) {
        return Array.isArray(value) && value.every(v => typeof v == "string");
    }
    Crypto.is = is;
})(Crypto = exports.Crypto || (exports.Crypto = {}));

});

var Renamer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Renamer = void 0;
var Renamer;
(function (Renamer) {
    function is(value) {
        return typeof value == "object" && Object.entries(value).every(thing => typeof thing[1] == "string");
    }
    Renamer.is = is;
})(Renamer = exports.Renamer || (exports.Renamer = {}));

});

var Creatable_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Creatable = void 0;



var Creatable;
(function (Creatable) {
    function is(value) {
        return Crypto_1.Crypto.is(value) || Converter_1$1.Converter.is(value) || Renamer_1.Renamer.is(value);
    }
    Creatable.is = is;
    let Converter;
    (function (Converter) {
        Converter.is = Converter_1$1.Converter.is;
    })(Converter = Creatable.Converter || (Creatable.Converter = {}));
    let Crypto;
    (function (Crypto) {
        Crypto.is = Crypto_1.Crypto.is;
    })(Crypto = Creatable.Crypto || (Creatable.Crypto = {}));
    let Renamer;
    (function (Renamer) {
        Renamer.is = Renamer_1.Renamer.is;
    })(Renamer = Creatable.Renamer || (Creatable.Renamer = {}));
})(Creatable = exports.Creatable || (exports.Creatable = {}));

});

var Remover_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Remover = void 0;
class Remover {
    constructor(toRemove) {
        this.toRemove = toRemove;
    }
    apply(payload) {
        return payload && this.removeFrom(payload);
    }
    reverse(payload) {
        return payload && this.removeFrom(payload);
    }
    removeFrom(payload) {
        let result = Object.assign({}, payload);
        this.toRemove.forEach(str => {
            const property = str.split(".");
            result = this.removeProperty(result, property);
        });
        return result;
    }
    removeProperty(payload, property) {
        const result = Object.assign({}, payload);
        if (result[property[0]])
            if (property.length == 1)
                delete result[property[0]];
            else
                result[property[0]] = this.removeProperty(result[property[0]], property.slice(1));
        return result;
    }
    static create(toRemove) {
        return toRemove.length == 0 ? undefined : new Remover(toRemove);
    }
}
exports.Remover = Remover;

});

var Crypto_1$1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Crypto = void 0;


class Crypto {
    constructor(secret, ...properties) {
        this.secret = secret;
        this.encoder = new dist.TextEncoder();
        this.decoder = new dist.TextDecoder();
        this.properties = properties.map(p => p.split("."));
    }
    apply(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return (payload &&
                this.process(payload, value => this.encoder.encode(JSON.stringify(value)), value => dist.Base64.encode(value, "url")));
        });
    }
    reverse(payload) {
        return __awaiter(this, void 0, void 0, function* () {
            return (payload &&
                this.process(payload, value => (typeof value == "string" ? dist.Base64.decode(value, "url") : new Uint8Array()), value => JSON.parse(this.decoder.decode(value))));
        });
    }
    process(payload, preprocess, postprocess) {
        return __awaiter(this, void 0, void 0, function* () {
            const secret = this.secret + payload.sub + payload.iat;
            for (const property of this.properties)
                payload = yield this.processProperty(payload, property, secret + property.join("."), preprocess, postprocess);
            return payload;
        });
    }
    processProperty(payload, property, secret, preprocess, postprocess) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = Object.assign({}, payload);
            if (result[property[0]])
                if (property.length == 1) {
                    const data = preprocess(payload[property[0]]);
                    const key = yield new dist.Digest("SHA-512").digest(this.encoder.encode(secret));
                    const processed = new Uint8Array(data.length);
                    for (let index = 0; index < data.length; index++)
                        processed[index] = data[index] ^ key[index];
                    result[property[0]] = postprocess(processed);
                }
                else
                    result[property[0]] = yield this.processProperty(result[property[0]], property.slice(1), secret, preprocess, postprocess);
            return result;
        });
    }
    static create(secret, ...properties) {
        return secret ? new Crypto(secret, ...properties) : Remover_1.Remover.create(properties);
    }
}
exports.Crypto = Crypto;

});

var Renamer_1$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Renamer = void 0;
class Renamer {
    constructor(forwardTransformMap) {
        this.forwardTransformMap = forwardTransformMap;
        this.createBackwardTransformMap();
    }
    createBackwardTransformMap() {
        const result = {};
        for (const key in this.forwardTransformMap) {
            result[this.forwardTransformMap[key]] = key;
        }
        this.backwardTransformMap = result ? result : {};
    }
    apply(payload) {
        return payload && this.remap(payload, true);
    }
    reverse(payload) {
        return payload && this.remap(payload, false);
    }
    remap(payload, forward) {
        const transformMap = forward ? this.forwardTransformMap : this.backwardTransformMap;
        const result = {};
        for (const key in payload)
            if (key in transformMap)
                result[transformMap[key]] = this.resolve(payload[key], forward);
            else
                result[key] = this.resolve(payload[key], forward);
        return result;
    }
    resolve(payload, forward) {
        let result;
        if (Array.isArray(payload)) {
            result = [];
            payload.forEach(value => result.push(this.resolve(value, forward)));
        }
        else
            result = typeof payload == "object" ? this.remap(payload, forward) : payload;
        return result;
    }
}
exports.Renamer = Renamer;

});

var Transformer_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transformer = void 0;
var Transformer;
(function (Transformer) {
    function is(value) {
        return typeof value == "object" && typeof value.apply == "function" && typeof value.reverse == "function";
    }
    Transformer.is = is;
    function create(transformer) {
        return {
            apply: transformer.apply ? transformer.apply : (v) => v,
            reverse: transformer.reverse ? transformer.reverse : (v) => v,
        };
    }
    Transformer.create = create;
})(Transformer = exports.Transformer || (exports.Transformer = {}));

});

var Typeguard_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Typeguard = void 0;
class Typeguard {
    constructor(...is) {
        this.is = is;
    }
    apply(payload) {
        return this.is.some(f => f(payload)) ? payload : undefined;
    }
    reverse(payload) {
        return this.is.some(f => f(payload)) ? payload : undefined;
    }
}
exports.Typeguard = Typeguard;

});

var Property = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Typeguard = exports.Transformer = exports.Renamer = exports.Remover = exports.Crypto = exports.Creatable = exports.Converter = void 0;

Object.defineProperty(exports, "Converter", { enumerable: true, get: function () { return Converter_1.Converter; } });

Object.defineProperty(exports, "Creatable", { enumerable: true, get: function () { return Creatable_1.Creatable; } });

Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return Crypto_1$1.Crypto; } });

Object.defineProperty(exports, "Remover", { enumerable: true, get: function () { return Remover_1.Remover; } });

Object.defineProperty(exports, "Renamer", { enumerable: true, get: function () { return Renamer_1$1.Renamer; } });

Object.defineProperty(exports, "Transformer", { enumerable: true, get: function () { return Transformer_1.Transformer; } });

Object.defineProperty(exports, "Typeguard", { enumerable: true, get: function () { return Typeguard_1.Typeguard; } });

});

var Actor_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Actor = void 0;

class Actor {
    constructor(id) {
        this.id = id;
        this.transformers = [];
    }
    add(...argument) {
        argument.forEach(value => value && this.transformers.push(Property.Creatable.is(value) ? this.creatableToTransformer(value) : value));
        return this;
    }
    creatableToTransformer(creatable) {
        return Property.Creatable.Converter.is(creatable)
            ? new Property.Converter(creatable)
            : Property.Creatable.Crypto.is(creatable)
                ? Property.Crypto.create(creatable[0], ...creatable.slice(1))
                : new Property.Renamer(creatable);
    }
}
exports.Actor = Actor;

});

var Symmetric_1$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Symmetric = void 0;
var Symmetric;
(function (Symmetric) {
    function is(value) {
        return value == "HS256" || value == "HS384" || value == "HS512";
    }
    Symmetric.is = is;
})(Symmetric = exports.Symmetric || (exports.Symmetric = {}));

});

var Asymmetric_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Asymmetric = void 0;
var Asymmetric;
(function (Asymmetric) {
    function is(value) {
        return (value == "RS256" ||
            value == "RS384" ||
            value == "RS512" ||
            value == "ES256" ||
            value == "ES384" ||
            value == "ES512" ||
            value == "PS256" ||
            value == "PS384" ||
            value == "PS512");
    }
    Asymmetric.is = is;
})(Asymmetric = exports.Asymmetric || (exports.Asymmetric = {}));

});

var Name_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Name = void 0;


var Name;
(function (Name) {
    function is(value) {
        return value == "none" || Symmetric_1$1.Symmetric.is(value) || Asymmetric_1.Asymmetric.is(value);
    }
    Name.is = is;
    let Symmetric;
    (function (Symmetric) {
        Symmetric.is = Symmetric_1$1.Symmetric.is;
    })(Symmetric = Name.Symmetric || (Name.Symmetric = {}));
    let Asymmetric;
    (function (Asymmetric) {
        Asymmetric.is = Asymmetric_1.Asymmetric.is;
    })(Asymmetric = Name.Asymmetric || (Name.Asymmetric = {}));
})(Name = exports.Name || (exports.Name = {}));

});

var Algorithm_1$2 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Algorithm = void 0;


class Algorithm {
    constructor(name, signer) {
        this.name = name;
        this.signer = signer;
    }
    sign(data) {
        return __awaiter(this, void 0, void 0, function* () {
            return typeof data == "string" ? this.signer.sign(data) : this.signer.sign(data);
        });
    }
    verify(data, signature) {
        return this.signer.verify(data, signature);
    }
    static create(name, ...keys) {
        let result;
        switch (name) {
            case "ES256":
                result = dist.Signer.create("ECDSA", "SHA-256", keys[0], keys[1]);
                break;
            case "ES384":
                result = dist.Signer.create("ECDSA", "SHA-384", keys[0], keys[1]);
                break;
            case "ES512":
                result = dist.Signer.create("ECDSA", "SHA-512", keys[0], keys[1]);
                break;
            case "PS256":
                result = dist.Signer.create("RSA-PSS", "SHA-256", keys[0], keys[1]);
                break;
            case "PS384":
                result = dist.Signer.create("RSA-PSS", "SHA-384", keys[0], keys[1]);
                break;
            case "PS512":
                result = dist.Signer.create("RSA-PSS", "SHA-512", keys[0], keys[1]);
                break;
            case "HS256":
                result = dist.Signer.create("HMAC", "SHA-256", keys[0]);
                break;
            case "HS384":
                result = dist.Signer.create("HMAC", "SHA-384", keys[0]);
                break;
            case "HS512":
                result = dist.Signer.create("HMAC", "SHA-512", keys[0]);
                break;
            case "RS256":
                result = dist.Signer.create("RSA", "SHA-256", keys[0], keys[1]);
                break;
            case "RS384":
                result = dist.Signer.create("RSA", "SHA-384", keys[0], keys[1]);
                break;
            case "RS512":
                result = dist.Signer.create("RSA", "SHA-512", keys[0], keys[1]);
                break;
            case "none":
                result = dist.Signer.create("None");
                break;
        }
        return result && new Algorithm(name, result);
    }
    static none() {
        return Algorithm.create("none");
    }
    static HS256(key) {
        return Algorithm.create("HS256", key);
    }
    static HS384(key) {
        return Algorithm.create("HS384", key);
    }
    static HS512(key) {
        return Algorithm.create("HS512", key);
    }
    static RS256(publicKey, privateKey) {
        return Algorithm.create("RS256", publicKey, privateKey);
    }
    static RS384(publicKey, privateKey) {
        return Algorithm.create("RS384", publicKey, privateKey);
    }
    static RS512(publicKey, privateKey) {
        return Algorithm.create("RS512", publicKey, privateKey);
    }
    static ES256(publicKey, privateKey) {
        return Algorithm.create("ES256", publicKey, privateKey);
    }
    static ES384(publicKey, privateKey) {
        return Algorithm.create("ES384", publicKey, privateKey);
    }
    static ES512(publicKey, privateKey) {
        return Algorithm.create("ES512", publicKey, privateKey);
    }
    static PS256(publicKey, privateKey) {
        return Algorithm.create("PS256", publicKey, privateKey);
    }
    static PS384(publicKey, privateKey) {
        return Algorithm.create("PS384", publicKey, privateKey);
    }
    static PS512(publicKey, privateKey) {
        return Algorithm.create("PS512", publicKey, privateKey);
    }
}
exports.Algorithm = Algorithm;
(function (Algorithm) {
    let Name;
    (function (Name) {
        Name.is = Name_1.Name.is;
        let Symmetric;
        (function (Symmetric) {
            Symmetric.is = Name_1.Name.Symmetric.is;
        })(Symmetric = Name.Symmetric || (Name.Symmetric = {}));
        let Asymmetric;
        (function (Asymmetric) {
            Asymmetric.is = Name_1.Name.Asymmetric.is;
        })(Asymmetric = Name.Asymmetric || (Name.Asymmetric = {}));
    })(Name = Algorithm.Name || (Algorithm.Name = {}));
})(Algorithm = exports.Algorithm || (exports.Algorithm = {}));

});

var Issuer_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Issuer = void 0;


class Issuer extends Actor_1.Actor {
    constructor(issuer, algorithm) {
        super(issuer);
        this.algorithm = algorithm;
    }
    get header() {
        return {
            alg: this.algorithm.name,
            typ: "JWT",
        };
    }
    get payload() {
        const result = { iss: this.id, iat: Issuer.issuedAt };
        if (this.audience)
            result.aud = this.audience;
        if (this.duration && result.iat)
            result.exp = result.iat + this.duration;
        return result;
    }
    sign(payload, issuedAt) {
        return __awaiter(this, void 0, void 0, function* () {
            payload = Object.assign(Object.assign({}, this.payload), payload);
            if (issuedAt)
                payload.iat = typeof issuedAt == "number" ? issuedAt : issuedAt.getTime();
            const transformed = yield this.transformers.reduce((p, c) => __awaiter(this, void 0, void 0, function* () { return c.apply(yield p); }), Promise.resolve(payload));
            const data = transformed &&
                `${dist.Base64.encode(new dist.TextEncoder().encode(JSON.stringify(this.header)), "url")}.${dist.Base64.encode(new dist.TextEncoder().encode(JSON.stringify(transformed)), "url")}`;
            return data && `${data}.${yield this.algorithm.sign(data)}`;
        });
    }
    static get issuedAt() {
        return Issuer.defaultIssuedAt == undefined
            ? Math.floor(Date.now() / 1000)
            : typeof Issuer.defaultIssuedAt == "number"
                ? Issuer.defaultIssuedAt
                : Math.floor(Issuer.defaultIssuedAt.getTime());
    }
    static create(issuer, algorithm) {
        return (algorithm && new Issuer(issuer, algorithm)) || undefined;
    }
}
exports.Issuer = Issuer;

});

var Token_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Token = void 0;
var Token;
(function (Token) {
    function is(value) {
        return typeof value == "string" && /^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/.test(value);
    }
    Token.is = is;
})(Token = exports.Token || (exports.Token = {}));

});

var Verifier_1 = createCommonjsModule(function (module, exports) {
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Verifier = void 0;


class Verifier extends Actor_1.Actor {
    constructor(...algorithms) {
        super();
        if (algorithms.length > 0) {
            this.algorithms = {};
            for (const algorithm of algorithms)
                if (this.algorithms[algorithm.name])
                    this.algorithms[algorithm.name].push(algorithm);
                else
                    this.algorithms[algorithm.name] = [algorithm];
        }
        else
            this.algorithms = undefined;
    }
    verify(token, ...audience) {
        return __awaiter(this, void 0, void 0, function* () {
            let result;
            if (token) {
                const splitted = token.split(".", 3);
                if (splitted.length < 2)
                    result = undefined;
                else {
                    try {
                        const oldDecoder = token.includes("/") || token.includes("+");
                        const header = JSON.parse(new dist.TextDecoder().decode(dist.Base64.decode(splitted[0], oldDecoder ? "standard" : "url")));
                        result = JSON.parse(new dist.TextDecoder().decode(dist.Base64.decode(splitted[1], oldDecoder ? "standard" : "url")));
                        if (this.algorithms) {
                            const algorithm = this.algorithms[header.alg];
                            result =
                                splitted.length == 3 &&
                                    algorithm &&
                                    algorithm.some((a) => __awaiter(this, void 0, void 0, function* () { return yield a.verify(`${splitted[0]}.${splitted[1]}`, splitted[2]); }))
                                    ? result
                                    : undefined;
                        }
                    }
                    catch (_a) {
                        result = undefined;
                    }
                    result = result && this.verifyAudience(result.aud, audience) ? result : undefined;
                    if (result) {
                        result.token = token;
                        result = yield this.transformers.reduceRight((p, c) => __awaiter(this, void 0, void 0, function* () { return c.reverse(yield p); }), Promise.resolve(result));
                    }
                    if (result) {
                        const now = Math.floor(Date.now() / 1000);
                        if ((result === null || result === void 0 ? void 0 : result.iat) && result.iat > 1000000000000)
                            result.iat = Math.floor(result.iat / 1000);
                        if ((result === null || result === void 0 ? void 0 : result.exp) && result.exp > 1000000000000)
                            result.exp = Math.floor(result.exp / 1000);
                        result =
                            (result.exp == undefined || result.exp > now) && (result.iat == undefined || result.iat <= now)
                                ? result
                                : undefined;
                    }
                }
            }
            return result;
        });
    }
    verifyAudience(audience, allowed) {
        return (audience == undefined ||
            allowed.length == 0 ||
            (typeof audience == "string" && allowed.some(a => a == audience)) ||
            (Array.isArray(audience) && audience.some(a => allowed.some(ta => ta == a))));
    }
    authenticate(authorization, ...audience) {
        return __awaiter(this, void 0, void 0, function* () {
            return authorization && authorization.startsWith("Bearer ")
                ? this.verify(authorization.substr(7), ...audience)
                : undefined;
        });
    }
    static create(...algorithms) {
        return (((algorithms.length == 0 || algorithms.some(a => !!a)) &&
            new Verifier(...algorithms.filter(a => !!a))) ||
            undefined);
    }
}
exports.Verifier = Verifier;

});

var dist$1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.Identifier = exports.Verifier = exports.Token = exports.TextEncoder = exports.TextDecoder = exports.Property = exports.Password = exports.Issuer = exports.Base64 = exports.Algorithm = exports.Actor = void 0;

Object.defineProperty(exports, "Base64", { enumerable: true, get: function () { return dist.Base64; } });
Object.defineProperty(exports, "Password", { enumerable: true, get: function () { return dist.Password; } });
Object.defineProperty(exports, "TextDecoder", { enumerable: true, get: function () { return dist.TextDecoder; } });
Object.defineProperty(exports, "TextEncoder", { enumerable: true, get: function () { return dist.TextEncoder; } });
Object.defineProperty(exports, "Identifier", { enumerable: true, get: function () { return dist.Identifier; } });


Object.defineProperty(exports, "Actor", { enumerable: true, get: function () { return Actor_1.Actor; } });

Object.defineProperty(exports, "Algorithm", { enumerable: true, get: function () { return Algorithm_1$2.Algorithm; } });

Object.defineProperty(exports, "Issuer", { enumerable: true, get: function () { return Issuer_1.Issuer; } });

exports.Property = Property;

Object.defineProperty(exports, "Token", { enumerable: true, get: function () { return Token_1.Token; } });

Object.defineProperty(exports, "Verifier", { enumerable: true, get: function () { return Verifier_1.Verifier; } });

});

export { dist$1 as d };
