Receiving an error of "CryptoKey does not support this operation, its algorithm.name must be AES-KW" #504
-
What happened?I am receiving an error of CryptoKey does not support this operation, its algorithm.name must be AES-KW when using the For background, I am building a Javascript SDK using Typescript, which then eventually gets compiled to vanilla Javascript using webpack. The vanilla javascript is then hosted on a URL and used in a The code snippet below has been used multiple times, but using ReactJS (v17.0.2) and jose (v4.12.0), which works as intended. Just now that when I did not use ReactJS, the error is received. I've tried using the same jose version (v4.12.0) to my existing project, but receiving errors related to the WebAPI Crypto) This is the code that executes the functions provided below: const jweTokenSecretKey: KeyLike | Uint8Array = await createSecretKey(
secretKey
);
const jweToken: string = await createAccessToken(
JSON.stringify({...}),
jweTokenSecretKey
); Version^4.12.0 RuntimeBrowser Runtime DetailsNode v16.14.2 Code to reproduceimport {
CompactEncrypt,
compactDecrypt,
CompactDecryptResult,
KeyLike,
importJWK,
} from 'jose';
export const createSecretKey = async (
secretKey?: string
): Promise<KeyLike | Uint8Array> => {
const JWK_ALGORITHM = 'HS256';
const JWE_DECRYPT_KEY =
secretKey || '...';
return importJWK(
{
kty: 'oct',
use: 'enc',
k: JWE_DECRYPT_KEY,
},
JWK_ALGORITHM
);
};
export const createAccessToken = async (
payload: any,
secretKey: KeyLike | Uint8Array
): Promise<string> => {
const JWE_ALGORITHM = 'A256KW';
const JWE_ENCRYPTION_ALGORITHM = 'A256CBC-HS512';
const encoder = new TextEncoder();
return new CompactEncrypt(encoder.encode(payload))
.setProtectedHeader({
alg: JWE_ALGORITHM,
enc: JWE_ENCRYPTION_ALGORITHM,
})
.encrypt(secretKey);
};
export const decryptAccessToken = async (
token: string,
secretKey: KeyLike | Uint8Array
): Promise<string> => {
const decoder = new TextDecoder();
const { plaintext }: CompactDecryptResult = await compactDecrypt(
token,
secretKey
);
return decoder.decode(plaintext);
}; Required
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Use the JWE_ALGORITHM value instead of JWK_ALGORITHM in importJWK. |
Beta Was this translation helpful? Give feedback.
Use the JWE_ALGORITHM value instead of JWK_ALGORITHM in importJWK.