@arex95/vue-core 1.1.23 → 1.1.25
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 +42 -8
- 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
|
}
|
|
@@ -2617,7 +2651,7 @@ async function getDecryptedItem(key, secretKey, isRememberMe) {
|
|
|
2617
2651
|
const cleanCredentials = async (preference) => {
|
|
2618
2652
|
const tokensConfig = getTokenConfig();
|
|
2619
2653
|
Object.keys(tokensConfig).forEach((key) => {
|
|
2620
|
-
const storage = preference === "
|
|
2654
|
+
const storage = preference === "local" ? localStorage : sessionStorage;
|
|
2621
2655
|
storage.removeItem(tokensConfig[key]);
|
|
2622
2656
|
});
|
|
2623
2657
|
};
|
|
@@ -2631,7 +2665,7 @@ const cleanCredentials = async (preference) => {
|
|
|
2631
2665
|
*/
|
|
2632
2666
|
const getAuthToken = async (secretKey, preference) => {
|
|
2633
2667
|
const tokensConfig = getTokenConfig();
|
|
2634
|
-
return await getDecryptedItem(tokensConfig.ACCESS_TOKEN, secretKey, preference === "
|
|
2668
|
+
return await getDecryptedItem(tokensConfig.ACCESS_TOKEN, secretKey, preference === "local");
|
|
2635
2669
|
};
|
|
2636
2670
|
/**
|
|
2637
2671
|
* Retrieves the authentication refresh token from storage, decrypting it
|
|
@@ -2643,7 +2677,7 @@ const getAuthToken = async (secretKey, preference) => {
|
|
|
2643
2677
|
*/
|
|
2644
2678
|
const getAuthRefreshToken = async (secretKey, preference) => {
|
|
2645
2679
|
const tokensConfig = getTokenConfig();
|
|
2646
|
-
return await getDecryptedItem(tokensConfig.REFRESH_TOKEN, secretKey, preference === "
|
|
2680
|
+
return await getDecryptedItem(tokensConfig.REFRESH_TOKEN, secretKey, preference === "local");
|
|
2647
2681
|
};
|
|
2648
2682
|
/**
|
|
2649
2683
|
* Stores the authentication token (access token) in storage after encrypting it,
|
|
@@ -2656,7 +2690,7 @@ const getAuthRefreshToken = async (secretKey, preference) => {
|
|
|
2656
2690
|
*/
|
|
2657
2691
|
const storeAuthToken = async (token, secretKey, preference) => {
|
|
2658
2692
|
const tokensConfig = getTokenConfig();
|
|
2659
|
-
await storeEncryptedItem(tokensConfig.ACCESS_TOKEN, token, secretKey, preference === "
|
|
2693
|
+
await storeEncryptedItem(tokensConfig.ACCESS_TOKEN, token, secretKey, preference === "local");
|
|
2660
2694
|
};
|
|
2661
2695
|
/**
|
|
2662
2696
|
* Stores the authentication refresh token in storage after encrypting it,
|
|
@@ -2669,7 +2703,7 @@ const storeAuthToken = async (token, secretKey, preference) => {
|
|
|
2669
2703
|
*/
|
|
2670
2704
|
const storeAuthRefreshToken = async (token, secretKey, preference) => {
|
|
2671
2705
|
const tokensConfig = getTokenConfig();
|
|
2672
|
-
await storeEncryptedItem(tokensConfig.REFRESH_TOKEN, token, secretKey, preference === "
|
|
2706
|
+
await storeEncryptedItem(tokensConfig.REFRESH_TOKEN, token, secretKey, preference === "local");
|
|
2673
2707
|
};
|
|
2674
2708
|
/**
|
|
2675
2709
|
* Verifies the validity and expiration of the current authentication token.
|
|
@@ -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>;
|