@arex95/vue-core 1.1.24 → 1.1.26
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/index.mjs +46 -3
- package/dist/utils/encryption.d.ts +8 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2518,20 +2518,35 @@ function ab2hex(buffer) {
|
|
|
2518
2518
|
* Converts a hexadecimal string to a Uint8Array.
|
|
2519
2519
|
* @param hex The hexadecimal string to convert.
|
|
2520
2520
|
* @returns The Uint8Array.
|
|
2521
|
+
* @throws {TypeError} If the input is not a string.
|
|
2522
|
+
* @throws {Error} If the hexadecimal string format is invalid or has an odd length.
|
|
2521
2523
|
*/
|
|
2522
2524
|
function hex2ab(hex) {
|
|
2523
|
-
if (
|
|
2525
|
+
if (typeof hex !== "string") {
|
|
2526
|
+
throw new TypeError("Input must be a string.");
|
|
2527
|
+
}
|
|
2528
|
+
if (hex.length === 0) {
|
|
2524
2529
|
return new Uint8Array();
|
|
2525
2530
|
}
|
|
2526
|
-
|
|
2527
|
-
|
|
2531
|
+
if (!/^[0-9a-fA-F]*$/.test(hex) || hex.length % 2 !== 0) {
|
|
2532
|
+
throw new Error("Invalid hexadecimal string format or odd length.");
|
|
2533
|
+
}
|
|
2534
|
+
const array = new Uint8Array(hex.length / 2);
|
|
2535
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
2536
|
+
array[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
2537
|
+
}
|
|
2538
|
+
return array;
|
|
2528
2539
|
}
|
|
2529
2540
|
/**
|
|
2530
2541
|
* Derives an encryption key from a secret key.
|
|
2531
2542
|
* @param secretKey The secret key in plain text.
|
|
2532
2543
|
* @returns A promise that resolves with the derived CryptoKey.
|
|
2544
|
+
* @throws {Error} If the secretKey is null or empty.
|
|
2533
2545
|
*/
|
|
2534
2546
|
async function importKey(secretKey) {
|
|
2547
|
+
if (!secretKey) {
|
|
2548
|
+
throw new Error("Secret key cannot be null or empty.");
|
|
2549
|
+
}
|
|
2535
2550
|
const keyMaterial = new TextEncoder().encode(secretKey);
|
|
2536
2551
|
const digest = await crypto.subtle.digest("SHA-256", keyMaterial);
|
|
2537
2552
|
return crypto.subtle.importKey("raw", digest, { name: "AES-CBC", length: 256 }, false, ["encrypt", "decrypt"]);
|
|
@@ -2541,6 +2556,7 @@ async function importKey(secretKey) {
|
|
|
2541
2556
|
* @param value The value to encrypt.
|
|
2542
2557
|
* @param secretKey The secret key for encryption.
|
|
2543
2558
|
* @returns A promise that resolves with the IV (hex) + ciphertext (hex) string.
|
|
2559
|
+
* @throws {Error} If the secretKey is null or empty (via importKey).
|
|
2544
2560
|
*/
|
|
2545
2561
|
async function encrypt(value, secretKey) {
|
|
2546
2562
|
const key = await importKey(secretKey);
|
|
@@ -2554,13 +2570,31 @@ async function encrypt(value, secretKey) {
|
|
|
2554
2570
|
* @param encryptedValue The encrypted string (IV_hex + ciphertext_hex).
|
|
2555
2571
|
* @param secretKey The secret key for decryption.
|
|
2556
2572
|
* @returns A promise that resolves with the decrypted value.
|
|
2573
|
+
* @throws {Error} If encryptedValue is null or empty, too short,
|
|
2574
|
+
* or if the IV/ciphertext have incorrect lengths after conversion.
|
|
2575
|
+
* @throws {Error} If the secretKey is null or empty (via importKey).
|
|
2576
|
+
* @throws {TypeError} If hex2ab receives an invalid input type.
|
|
2557
2577
|
*/
|
|
2558
2578
|
async function decrypt(encryptedValue, secretKey) {
|
|
2579
|
+
if (!encryptedValue) {
|
|
2580
|
+
throw new Error("Encrypted value cannot be null or empty.");
|
|
2581
|
+
}
|
|
2582
|
+
// For AES-CBC, the IV is ALWAYS 16 bytes.
|
|
2583
|
+
// 16 bytes * 2 hex characters/byte = 32 hex characters for the IV.
|
|
2584
|
+
if (encryptedValue.length < 32) {
|
|
2585
|
+
throw new Error("Encrypted value is too short. Expected at least 32 hexadecimal characters for the IV.");
|
|
2586
|
+
}
|
|
2559
2587
|
const key = await importKey(secretKey);
|
|
2560
2588
|
const ivHex = encryptedValue.substring(0, 32);
|
|
2561
2589
|
const ciphertextHex = encryptedValue.substring(32);
|
|
2562
2590
|
const iv = hex2ab(ivHex);
|
|
2563
2591
|
const ciphertext = hex2ab(ciphertextHex);
|
|
2592
|
+
if (iv.byteLength !== 16) {
|
|
2593
|
+
throw new Error(`Converted IV has incorrect length: ${iv.byteLength} bytes. Expected 16 bytes.`);
|
|
2594
|
+
}
|
|
2595
|
+
if (ciphertext.byteLength === 0) {
|
|
2596
|
+
throw new Error("Ciphertext is empty. No data to decrypt.");
|
|
2597
|
+
}
|
|
2564
2598
|
const decryptedBuffer = await crypto.subtle.decrypt({ name: "AES-CBC", iv: iv }, key, ciphertext);
|
|
2565
2599
|
return new TextDecoder().decode(decryptedBuffer);
|
|
2566
2600
|
}
|
|
@@ -3319,6 +3353,15 @@ function useAuth(secretKey = getSecretKey()) {
|
|
|
3319
3353
|
const login = async (params, persistence) => {
|
|
3320
3354
|
try {
|
|
3321
3355
|
const { data } = await axiosInstance.post(endpoints.LOGIN, params);
|
|
3356
|
+
if (!data) {
|
|
3357
|
+
throw new Error("LOGIN_ERROR: No data received from login endpoint.");
|
|
3358
|
+
}
|
|
3359
|
+
if (!data.access_token) {
|
|
3360
|
+
throw new Error("LOGIN_ERROR: Access token not found in response.");
|
|
3361
|
+
}
|
|
3362
|
+
if (!data.refresh_token) {
|
|
3363
|
+
throw new Error("LOGIN_ERROR: Refresh token not found in response.");
|
|
3364
|
+
}
|
|
3322
3365
|
configSession({
|
|
3323
3366
|
persistencePreference: persistence,
|
|
3324
3367
|
});
|
|
@@ -8,12 +8,15 @@ export declare function ab2hex(buffer: ArrayBuffer | Uint8Array): string;
|
|
|
8
8
|
* Converts a hexadecimal string to a Uint8Array.
|
|
9
9
|
* @param hex The hexadecimal string to convert.
|
|
10
10
|
* @returns The Uint8Array.
|
|
11
|
+
* @throws {TypeError} If the input is not a string.
|
|
12
|
+
* @throws {Error} If the hexadecimal string format is invalid or has an odd length.
|
|
11
13
|
*/
|
|
12
14
|
export declare function hex2ab(hex: string): Uint8Array;
|
|
13
15
|
/**
|
|
14
16
|
* Derives an encryption key from a secret key.
|
|
15
17
|
* @param secretKey The secret key in plain text.
|
|
16
18
|
* @returns A promise that resolves with the derived CryptoKey.
|
|
19
|
+
* @throws {Error} If the secretKey is null or empty.
|
|
17
20
|
*/
|
|
18
21
|
export declare function importKey(secretKey: string): Promise<CryptoKey>;
|
|
19
22
|
/**
|
|
@@ -21,6 +24,7 @@ export declare function importKey(secretKey: string): Promise<CryptoKey>;
|
|
|
21
24
|
* @param value The value to encrypt.
|
|
22
25
|
* @param secretKey The secret key for encryption.
|
|
23
26
|
* @returns A promise that resolves with the IV (hex) + ciphertext (hex) string.
|
|
27
|
+
* @throws {Error} If the secretKey is null or empty (via importKey).
|
|
24
28
|
*/
|
|
25
29
|
export declare function encrypt(value: string, secretKey: string): Promise<string>;
|
|
26
30
|
/**
|
|
@@ -28,5 +32,9 @@ export declare function encrypt(value: string, secretKey: string): Promise<strin
|
|
|
28
32
|
* @param encryptedValue The encrypted string (IV_hex + ciphertext_hex).
|
|
29
33
|
* @param secretKey The secret key for decryption.
|
|
30
34
|
* @returns A promise that resolves with the decrypted value.
|
|
35
|
+
* @throws {Error} If encryptedValue is null or empty, too short,
|
|
36
|
+
* or if the IV/ciphertext have incorrect lengths after conversion.
|
|
37
|
+
* @throws {Error} If the secretKey is null or empty (via importKey).
|
|
38
|
+
* @throws {TypeError} If hex2ab receives an invalid input type.
|
|
31
39
|
*/
|
|
32
40
|
export declare function decrypt(encryptedValue: string, secretKey: string): Promise<string>;
|