From 504cbe1c2309ddee41ad026c686bfa86d32508e1 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sat, 22 Nov 2025 17:49:01 +0100 Subject: [PATCH] feat(JWT Parser): secret encoding (text, hex array or base64) Fix #208 --- locales/en.yml | 7 +++++ src/tools/jwt-parser/jwt-parser.vue | 44 ++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/locales/en.yml b/locales/en.yml index 40a23ee2..64728036 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -5312,6 +5312,13 @@ tools: label-secret-or-public-key-spki-or-jwk: Secret or Public Key (SPKI or JWK) placeholder-put-your-secret-or-public-key-here: Put your secret or Public key here... tag-signature-verified: Signature verified + label-jwk: JWK Key + label-pub-key: PEM Public Key + label-secret-raw-text: Secret - Raw Text + label-secret-hex-array: Secret - Hex Array + label-secret-base64: Secret - Base64 + label-secret-encoding: 'Secret Encoding/Type:' + key-or-secret-or-verification-error-e-tostring: 'Key or secret or verification error: {0}' constants: text: token-introspection-response: Token introspection response diff --git a/src/tools/jwt-parser/jwt-parser.vue b/src/tools/jwt-parser/jwt-parser.vue index 49b48000..5a567f68 100644 --- a/src/tools/jwt-parser/jwt-parser.vue +++ b/src/tools/jwt-parser/jwt-parser.vue @@ -2,6 +2,8 @@ import { useI18n } from 'vue-i18n'; import * as jose from 'jose'; import JSON5 from 'json5'; +import hexArray from 'hex-array'; +import { Base64 } from 'js-base64'; import { decodeJwt, getJwtAlgorithm } from './jwt-parser.service'; import { useValidation } from '@/composable/validation'; import { isNotThrowing } from '@/utils/boolean'; @@ -33,6 +35,14 @@ const validation = useValidation({ }); const secretOrPublicKey = ref(''); +const secretEncoding = ref<'text' | 'base64' | 'hex' | 'key' | 'jwk'>('text'); +const encodings = [ + { label: t('tools.jwt-parser.texts.label-jwk'), value: 'jwk' }, + { label: t('tools.jwt-parser.texts.label-pub-key'), value: 'key' }, + { label: t('tools.jwt-parser.texts.label-secret-raw-text'), value: 'text' }, + { label: t('tools.jwt-parser.texts.label-secret-hex-array'), value: 'hex' }, + { label: t('tools.jwt-parser.texts.label-secret-base64'), value: 'base64' }, +]; const signatureVerification = computedAsync(async () => { const secretOrPublicKeyValue = secretOrPublicKey.value?.trim(); if (!secretOrPublicKeyValue) { @@ -45,25 +55,44 @@ const signatureVerification = computedAsync(async () => { try { const alg = getJwtAlgorithm({ jwt }) || 'unk'; - if (secretOrPublicKeyValue.startsWith('{')) { + if (secretEncoding.value === 'jwk' || secretOrPublicKeyValue.startsWith('{')) { const jwk = JSON5.parse(secretOrPublicKeyValue); const publicKey = await jose.importJWK(jwk, alg); await jose.jwtVerify(jwt, publicKey); } - else if (secretOrPublicKeyValue.includes('-----BEGIN PUBLIC KEY-----')) { + else if (secretEncoding.value === 'key' || secretOrPublicKeyValue.includes('-----BEGIN PUBLIC KEY-----')) { const publicKey = await jose.importSPKI(secretOrPublicKeyValue, alg); await jose.jwtVerify(jwt, publicKey); } else { - const secret = new TextEncoder().encode(secretOrPublicKeyValue); + let secret; + try { + switch (secretEncoding.value) { + case 'text': + secret = new TextEncoder().encode(secretOrPublicKeyValue); + break; + case 'hex': + secret = hexArray.fromString(secretOrPublicKeyValue); + break; + case 'base64': + if (!Base64.isValid(secretOrPublicKeyValue)) { + throw new Error(t('tools.jwt-generator.texts.invalid-base64-string')); + } + secret = Base64.toUint8Array(secretOrPublicKeyValue); + break; + } + } + catch (parseError: any) { + throw new Error(t('tools.jwt-generator.texts.cannot-parse-secret-as-encoding', [secretOrPublicKeyValue, secretEncoding.value, parseError])); + } await jose.jwtVerify(jwt, secret); } return { error: '' }; } catch (e: any) { - return { error: `Key or secret or verification error: ${e.toString()}` }; + return { error: t('tools.jwt-parser.texts.key-or-secret-or-verification-error-e-tostring', [e.toString()]) }; } }); @@ -99,6 +128,13 @@ const signatureVerification = computedAsync(async () => { +