@aura-stack/jose 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assert.cjs +69 -0
- package/dist/assert.d.ts +9 -0
- package/dist/assert.js +15 -0
- package/dist/chunk-BMXFAB6Q.js +47 -0
- package/dist/{chunk-ODRHALUH.js → chunk-CXN54JNY.js} +1 -1
- package/dist/chunk-EX3NULRX.js +59 -0
- package/dist/chunk-SES6WQL3.js +57 -0
- package/dist/chunk-URDLFFH3.js +58 -0
- package/dist/chunk-ZHFHDRQH.js +29 -0
- package/dist/deriveKey.cjs +45 -6
- package/dist/deriveKey.d.ts +2 -2
- package/dist/deriveKey.js +4 -2
- package/dist/encrypt.cjs +87 -15
- package/dist/encrypt.d.ts +3 -3
- package/dist/encrypt.js +4 -2
- package/dist/errors.cjs +79 -0
- package/dist/errors.d.ts +34 -0
- package/dist/errors.js +22 -0
- package/dist/index.cjs +172 -42
- package/dist/index.d.ts +3 -145
- package/dist/index.js +37 -13
- package/dist/secret.cjs +66 -6
- package/dist/secret.d.ts +2 -13
- package/dist/secret.js +11 -3
- package/dist/sign.cjs +90 -12
- package/dist/sign.d.ts +174 -3
- package/dist/sign.js +4 -2
- package/package.json +3 -1
- package/dist/chunk-KSVD3YEC.js +0 -33
- package/dist/chunk-M4WAOCIJ.js +0 -15
- package/dist/chunk-T7MMDRY3.js +0 -33
package/dist/assert.cjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/assert.ts
|
|
21
|
+
var assert_exports = {};
|
|
22
|
+
__export(assert_exports, {
|
|
23
|
+
isAuraJoseError: () => isAuraJoseError,
|
|
24
|
+
isFalsy: () => isFalsy,
|
|
25
|
+
isInvalidPayload: () => isInvalidPayload,
|
|
26
|
+
isInvalidSecretError: () => isInvalidSecretError,
|
|
27
|
+
isObject: () => isObject
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(assert_exports);
|
|
30
|
+
|
|
31
|
+
// src/errors.ts
|
|
32
|
+
var AuraJoseError = class extends Error {
|
|
33
|
+
static code = "ERR_AURA_JOSE_ERROR";
|
|
34
|
+
code;
|
|
35
|
+
constructor(message, options) {
|
|
36
|
+
super(message, options);
|
|
37
|
+
this.name = new.target.name;
|
|
38
|
+
this.code = new.target.code;
|
|
39
|
+
Error.captureStackTrace(this, new.target);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var InvalidSecretError = class extends AuraJoseError {
|
|
43
|
+
static code = "ERR_INVALID_SECRET";
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/assert.ts
|
|
47
|
+
var isAuraJoseError = (error) => {
|
|
48
|
+
return error instanceof AuraJoseError;
|
|
49
|
+
};
|
|
50
|
+
var isInvalidSecretError = (error) => {
|
|
51
|
+
return error instanceof InvalidSecretError;
|
|
52
|
+
};
|
|
53
|
+
var isFalsy = (value) => {
|
|
54
|
+
return value === null || value === void 0 || value === false || value === 0 || value === "" || Number.isNaN(value);
|
|
55
|
+
};
|
|
56
|
+
var isObject = (value) => {
|
|
57
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
58
|
+
};
|
|
59
|
+
var isInvalidPayload = (payload) => {
|
|
60
|
+
return isFalsy(payload) || !isObject(payload) || typeof payload === "object" && payload !== null && !Array.isArray(payload) && Object.keys(payload).length === 0;
|
|
61
|
+
};
|
|
62
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
+
0 && (module.exports = {
|
|
64
|
+
isAuraJoseError,
|
|
65
|
+
isFalsy,
|
|
66
|
+
isInvalidPayload,
|
|
67
|
+
isInvalidSecretError,
|
|
68
|
+
isObject
|
|
69
|
+
});
|
package/dist/assert.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AuraJoseError, InvalidSecretError } from './errors.js';
|
|
2
|
+
|
|
3
|
+
declare const isAuraJoseError: (error: unknown) => error is AuraJoseError;
|
|
4
|
+
declare const isInvalidSecretError: (error: unknown) => error is InvalidSecretError;
|
|
5
|
+
declare const isFalsy: (value: unknown) => boolean;
|
|
6
|
+
declare const isObject: (value: unknown) => value is Record<string, unknown>;
|
|
7
|
+
declare const isInvalidPayload: (payload: unknown) => boolean;
|
|
8
|
+
|
|
9
|
+
export { isAuraJoseError, isFalsy, isInvalidPayload, isInvalidSecretError, isObject };
|
package/dist/assert.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isAuraJoseError,
|
|
3
|
+
isFalsy,
|
|
4
|
+
isInvalidPayload,
|
|
5
|
+
isInvalidSecretError,
|
|
6
|
+
isObject
|
|
7
|
+
} from "./chunk-ZHFHDRQH.js";
|
|
8
|
+
import "./chunk-BMXFAB6Q.js";
|
|
9
|
+
export {
|
|
10
|
+
isAuraJoseError,
|
|
11
|
+
isFalsy,
|
|
12
|
+
isInvalidPayload,
|
|
13
|
+
isInvalidSecretError,
|
|
14
|
+
isObject
|
|
15
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var AuraJoseError = class extends Error {
|
|
3
|
+
static code = "ERR_AURA_JOSE_ERROR";
|
|
4
|
+
code;
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = new.target.name;
|
|
8
|
+
this.code = new.target.code;
|
|
9
|
+
Error.captureStackTrace(this, new.target);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var JWTEncodingError = class extends AuraJoseError {
|
|
13
|
+
static code = "ERR_JWT_ENCODING";
|
|
14
|
+
};
|
|
15
|
+
var JWTDecodingError = class extends AuraJoseError {
|
|
16
|
+
static code = "ERR_JWT_DECODING";
|
|
17
|
+
};
|
|
18
|
+
var InvalidPayloadError = class extends AuraJoseError {
|
|
19
|
+
static code = "ERR_INVALID_PAYLOAD";
|
|
20
|
+
};
|
|
21
|
+
var JWSVerificationError = class extends AuraJoseError {
|
|
22
|
+
static code = "ERR_JWS_VERIFICATION";
|
|
23
|
+
};
|
|
24
|
+
var JWSSigningError = class extends AuraJoseError {
|
|
25
|
+
static code = "ERR_JWS_SIGNING";
|
|
26
|
+
};
|
|
27
|
+
var JWEDecryptionError = class extends AuraJoseError {
|
|
28
|
+
static code = "ERR_JWE_DECRYPTION";
|
|
29
|
+
};
|
|
30
|
+
var JWEEncryptionError = class extends AuraJoseError {
|
|
31
|
+
static code = "ERR_JWE_ENCRYPTION";
|
|
32
|
+
};
|
|
33
|
+
var InvalidSecretError = class extends AuraJoseError {
|
|
34
|
+
static code = "ERR_INVALID_SECRET";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
AuraJoseError,
|
|
39
|
+
JWTEncodingError,
|
|
40
|
+
JWTDecodingError,
|
|
41
|
+
InvalidPayloadError,
|
|
42
|
+
JWSVerificationError,
|
|
43
|
+
JWSSigningError,
|
|
44
|
+
JWEDecryptionError,
|
|
45
|
+
JWEEncryptionError,
|
|
46
|
+
InvalidSecretError
|
|
47
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSecret
|
|
3
|
+
} from "./chunk-SES6WQL3.js";
|
|
4
|
+
import {
|
|
5
|
+
isAuraJoseError,
|
|
6
|
+
isFalsy,
|
|
7
|
+
isInvalidPayload
|
|
8
|
+
} from "./chunk-ZHFHDRQH.js";
|
|
9
|
+
import {
|
|
10
|
+
InvalidPayloadError,
|
|
11
|
+
JWSSigningError,
|
|
12
|
+
JWSVerificationError
|
|
13
|
+
} from "./chunk-BMXFAB6Q.js";
|
|
14
|
+
|
|
15
|
+
// src/sign.ts
|
|
16
|
+
import crypto from "crypto";
|
|
17
|
+
import { jwtVerify, SignJWT } from "jose";
|
|
18
|
+
var signJWS = async (payload, secret) => {
|
|
19
|
+
try {
|
|
20
|
+
if (isInvalidPayload(payload)) {
|
|
21
|
+
throw new InvalidPayloadError("The payload must be a non-empty object");
|
|
22
|
+
}
|
|
23
|
+
const secretKey = createSecret(secret);
|
|
24
|
+
const jti = crypto.randomBytes(32).toString("base64url");
|
|
25
|
+
return new SignJWT(payload).setProtectedHeader({ alg: "HS256", typ: "JWT" }).setIssuedAt().setNotBefore(payload.nbf ?? "0s").setExpirationTime(payload.exp ?? "15d").setJti(jti).sign(secretKey);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (isAuraJoseError(error)) {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
throw new JWSSigningError("JWS signing failed", { cause: error });
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var verifyJWS = async (token, secret, options) => {
|
|
34
|
+
try {
|
|
35
|
+
if (isFalsy(token)) {
|
|
36
|
+
throw new InvalidPayloadError("The token must be a non-empty string");
|
|
37
|
+
}
|
|
38
|
+
const secretKey = createSecret(secret);
|
|
39
|
+
const { payload } = await jwtVerify(token, secretKey, options);
|
|
40
|
+
return payload;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (isAuraJoseError(error)) {
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
throw new JWSVerificationError("JWS signature verification failed", { cause: error });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var createJWS = (secret) => {
|
|
49
|
+
return {
|
|
50
|
+
signJWS: (payload) => signJWS(payload, secret),
|
|
51
|
+
verifyJWS: (payload, options) => verifyJWS(payload, secret, options)
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
signJWS,
|
|
57
|
+
verifyJWS,
|
|
58
|
+
createJWS
|
|
59
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isObject
|
|
3
|
+
} from "./chunk-ZHFHDRQH.js";
|
|
4
|
+
import {
|
|
5
|
+
InvalidSecretError
|
|
6
|
+
} from "./chunk-BMXFAB6Q.js";
|
|
7
|
+
|
|
8
|
+
// src/secret.ts
|
|
9
|
+
var MIN_SECRET_ENTROPY_BITS = 4.5;
|
|
10
|
+
var getEntropy = (secret) => {
|
|
11
|
+
const charFreq = /* @__PURE__ */ new Map();
|
|
12
|
+
for (const char of secret) {
|
|
13
|
+
if (!charFreq.has(char)) {
|
|
14
|
+
charFreq.set(char, 0);
|
|
15
|
+
}
|
|
16
|
+
charFreq.set(char, charFreq.get(char) + 1);
|
|
17
|
+
}
|
|
18
|
+
let entropy = 0;
|
|
19
|
+
const length = secret.length;
|
|
20
|
+
for (const freq of charFreq.values()) {
|
|
21
|
+
const p = freq / length;
|
|
22
|
+
entropy -= p * Math.log2(p);
|
|
23
|
+
}
|
|
24
|
+
return entropy;
|
|
25
|
+
};
|
|
26
|
+
var createSecret = (secret, length = 32) => {
|
|
27
|
+
if (!Boolean(secret)) throw new InvalidSecretError("Secret is required");
|
|
28
|
+
if (typeof secret === "string") {
|
|
29
|
+
const byteLength = new TextEncoder().encode(secret).byteLength;
|
|
30
|
+
if (byteLength < length) {
|
|
31
|
+
throw new InvalidSecretError(`Secret string must be at least ${length} bytes long`);
|
|
32
|
+
}
|
|
33
|
+
const entropy = getEntropy(secret);
|
|
34
|
+
if (entropy < MIN_SECRET_ENTROPY_BITS) {
|
|
35
|
+
throw new InvalidSecretError(
|
|
36
|
+
`Secret string must have an entropy of at least ${MIN_SECRET_ENTROPY_BITS} bits per character`
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return new Uint8Array(Buffer.from(secret, "utf-8"));
|
|
40
|
+
}
|
|
41
|
+
return secret;
|
|
42
|
+
};
|
|
43
|
+
var getSecrets = (secret) => {
|
|
44
|
+
const jwsSecret = isObject(secret) && "jws" in secret ? secret.jws : secret;
|
|
45
|
+
const jweSecret = isObject(secret) && "jwe" in secret ? secret.jwe : secret;
|
|
46
|
+
return {
|
|
47
|
+
jwsSecret,
|
|
48
|
+
jweSecret
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
MIN_SECRET_ENTROPY_BITS,
|
|
54
|
+
getEntropy,
|
|
55
|
+
createSecret,
|
|
56
|
+
getSecrets
|
|
57
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSecret
|
|
3
|
+
} from "./chunk-SES6WQL3.js";
|
|
4
|
+
import {
|
|
5
|
+
isAuraJoseError,
|
|
6
|
+
isFalsy
|
|
7
|
+
} from "./chunk-ZHFHDRQH.js";
|
|
8
|
+
import {
|
|
9
|
+
InvalidPayloadError,
|
|
10
|
+
JWEDecryptionError,
|
|
11
|
+
JWEEncryptionError
|
|
12
|
+
} from "./chunk-BMXFAB6Q.js";
|
|
13
|
+
|
|
14
|
+
// src/encrypt.ts
|
|
15
|
+
import crypto from "crypto";
|
|
16
|
+
import { EncryptJWT, jwtDecrypt } from "jose";
|
|
17
|
+
var encryptJWE = async (payload, secret, options) => {
|
|
18
|
+
try {
|
|
19
|
+
if (isFalsy(payload)) {
|
|
20
|
+
throw new InvalidPayloadError("The payload must be a non-empty string");
|
|
21
|
+
}
|
|
22
|
+
const secretKey = createSecret(secret);
|
|
23
|
+
const jti = crypto.randomBytes(32).toString("base64url");
|
|
24
|
+
return new EncryptJWT({ payload }).setProtectedHeader({ alg: "dir", enc: "A256GCM", typ: "JWT", cty: "JWT" }).setIssuedAt().setNotBefore(options?.nbf ?? "0s").setExpirationTime(options?.exp ?? "15d").setJti(jti).encrypt(secretKey);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (isAuraJoseError(error)) {
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
throw new JWEEncryptionError("JWE encryption failed", { cause: error });
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var decryptJWE = async (token, secret, options) => {
|
|
33
|
+
try {
|
|
34
|
+
if (isFalsy(token)) {
|
|
35
|
+
throw new InvalidPayloadError("The token must be a non-empty string");
|
|
36
|
+
}
|
|
37
|
+
const secretKey = createSecret(secret);
|
|
38
|
+
const { payload } = await jwtDecrypt(token, secretKey, options);
|
|
39
|
+
return payload.payload;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if (isAuraJoseError(error)) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
throw new JWEDecryptionError("JWE decryption verification failed", { cause: error });
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var createJWE = (secret) => {
|
|
48
|
+
return {
|
|
49
|
+
encryptJWE: (payload, options) => encryptJWE(payload, secret, options),
|
|
50
|
+
decryptJWE: (payload, options) => decryptJWE(payload, secret, options)
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
encryptJWE,
|
|
56
|
+
decryptJWE,
|
|
57
|
+
createJWE
|
|
58
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuraJoseError,
|
|
3
|
+
InvalidSecretError
|
|
4
|
+
} from "./chunk-BMXFAB6Q.js";
|
|
5
|
+
|
|
6
|
+
// src/assert.ts
|
|
7
|
+
var isAuraJoseError = (error) => {
|
|
8
|
+
return error instanceof AuraJoseError;
|
|
9
|
+
};
|
|
10
|
+
var isInvalidSecretError = (error) => {
|
|
11
|
+
return error instanceof InvalidSecretError;
|
|
12
|
+
};
|
|
13
|
+
var isFalsy = (value) => {
|
|
14
|
+
return value === null || value === void 0 || value === false || value === 0 || value === "" || Number.isNaN(value);
|
|
15
|
+
};
|
|
16
|
+
var isObject = (value) => {
|
|
17
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
18
|
+
};
|
|
19
|
+
var isInvalidPayload = (payload) => {
|
|
20
|
+
return isFalsy(payload) || !isObject(payload) || typeof payload === "object" && payload !== null && !Array.isArray(payload) && Object.keys(payload).length === 0;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
isAuraJoseError,
|
|
25
|
+
isInvalidSecretError,
|
|
26
|
+
isFalsy,
|
|
27
|
+
isObject,
|
|
28
|
+
isInvalidPayload
|
|
29
|
+
};
|
package/dist/deriveKey.cjs
CHANGED
|
@@ -24,14 +24,53 @@ __export(deriveKey_exports, {
|
|
|
24
24
|
deriveKey: () => deriveKey
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(deriveKey_exports);
|
|
27
|
-
var
|
|
27
|
+
var import_crypto = require("crypto");
|
|
28
|
+
|
|
29
|
+
// src/errors.ts
|
|
30
|
+
var AuraJoseError = class extends Error {
|
|
31
|
+
static code = "ERR_AURA_JOSE_ERROR";
|
|
32
|
+
code;
|
|
33
|
+
constructor(message, options) {
|
|
34
|
+
super(message, options);
|
|
35
|
+
this.name = new.target.name;
|
|
36
|
+
this.code = new.target.code;
|
|
37
|
+
Error.captureStackTrace(this, new.target);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var InvalidSecretError = class extends AuraJoseError {
|
|
41
|
+
static code = "ERR_INVALID_SECRET";
|
|
42
|
+
};
|
|
28
43
|
|
|
29
44
|
// src/secret.ts
|
|
30
|
-
var
|
|
31
|
-
|
|
45
|
+
var MIN_SECRET_ENTROPY_BITS = 4.5;
|
|
46
|
+
var getEntropy = (secret) => {
|
|
47
|
+
const charFreq = /* @__PURE__ */ new Map();
|
|
48
|
+
for (const char of secret) {
|
|
49
|
+
if (!charFreq.has(char)) {
|
|
50
|
+
charFreq.set(char, 0);
|
|
51
|
+
}
|
|
52
|
+
charFreq.set(char, charFreq.get(char) + 1);
|
|
53
|
+
}
|
|
54
|
+
let entropy = 0;
|
|
55
|
+
const length = secret.length;
|
|
56
|
+
for (const freq of charFreq.values()) {
|
|
57
|
+
const p = freq / length;
|
|
58
|
+
entropy -= p * Math.log2(p);
|
|
59
|
+
}
|
|
60
|
+
return entropy;
|
|
61
|
+
};
|
|
62
|
+
var createSecret = (secret, length = 32) => {
|
|
63
|
+
if (!Boolean(secret)) throw new InvalidSecretError("Secret is required");
|
|
32
64
|
if (typeof secret === "string") {
|
|
33
|
-
|
|
34
|
-
|
|
65
|
+
const byteLength = new TextEncoder().encode(secret).byteLength;
|
|
66
|
+
if (byteLength < length) {
|
|
67
|
+
throw new InvalidSecretError(`Secret string must be at least ${length} bytes long`);
|
|
68
|
+
}
|
|
69
|
+
const entropy = getEntropy(secret);
|
|
70
|
+
if (entropy < MIN_SECRET_ENTROPY_BITS) {
|
|
71
|
+
throw new InvalidSecretError(
|
|
72
|
+
`Secret string must have an entropy of at least ${MIN_SECRET_ENTROPY_BITS} bits per character`
|
|
73
|
+
);
|
|
35
74
|
}
|
|
36
75
|
return new Uint8Array(Buffer.from(secret, "utf-8"));
|
|
37
76
|
}
|
|
@@ -41,7 +80,7 @@ var createSecret = (secret) => {
|
|
|
41
80
|
// src/deriveKey.ts
|
|
42
81
|
var deriveKey = (secret, salt, info, length = 32) => {
|
|
43
82
|
try {
|
|
44
|
-
const key = (0,
|
|
83
|
+
const key = (0, import_crypto.hkdfSync)("SHA256", secret, salt, info, length);
|
|
45
84
|
const derivedKey = Buffer.from(key);
|
|
46
85
|
return {
|
|
47
86
|
key,
|
package/dist/deriveKey.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import '
|
|
2
|
-
export { createDeriveKey, deriveKey } from './
|
|
1
|
+
import 'crypto';
|
|
2
|
+
export { m as createDeriveKey, l as deriveKey } from './sign.js';
|
|
3
3
|
import 'jose';
|
package/dist/deriveKey.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createDeriveKey,
|
|
3
3
|
deriveKey
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
import "./chunk-
|
|
4
|
+
} from "./chunk-CXN54JNY.js";
|
|
5
|
+
import "./chunk-SES6WQL3.js";
|
|
6
|
+
import "./chunk-ZHFHDRQH.js";
|
|
7
|
+
import "./chunk-BMXFAB6Q.js";
|
|
6
8
|
export {
|
|
7
9
|
createDeriveKey,
|
|
8
10
|
deriveKey
|
package/dist/encrypt.cjs
CHANGED
|
@@ -35,15 +35,71 @@ __export(encrypt_exports, {
|
|
|
35
35
|
encryptJWE: () => encryptJWE
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(encrypt_exports);
|
|
38
|
-
var
|
|
38
|
+
var import_crypto = __toESM(require("crypto"), 1);
|
|
39
39
|
var import_jose = require("jose");
|
|
40
40
|
|
|
41
|
+
// src/errors.ts
|
|
42
|
+
var AuraJoseError = class extends Error {
|
|
43
|
+
static code = "ERR_AURA_JOSE_ERROR";
|
|
44
|
+
code;
|
|
45
|
+
constructor(message, options) {
|
|
46
|
+
super(message, options);
|
|
47
|
+
this.name = new.target.name;
|
|
48
|
+
this.code = new.target.code;
|
|
49
|
+
Error.captureStackTrace(this, new.target);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var InvalidPayloadError = class extends AuraJoseError {
|
|
53
|
+
static code = "ERR_INVALID_PAYLOAD";
|
|
54
|
+
};
|
|
55
|
+
var JWEDecryptionError = class extends AuraJoseError {
|
|
56
|
+
static code = "ERR_JWE_DECRYPTION";
|
|
57
|
+
};
|
|
58
|
+
var JWEEncryptionError = class extends AuraJoseError {
|
|
59
|
+
static code = "ERR_JWE_ENCRYPTION";
|
|
60
|
+
};
|
|
61
|
+
var InvalidSecretError = class extends AuraJoseError {
|
|
62
|
+
static code = "ERR_INVALID_SECRET";
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/assert.ts
|
|
66
|
+
var isAuraJoseError = (error) => {
|
|
67
|
+
return error instanceof AuraJoseError;
|
|
68
|
+
};
|
|
69
|
+
var isFalsy = (value) => {
|
|
70
|
+
return value === null || value === void 0 || value === false || value === 0 || value === "" || Number.isNaN(value);
|
|
71
|
+
};
|
|
72
|
+
|
|
41
73
|
// src/secret.ts
|
|
42
|
-
var
|
|
43
|
-
|
|
74
|
+
var MIN_SECRET_ENTROPY_BITS = 4.5;
|
|
75
|
+
var getEntropy = (secret) => {
|
|
76
|
+
const charFreq = /* @__PURE__ */ new Map();
|
|
77
|
+
for (const char of secret) {
|
|
78
|
+
if (!charFreq.has(char)) {
|
|
79
|
+
charFreq.set(char, 0);
|
|
80
|
+
}
|
|
81
|
+
charFreq.set(char, charFreq.get(char) + 1);
|
|
82
|
+
}
|
|
83
|
+
let entropy = 0;
|
|
84
|
+
const length = secret.length;
|
|
85
|
+
for (const freq of charFreq.values()) {
|
|
86
|
+
const p = freq / length;
|
|
87
|
+
entropy -= p * Math.log2(p);
|
|
88
|
+
}
|
|
89
|
+
return entropy;
|
|
90
|
+
};
|
|
91
|
+
var createSecret = (secret, length = 32) => {
|
|
92
|
+
if (!Boolean(secret)) throw new InvalidSecretError("Secret is required");
|
|
44
93
|
if (typeof secret === "string") {
|
|
45
|
-
|
|
46
|
-
|
|
94
|
+
const byteLength = new TextEncoder().encode(secret).byteLength;
|
|
95
|
+
if (byteLength < length) {
|
|
96
|
+
throw new InvalidSecretError(`Secret string must be at least ${length} bytes long`);
|
|
97
|
+
}
|
|
98
|
+
const entropy = getEntropy(secret);
|
|
99
|
+
if (entropy < MIN_SECRET_ENTROPY_BITS) {
|
|
100
|
+
throw new InvalidSecretError(
|
|
101
|
+
`Secret string must have an entropy of at least ${MIN_SECRET_ENTROPY_BITS} bits per character`
|
|
102
|
+
);
|
|
47
103
|
}
|
|
48
104
|
return new Uint8Array(Buffer.from(secret, "utf-8"));
|
|
49
105
|
}
|
|
@@ -51,24 +107,40 @@ var createSecret = (secret) => {
|
|
|
51
107
|
};
|
|
52
108
|
|
|
53
109
|
// src/encrypt.ts
|
|
54
|
-
var encryptJWE = async (payload, secret) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
110
|
+
var encryptJWE = async (payload, secret, options) => {
|
|
111
|
+
try {
|
|
112
|
+
if (isFalsy(payload)) {
|
|
113
|
+
throw new InvalidPayloadError("The payload must be a non-empty string");
|
|
114
|
+
}
|
|
115
|
+
const secretKey = createSecret(secret);
|
|
116
|
+
const jti = import_crypto.default.randomBytes(32).toString("base64url");
|
|
117
|
+
return new import_jose.EncryptJWT({ payload }).setProtectedHeader({ alg: "dir", enc: "A256GCM", typ: "JWT", cty: "JWT" }).setIssuedAt().setNotBefore(options?.nbf ?? "0s").setExpirationTime(options?.exp ?? "15d").setJti(jti).encrypt(secretKey);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (isAuraJoseError(error)) {
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
throw new JWEEncryptionError("JWE encryption failed", { cause: error });
|
|
123
|
+
}
|
|
58
124
|
};
|
|
59
|
-
var decryptJWE = async (token, secret) => {
|
|
125
|
+
var decryptJWE = async (token, secret, options) => {
|
|
60
126
|
try {
|
|
127
|
+
if (isFalsy(token)) {
|
|
128
|
+
throw new InvalidPayloadError("The token must be a non-empty string");
|
|
129
|
+
}
|
|
61
130
|
const secretKey = createSecret(secret);
|
|
62
|
-
const { payload } = await (0, import_jose.jwtDecrypt)(token, secretKey);
|
|
63
|
-
return payload.
|
|
131
|
+
const { payload } = await (0, import_jose.jwtDecrypt)(token, secretKey, options);
|
|
132
|
+
return payload.payload;
|
|
64
133
|
} catch (error) {
|
|
65
|
-
|
|
134
|
+
if (isAuraJoseError(error)) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
throw new JWEDecryptionError("JWE decryption verification failed", { cause: error });
|
|
66
138
|
}
|
|
67
139
|
};
|
|
68
140
|
var createJWE = (secret) => {
|
|
69
141
|
return {
|
|
70
|
-
encryptJWE: (payload) => encryptJWE(payload, secret),
|
|
71
|
-
decryptJWE: (payload) => decryptJWE(payload, secret)
|
|
142
|
+
encryptJWE: (payload, options) => encryptJWE(payload, secret, options),
|
|
143
|
+
decryptJWE: (payload, options) => decryptJWE(payload, secret, options)
|
|
72
144
|
};
|
|
73
145
|
};
|
|
74
146
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/encrypt.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
import '
|
|
1
|
+
export { JWTDecryptOptions } from 'jose';
|
|
2
|
+
export { a as EncryptOptions, E as EncryptedPayload, c as createJWE, d as decryptJWE, e as encryptJWE } from './sign.js';
|
|
3
|
+
import 'crypto';
|
package/dist/encrypt.js
CHANGED
|
@@ -2,8 +2,10 @@ import {
|
|
|
2
2
|
createJWE,
|
|
3
3
|
decryptJWE,
|
|
4
4
|
encryptJWE
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
import "./chunk-
|
|
5
|
+
} from "./chunk-URDLFFH3.js";
|
|
6
|
+
import "./chunk-SES6WQL3.js";
|
|
7
|
+
import "./chunk-ZHFHDRQH.js";
|
|
8
|
+
import "./chunk-BMXFAB6Q.js";
|
|
7
9
|
export {
|
|
8
10
|
createJWE,
|
|
9
11
|
decryptJWE,
|
package/dist/errors.cjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/errors.ts
|
|
21
|
+
var errors_exports = {};
|
|
22
|
+
__export(errors_exports, {
|
|
23
|
+
AuraJoseError: () => AuraJoseError,
|
|
24
|
+
InvalidPayloadError: () => InvalidPayloadError,
|
|
25
|
+
InvalidSecretError: () => InvalidSecretError,
|
|
26
|
+
JWEDecryptionError: () => JWEDecryptionError,
|
|
27
|
+
JWEEncryptionError: () => JWEEncryptionError,
|
|
28
|
+
JWSSigningError: () => JWSSigningError,
|
|
29
|
+
JWSVerificationError: () => JWSVerificationError,
|
|
30
|
+
JWTDecodingError: () => JWTDecodingError,
|
|
31
|
+
JWTEncodingError: () => JWTEncodingError
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(errors_exports);
|
|
34
|
+
var AuraJoseError = class extends Error {
|
|
35
|
+
static code = "ERR_AURA_JOSE_ERROR";
|
|
36
|
+
code;
|
|
37
|
+
constructor(message, options) {
|
|
38
|
+
super(message, options);
|
|
39
|
+
this.name = new.target.name;
|
|
40
|
+
this.code = new.target.code;
|
|
41
|
+
Error.captureStackTrace(this, new.target);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var JWTEncodingError = class extends AuraJoseError {
|
|
45
|
+
static code = "ERR_JWT_ENCODING";
|
|
46
|
+
};
|
|
47
|
+
var JWTDecodingError = class extends AuraJoseError {
|
|
48
|
+
static code = "ERR_JWT_DECODING";
|
|
49
|
+
};
|
|
50
|
+
var InvalidPayloadError = class extends AuraJoseError {
|
|
51
|
+
static code = "ERR_INVALID_PAYLOAD";
|
|
52
|
+
};
|
|
53
|
+
var JWSVerificationError = class extends AuraJoseError {
|
|
54
|
+
static code = "ERR_JWS_VERIFICATION";
|
|
55
|
+
};
|
|
56
|
+
var JWSSigningError = class extends AuraJoseError {
|
|
57
|
+
static code = "ERR_JWS_SIGNING";
|
|
58
|
+
};
|
|
59
|
+
var JWEDecryptionError = class extends AuraJoseError {
|
|
60
|
+
static code = "ERR_JWE_DECRYPTION";
|
|
61
|
+
};
|
|
62
|
+
var JWEEncryptionError = class extends AuraJoseError {
|
|
63
|
+
static code = "ERR_JWE_ENCRYPTION";
|
|
64
|
+
};
|
|
65
|
+
var InvalidSecretError = class extends AuraJoseError {
|
|
66
|
+
static code = "ERR_INVALID_SECRET";
|
|
67
|
+
};
|
|
68
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
+
0 && (module.exports = {
|
|
70
|
+
AuraJoseError,
|
|
71
|
+
InvalidPayloadError,
|
|
72
|
+
InvalidSecretError,
|
|
73
|
+
JWEDecryptionError,
|
|
74
|
+
JWEEncryptionError,
|
|
75
|
+
JWSSigningError,
|
|
76
|
+
JWSVerificationError,
|
|
77
|
+
JWTDecodingError,
|
|
78
|
+
JWTEncodingError
|
|
79
|
+
});
|