it-tools/src/tools/jwt-parser/jwt-parser.vue

68 lines
2 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import { decodeJwt } from './jwt-parser.service';
import { useValidation } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
2023-01-13 13:59:27 +01:00
import { withDefaultOnError } from '@/utils/defaults';
const rawJwt = ref(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
);
2023-01-13 13:59:27 +01:00
const decodedJWT = computed(() =>
withDefaultOnError(() => decodeJwt({ jwt: rawJwt.value }), { header: [], payload: [] }),
);
const sections = [
{ key: 'header', title: 'Header' },
{ key: 'payload', title: 'Payload' },
] as const;
const validation = useValidation({
source: rawJwt,
rules: [
{
validator: value => value.length > 0 && isNotThrowing(() => decodeJwt({ jwt: rawJwt.value })),
message: 'Invalid JWT',
},
],
});
</script>
<template>
<c-card>
<c-input-text v-model:value="rawJwt" label="JWT to decode" :validation="validation" placeholder="Put your token here..." rows="5" multiline raw-text autofocus mb-3 />
<n-table v-if="validation.isValid">
<tbody>
<template v-for="section of sections" :key="section.key">
<th colspan="2" class="table-header">
{{ section.title }}
</th>
<tr v-for="{ claim, claimDescription, friendlyValue, value } in decodedJWT[section.key]" :key="claim + value">
<td class="claims">
2023-06-25 15:49:43 +02:00
<span font-bold>
{{ claim }}
2023-06-25 15:49:43 +02:00
</span>
<span v-if="claimDescription" ml-2 op-70>
({{ claimDescription }})
2023-06-25 15:49:43 +02:00
</span>
</td>
<td>
2023-06-25 15:49:43 +02:00
<span>{{ value }}</span>
<span v-if="friendlyValue" ml-2 op-70>
({{ friendlyValue }})
2023-06-25 15:49:43 +02:00
</span>
</td>
</tr>
</template>
</tbody>
</n-table>
</c-card>
</template>
<style lang="less" scoped>
.table-header {
text-align: center;
}
</style>