|
2 | 2 | <form class="space-y-3"> |
3 | 3 | <div> |
4 | 4 | <div v-if="error" class="text-red-500">{{ error }}</div> |
5 | | - <textarea v-model="localValue" rows="10" cols="30"></textarea> |
| 5 | + <textarea v-model="localValue" rows="10" cols="30" placeholder="Verifiable Credential as JSON or JWT"></textarea> |
6 | 6 | </div> |
7 | 7 | </form> |
8 | 8 | </template> |
9 | 9 | <script> |
10 | 10 | export default { |
11 | 11 | props: { |
12 | | - value: Object, |
| 12 | + value: Object | String, |
13 | 13 | mode: String |
14 | 14 | }, |
15 | | - data () { |
| 15 | + data() { |
16 | 16 | return { |
| 17 | + error: undefined, |
17 | 18 | localValue: JSON.stringify(this.value, null, 2) |
18 | 19 | } |
19 | 20 | }, |
20 | 21 | emits: ['input'], |
21 | 22 | watch: { |
22 | | - localValue (newValue) { |
23 | | - try { |
24 | | - this.error = ''; |
25 | | - let parsedInput = JSON.parse(newValue) |
26 | | - this.$emit('input', parsedInput); |
27 | | - } catch (e) { |
28 | | - this.error = 'Invalid JSON format'; |
29 | | - } |
| 23 | + localValue(newValue) { |
| 24 | + // if starts with a {, assume JSON. Otherwise, parse as JWT. |
| 25 | + this.error = ''; |
| 26 | + if (newValue.startsWith('{')) { |
| 27 | + try { |
| 28 | + let parsedInput = JSON.parse(newValue) |
| 29 | + this.$emit('input', parsedInput); |
| 30 | + } catch (e) { |
| 31 | + this.error = 'Invalid JSON format'; |
| 32 | + } |
| 33 | + } else { |
| 34 | + // simplistic parsing as form of validation |
| 35 | + let parts = newValue.split('.'); |
| 36 | + try { |
| 37 | + if (parts.length !== 3) { |
| 38 | + this.error = 'Invalid JWT format'; |
| 39 | + } else { |
| 40 | + // first and second part should be base64 encoded JSON |
| 41 | + JSON.parse(atob(parts[0])); |
| 42 | + JSON.parse(atob(parts[1])); |
| 43 | + } |
| 44 | + this.$emit('input', newValue); |
| 45 | + } catch (e) { |
| 46 | + console.log(e); |
| 47 | + this.error = 'Invalid JWT format'; |
| 48 | + } |
| 49 | + } |
30 | 50 | } |
31 | 51 | } |
32 | 52 | } |
|
0 commit comments