@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/secret.d.ts
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import 'node:crypto';
|
|
1
|
+
import 'crypto';
|
|
2
|
+
export { M as MIN_SECRET_ENTROPY_BITS, b as createSecret, g as getEntropy, f as getSecrets } from './sign.js';
|
|
4
3
|
import 'jose';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Create a secret in Uint8Array format
|
|
8
|
-
*
|
|
9
|
-
* @param secret - The secret as a string or Uint8Array
|
|
10
|
-
* @returns The secret in Uint8Array format
|
|
11
|
-
*/
|
|
12
|
-
declare const createSecret: (secret: SecretInput) => crypto.KeyObject | Uint8Array<ArrayBufferLike>;
|
|
13
|
-
|
|
14
|
-
export { createSecret };
|
package/dist/secret.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
MIN_SECRET_ENTROPY_BITS,
|
|
3
|
+
createSecret,
|
|
4
|
+
getEntropy,
|
|
5
|
+
getSecrets
|
|
6
|
+
} from "./chunk-SES6WQL3.js";
|
|
7
|
+
import "./chunk-ZHFHDRQH.js";
|
|
8
|
+
import "./chunk-BMXFAB6Q.js";
|
|
4
9
|
export {
|
|
5
|
-
|
|
10
|
+
MIN_SECRET_ENTROPY_BITS,
|
|
11
|
+
createSecret,
|
|
12
|
+
getEntropy,
|
|
13
|
+
getSecrets
|
|
6
14
|
};
|
package/dist/sign.cjs
CHANGED
|
@@ -35,15 +35,77 @@ __export(sign_exports, {
|
|
|
35
35
|
verifyJWS: () => verifyJWS
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(sign_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 JWSVerificationError = class extends AuraJoseError {
|
|
56
|
+
static code = "ERR_JWS_VERIFICATION";
|
|
57
|
+
};
|
|
58
|
+
var JWSSigningError = class extends AuraJoseError {
|
|
59
|
+
static code = "ERR_JWS_SIGNING";
|
|
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
|
+
var isObject = (value) => {
|
|
73
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
74
|
+
};
|
|
75
|
+
var isInvalidPayload = (payload) => {
|
|
76
|
+
return isFalsy(payload) || !isObject(payload) || typeof payload === "object" && payload !== null && !Array.isArray(payload) && Object.keys(payload).length === 0;
|
|
77
|
+
};
|
|
78
|
+
|
|
41
79
|
// src/secret.ts
|
|
42
|
-
var
|
|
43
|
-
|
|
80
|
+
var MIN_SECRET_ENTROPY_BITS = 4.5;
|
|
81
|
+
var getEntropy = (secret) => {
|
|
82
|
+
const charFreq = /* @__PURE__ */ new Map();
|
|
83
|
+
for (const char of secret) {
|
|
84
|
+
if (!charFreq.has(char)) {
|
|
85
|
+
charFreq.set(char, 0);
|
|
86
|
+
}
|
|
87
|
+
charFreq.set(char, charFreq.get(char) + 1);
|
|
88
|
+
}
|
|
89
|
+
let entropy = 0;
|
|
90
|
+
const length = secret.length;
|
|
91
|
+
for (const freq of charFreq.values()) {
|
|
92
|
+
const p = freq / length;
|
|
93
|
+
entropy -= p * Math.log2(p);
|
|
94
|
+
}
|
|
95
|
+
return entropy;
|
|
96
|
+
};
|
|
97
|
+
var createSecret = (secret, length = 32) => {
|
|
98
|
+
if (!Boolean(secret)) throw new InvalidSecretError("Secret is required");
|
|
44
99
|
if (typeof secret === "string") {
|
|
45
|
-
|
|
46
|
-
|
|
100
|
+
const byteLength = new TextEncoder().encode(secret).byteLength;
|
|
101
|
+
if (byteLength < length) {
|
|
102
|
+
throw new InvalidSecretError(`Secret string must be at least ${length} bytes long`);
|
|
103
|
+
}
|
|
104
|
+
const entropy = getEntropy(secret);
|
|
105
|
+
if (entropy < MIN_SECRET_ENTROPY_BITS) {
|
|
106
|
+
throw new InvalidSecretError(
|
|
107
|
+
`Secret string must have an entropy of at least ${MIN_SECRET_ENTROPY_BITS} bits per character`
|
|
108
|
+
);
|
|
47
109
|
}
|
|
48
110
|
return new Uint8Array(Buffer.from(secret, "utf-8"));
|
|
49
111
|
}
|
|
@@ -52,23 +114,39 @@ var createSecret = (secret) => {
|
|
|
52
114
|
|
|
53
115
|
// src/sign.ts
|
|
54
116
|
var signJWS = async (payload, secret) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
117
|
+
try {
|
|
118
|
+
if (isInvalidPayload(payload)) {
|
|
119
|
+
throw new InvalidPayloadError("The payload must be a non-empty object");
|
|
120
|
+
}
|
|
121
|
+
const secretKey = createSecret(secret);
|
|
122
|
+
const jti = import_crypto.default.randomBytes(32).toString("base64url");
|
|
123
|
+
return new import_jose.SignJWT(payload).setProtectedHeader({ alg: "HS256", typ: "JWT" }).setIssuedAt().setNotBefore(payload.nbf ?? "0s").setExpirationTime(payload.exp ?? "15d").setJti(jti).sign(secretKey);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (isAuraJoseError(error)) {
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
throw new JWSSigningError("JWS signing failed", { cause: error });
|
|
129
|
+
}
|
|
58
130
|
};
|
|
59
|
-
var verifyJWS = async (token, secret) => {
|
|
131
|
+
var verifyJWS = async (token, secret, options) => {
|
|
60
132
|
try {
|
|
133
|
+
if (isFalsy(token)) {
|
|
134
|
+
throw new InvalidPayloadError("The token must be a non-empty string");
|
|
135
|
+
}
|
|
61
136
|
const secretKey = createSecret(secret);
|
|
62
|
-
const { payload } = await (0, import_jose.jwtVerify)(token, secretKey);
|
|
137
|
+
const { payload } = await (0, import_jose.jwtVerify)(token, secretKey, options);
|
|
63
138
|
return payload;
|
|
64
139
|
} catch (error) {
|
|
65
|
-
|
|
140
|
+
if (isAuraJoseError(error)) {
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
throw new JWSVerificationError("JWS signature verification failed", { cause: error });
|
|
66
144
|
}
|
|
67
145
|
};
|
|
68
146
|
var createJWS = (secret) => {
|
|
69
147
|
return {
|
|
70
148
|
signJWS: (payload) => signJWS(payload, secret),
|
|
71
|
-
verifyJWS: (payload) => verifyJWS(payload, secret)
|
|
149
|
+
verifyJWS: (payload, options) => verifyJWS(payload, secret, options)
|
|
72
150
|
};
|
|
73
151
|
};
|
|
74
152
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/sign.d.ts
CHANGED
|
@@ -1,3 +1,174 @@
|
|
|
1
|
-
import '
|
|
2
|
-
|
|
3
|
-
import '
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
import { KeyObject, BinaryLike } from 'crypto';
|
|
3
|
+
import { JWTPayload, JWTVerifyOptions, JWTDecryptOptions } from 'jose';
|
|
4
|
+
export { JWTVerifyOptions } from 'jose';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sign a standard JWT token with the following claims:
|
|
8
|
+
* - alg: algorithm used to sign the JWT
|
|
9
|
+
* - typ: type of the token
|
|
10
|
+
* - iat: time at which the JWT was issued
|
|
11
|
+
* - nbf: not before time of the JWT
|
|
12
|
+
* - exp: expiration time of the JWT
|
|
13
|
+
* - jti: unique identifier to avoid collisions
|
|
14
|
+
*
|
|
15
|
+
* @param payload - Payload data information to sign the JWT
|
|
16
|
+
* @param secret - Secret key to sign the JWT (CryptoKey, KeyObject, string or Uint8Array)
|
|
17
|
+
* @returns Signed JWT string
|
|
18
|
+
*/
|
|
19
|
+
declare const signJWS: (payload: JWTPayload, secret: SecretInput) => Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Verify the integrity of a JWT token and return the payload if valid, rejecting
|
|
22
|
+
* tokens that use the "none" algorithm to prevent unsecured tokens.
|
|
23
|
+
*
|
|
24
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7519#section-6 Unsecured JWTs
|
|
25
|
+
* @param token - JWT string to verify
|
|
26
|
+
* @param secret - CryptoKey or KeyObject used to verify the JWT
|
|
27
|
+
* @param options - Additional JWT verification options
|
|
28
|
+
* @returns verify and return the payload of the JWT
|
|
29
|
+
*/
|
|
30
|
+
declare const verifyJWS: (token: string, secret: SecretInput, options?: JWTVerifyOptions) => Promise<JWTPayload>;
|
|
31
|
+
/**
|
|
32
|
+
* Create a JWS (JSON Web Signature) signer and verifier. It implements the `signJWS`
|
|
33
|
+
* and `verifyJWS` functions of the module.
|
|
34
|
+
*
|
|
35
|
+
* @param secret - Secret key used for signing and verifying the JWS
|
|
36
|
+
* @returns signJWS and verifyJWS functions
|
|
37
|
+
*/
|
|
38
|
+
declare const createJWS: (secret: SecretInput) => {
|
|
39
|
+
signJWS: (payload: JWTPayload) => Promise<string>;
|
|
40
|
+
verifyJWS: (payload: string, options?: JWTVerifyOptions) => Promise<JWTPayload>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
interface EncryptedPayload {
|
|
44
|
+
payload: string;
|
|
45
|
+
}
|
|
46
|
+
interface EncryptOptions {
|
|
47
|
+
nbf?: string | number | Date;
|
|
48
|
+
exp?: string | number | Date;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Encrypt a standard JWT token with the following claims:
|
|
52
|
+
* - alg: algorithm used to encrypt the JWT
|
|
53
|
+
* - enc: encryption method used
|
|
54
|
+
* - typ: type of the token
|
|
55
|
+
* - cty: content type of the token
|
|
56
|
+
*
|
|
57
|
+
* @param payload - Payload data information to encrypt the JWT
|
|
58
|
+
* @param secret - Secret key to encrypt the JWT (CryptoKey, KeyObject, string or Uint8Array)
|
|
59
|
+
* @returns Encrypted JWT string
|
|
60
|
+
*/
|
|
61
|
+
declare const encryptJWE: (payload: string, secret: SecretInput, options?: EncryptOptions) => Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Decrypt a JWE token and return the payload if valid.
|
|
64
|
+
*
|
|
65
|
+
* @param token - Encrypted JWT string to decrypt
|
|
66
|
+
* @param secret - Secret key to decrypt the JWT (CryptoKey, KeyObject, string or Uint8Array)
|
|
67
|
+
* @returns Decrypted JWT payload string
|
|
68
|
+
*/
|
|
69
|
+
declare const decryptJWE: (token: string, secret: SecretInput, options?: JWTDecryptOptions) => Promise<string>;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a `JWE (JSON Web Encryption)` encrypter and decrypter. It implements the `encryptJWE`
|
|
72
|
+
* and `decryptJWE` functions of the module.
|
|
73
|
+
*
|
|
74
|
+
* @param secret - Secret key used for encrypting and decrypting the JWE
|
|
75
|
+
* @returns encryptJWE and decryptJWE functions
|
|
76
|
+
*/
|
|
77
|
+
declare const createJWE: (secret: SecretInput) => {
|
|
78
|
+
encryptJWE: (payload: string, options?: EncryptOptions) => Promise<string>;
|
|
79
|
+
decryptJWE: (payload: string, options?: JWTDecryptOptions) => Promise<string>;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
declare const MIN_SECRET_ENTROPY_BITS = 4.5;
|
|
83
|
+
declare const getEntropy: (secret: string) => number;
|
|
84
|
+
/**
|
|
85
|
+
* Create a secret in Uint8Array format
|
|
86
|
+
*
|
|
87
|
+
* @param secret - The secret as a string or Uint8Array
|
|
88
|
+
* @returns The secret in Uint8Array format
|
|
89
|
+
*/
|
|
90
|
+
declare const createSecret: (secret: SecretInput, length?: number) => crypto.KeyObject | Uint8Array<ArrayBufferLike>;
|
|
91
|
+
declare const getSecrets: (secret: SecretInput | DerivedKeyInput) => {
|
|
92
|
+
jwsSecret: SecretInput;
|
|
93
|
+
jweSecret: SecretInput;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @module @aura-stack/jose
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
type SecretInput = KeyObject | Uint8Array | string;
|
|
101
|
+
type DerivedKeyInput = {
|
|
102
|
+
jws: SecretInput;
|
|
103
|
+
jwe: SecretInput;
|
|
104
|
+
};
|
|
105
|
+
type DecodedJWTPayloadOptions = {
|
|
106
|
+
jws: JWTVerifyOptions;
|
|
107
|
+
jwt: JWTDecryptOptions;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Encode a JWT signed and encrypted token. The token first signed using JWS
|
|
111
|
+
* and then encrypted using JWE to ensure both integrity and confidentiality.
|
|
112
|
+
* It implements the `signJWS` and `encryptJWE` functions of the module.
|
|
113
|
+
*
|
|
114
|
+
* Based on the RFC 7519 standard
|
|
115
|
+
* - Official RFC: https://datatracker.ietf.org/doc/html/rfc7519
|
|
116
|
+
* - Nested JWTs should be signed and then encrypted: https://datatracker.ietf.org/doc/html/rfc7519#section-5.2
|
|
117
|
+
* - Ensuring the integrity and confidentiality of the claims: https://datatracker.ietf.org/doc/html/rfc7519#section-11.2
|
|
118
|
+
*
|
|
119
|
+
* @param token - Payload data to encode in the JWT
|
|
120
|
+
* @param secret - Secret key used for both signing and encrypting the JWT
|
|
121
|
+
* @returns Promise resolving to the signed and encrypted JWT string
|
|
122
|
+
*/
|
|
123
|
+
declare const encodeJWT: (token: JWTPayload, secret: SecretInput | DerivedKeyInput) => Promise<string>;
|
|
124
|
+
/**
|
|
125
|
+
* Decode a JWT signed and encrypted token. The token is first decrypted using JWE
|
|
126
|
+
* and then verified using JWS to ensure both confidentiality and integrity. It
|
|
127
|
+
* implements the `decryptJWE` and `verifyJWS` functions of the module.
|
|
128
|
+
*
|
|
129
|
+
* Based on the RFC 7519 standard
|
|
130
|
+
* - Official RFC: https://datatracker.ietf.org/doc/html/rfc7519
|
|
131
|
+
* - Validating a JWT: https://datatracker.ietf.org/doc/html/rfc7519#section-7.2
|
|
132
|
+
* @param token
|
|
133
|
+
* @param secret
|
|
134
|
+
* @returns
|
|
135
|
+
*/
|
|
136
|
+
declare const decodeJWT: (token: string, secret: SecretInput | DerivedKeyInput, options?: DecodedJWTPayloadOptions) => Promise<JWTPayload>;
|
|
137
|
+
/**
|
|
138
|
+
* Create a JWT handler with encode and decode methods to `signJWS/encryptJWE` and `verifyJWS/decryptJWE`
|
|
139
|
+
* JWT tokens. The JWTs are signed and verified using JWS and encrypted and decrypted using JWE. It
|
|
140
|
+
* implements the `signJWS`, `verifyJWS`, `encryptJWE` and `decryptJWE` functions of the module.
|
|
141
|
+
*
|
|
142
|
+
* @param secret - Secret key used for signing, verifying, encrypting and decrypting the JWT
|
|
143
|
+
* @returns JWT handler object with `signJWS/encryptJWE` and `verifyJWS/decryptJWE` methods
|
|
144
|
+
*/
|
|
145
|
+
declare const createJWT: (secret: SecretInput | DerivedKeyInput) => {
|
|
146
|
+
encodeJWT: (payload: JWTPayload) => Promise<string>;
|
|
147
|
+
decodeJWT: (token: string) => Promise<JWTPayload>;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Generate a derived key using HKDF (HMAC-based Extract-and-Expand Key Derivation Function)
|
|
152
|
+
*
|
|
153
|
+
* @param secret Value used as the input keying material
|
|
154
|
+
* @param salt Cryptographic salt
|
|
155
|
+
* @param info Context and application specific information
|
|
156
|
+
* @param length Size of the derived key in bytes (default is 32 bytes)
|
|
157
|
+
* @returns Derived key as Uint8Array and base64 encoded string
|
|
158
|
+
*/
|
|
159
|
+
declare const deriveKey: (secret: SecretInput, salt: BinaryLike, info: string, length?: number) => {
|
|
160
|
+
key: ArrayBuffer;
|
|
161
|
+
derivedKey: Buffer<ArrayBuffer>;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Create a derived key from a given secret.
|
|
165
|
+
*
|
|
166
|
+
* @param secret - The secret as a string or Uint8Array
|
|
167
|
+
* @returns The secret in Uint8Array format
|
|
168
|
+
*/
|
|
169
|
+
declare const createDeriveKey: (secret: SecretInput, salt?: BinaryLike, info?: string, length?: number) => {
|
|
170
|
+
key: ArrayBuffer;
|
|
171
|
+
derivedKey: Buffer<ArrayBuffer>;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export { type DerivedKeyInput as D, type EncryptedPayload as E, MIN_SECRET_ENTROPY_BITS as M, type SecretInput as S, type EncryptOptions as a, createSecret as b, createJWE as c, createJWS, decryptJWE as d, encryptJWE as e, getSecrets as f, getEntropy as g, type DecodedJWTPayloadOptions as h, encodeJWT as i, decodeJWT as j, createJWT as k, deriveKey as l, createDeriveKey as m, signJWS, verifyJWS };
|
package/dist/sign.js
CHANGED
|
@@ -2,8 +2,10 @@ import {
|
|
|
2
2
|
createJWS,
|
|
3
3
|
signJWS,
|
|
4
4
|
verifyJWS
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
import "./chunk-
|
|
5
|
+
} from "./chunk-EX3NULRX.js";
|
|
6
|
+
import "./chunk-SES6WQL3.js";
|
|
7
|
+
import "./chunk-ZHFHDRQH.js";
|
|
8
|
+
import "./chunk-BMXFAB6Q.js";
|
|
7
9
|
export {
|
|
8
10
|
createJWS,
|
|
9
11
|
signJWS,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aura-stack/jose",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "JOSE utilities for @aura-stack/auth",
|
|
@@ -57,6 +57,8 @@
|
|
|
57
57
|
"jose": "^6.1.2"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
+
"@types/node": "^24.9.2",
|
|
61
|
+
"typescript": "^5.9.2",
|
|
60
62
|
"@aura-stack/tsconfig": "0.0.0",
|
|
61
63
|
"@aura-stack/tsup-config": "0.0.0"
|
|
62
64
|
},
|
package/dist/chunk-KSVD3YEC.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createSecret
|
|
3
|
-
} from "./chunk-M4WAOCIJ.js";
|
|
4
|
-
|
|
5
|
-
// src/sign.ts
|
|
6
|
-
import crypto from "crypto";
|
|
7
|
-
import { jwtVerify, SignJWT } from "jose";
|
|
8
|
-
var signJWS = async (payload, secret) => {
|
|
9
|
-
const secretKey = createSecret(secret);
|
|
10
|
-
const jti = crypto.randomBytes(32).toString("base64");
|
|
11
|
-
return new SignJWT(payload).setProtectedHeader({ alg: "HS256", typ: "JWT" }).setIssuedAt().setNotBefore("0s").setExpirationTime("15d").setJti(jti).sign(secretKey);
|
|
12
|
-
};
|
|
13
|
-
var verifyJWS = async (token, secret) => {
|
|
14
|
-
try {
|
|
15
|
-
const secretKey = createSecret(secret);
|
|
16
|
-
const { payload } = await jwtVerify(token, secretKey);
|
|
17
|
-
return payload;
|
|
18
|
-
} catch (error) {
|
|
19
|
-
throw new Error("Invalid JWS", { cause: error });
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
var createJWS = (secret) => {
|
|
23
|
-
return {
|
|
24
|
-
signJWS: (payload) => signJWS(payload, secret),
|
|
25
|
-
verifyJWS: (payload) => verifyJWS(payload, secret)
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export {
|
|
30
|
-
signJWS,
|
|
31
|
-
verifyJWS,
|
|
32
|
-
createJWS
|
|
33
|
-
};
|
package/dist/chunk-M4WAOCIJ.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// src/secret.ts
|
|
2
|
-
var createSecret = (secret) => {
|
|
3
|
-
if (secret === void 0) throw new Error("Secret is required");
|
|
4
|
-
if (typeof secret === "string") {
|
|
5
|
-
if (new TextEncoder().encode(secret).byteLength < 32) {
|
|
6
|
-
throw new Error("Secret string must be at least 32 characters long");
|
|
7
|
-
}
|
|
8
|
-
return new Uint8Array(Buffer.from(secret, "utf-8"));
|
|
9
|
-
}
|
|
10
|
-
return secret;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export {
|
|
14
|
-
createSecret
|
|
15
|
-
};
|
package/dist/chunk-T7MMDRY3.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createSecret
|
|
3
|
-
} from "./chunk-M4WAOCIJ.js";
|
|
4
|
-
|
|
5
|
-
// src/encrypt.ts
|
|
6
|
-
import crypto from "crypto";
|
|
7
|
-
import { EncryptJWT, jwtDecrypt } from "jose";
|
|
8
|
-
var encryptJWE = async (payload, secret) => {
|
|
9
|
-
const secretKey = createSecret(secret);
|
|
10
|
-
const jti = crypto.randomBytes(32).toString("base64");
|
|
11
|
-
return new EncryptJWT({ token: payload }).setProtectedHeader({ alg: "dir", enc: "A256GCM", typ: "JWT", cty: "JWT" }).setIssuedAt().setNotBefore("0s").setExpirationTime("15d").setJti(jti).encrypt(secretKey);
|
|
12
|
-
};
|
|
13
|
-
var decryptJWE = async (token, secret) => {
|
|
14
|
-
try {
|
|
15
|
-
const secretKey = createSecret(secret);
|
|
16
|
-
const { payload } = await jwtDecrypt(token, secretKey);
|
|
17
|
-
return payload.token;
|
|
18
|
-
} catch (error) {
|
|
19
|
-
throw new Error("Invalid JWE", { cause: error });
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
var createJWE = (secret) => {
|
|
23
|
-
return {
|
|
24
|
-
encryptJWE: (payload) => encryptJWE(payload, secret),
|
|
25
|
-
decryptJWE: (payload) => decryptJWE(payload, secret)
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export {
|
|
30
|
-
encryptJWE,
|
|
31
|
-
decryptJWE,
|
|
32
|
-
createJWE
|
|
33
|
-
};
|