diff --git a/locales/en.yml b/locales/en.yml index b0579bab..a6771330 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -2513,6 +2513,8 @@ tools: token-keys: Token Keys cannot-parse-secret-as-encoding: 'Cannot parse ''{0}'' as ''{1}'': {2}' invalid-base64-string: Invalid Base64 string + placeholder-input-token: Paste your JWT token to edit... + click-to-input-a-existing-jwt-token-to-modify: Click to input a existing JWT token to modify length-converter: title: Length Units Converter description: Convert values from length units diff --git a/src/tools/jwt-generator/jwt-generator.vue b/src/tools/jwt-generator/jwt-generator.vue index 23626260..3a254168 100644 --- a/src/tools/jwt-generator/jwt-generator.vue +++ b/src/tools/jwt-generator/jwt-generator.vue @@ -5,7 +5,10 @@ import { type KeyLike } from 'jose'; import JSON5 from 'json5'; import hexArray from 'hex-array'; import { Base64 } from 'js-base64'; +import { type JwtPayload, jwtDecode } from 'jwt-decode'; +import { getJwtAlgorithm } from '../jwt-parser/jwt-parser.service'; import { jwsAlgorithms } from './jwt-generator.constants'; + import { useValidation } from '@/composable/validation'; import { useQueryParamOrStorage } from '@/composable/queryParams'; @@ -21,6 +24,19 @@ const alg = useQueryParamOrStorage({ name: 'alg', storageName: 'jwt-gen:alg', de const algInfo = computed(() => jwsAlgorithms.find(a => a.alg === alg.value) || { alg: 'UNK', keyDesc: '', key: 'secret', verify: '' }); const isSecret = computed(() => algInfo.value.key === 'secret'); +const inputToken = ref(''); +const inputTokenError = ref(''); +watch(inputToken, (value) => { + inputTokenError.value = ''; + try { + payload.value = JSON.stringify(jwtDecode(value), null, 2); + alg.value = getJwtAlgorithm({ jwt: value }) || 'HS512'; + } + catch (e: any) { + inputTokenError.value = e.toString(); + } +}); + const secret = ref(''); const secretEncoding = ref<'text' | 'base64' | 'hex'>('text'); const encodings = [ @@ -139,6 +155,21 @@ const jsonInputValidation = useValidation({