Skip to content

Commit 033414d

Browse files
committed
Support uploading credentials in JWT format
1 parent f37cdb7 commit 033414d

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

web/src/admin/credentials/UploadCredential.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
title="Upload a Credential JSON" type="add">
44

55
<p class="mb-3 text-sm">
6-
Here you can add a credential issued by another party to be stored in your wallet. Paste the JSON object below.
6+
Here you can add a credential issued by another party to be stored in your wallet. Paste the JSON object or JWT below.
77
</p>
88

99
<ErrorMessage v-if="apiError" :message="apiError" title="Could not upload credential"/>
1010

1111
<div class="mt-4">
12-
<upload-credential-form mode="new" :value="credential" @input="(credentialPaste)=> {credential = credentialPaste}"/>
12+
<upload-credential-form mode="new" :value="credential" @input="(credentialPaste)=> {credential = credentialPaste}"/>
1313
</div>
1414
</modal-window>
1515
</template>
@@ -32,7 +32,7 @@ export default {
3232
return {
3333
apiError: '',
3434
subjectID: '',
35-
credential: {}
35+
credential: undefined,
3636
}
3737
},
3838
methods: {

web/src/admin/credentials/UploadCredentialForm.vue

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,51 @@
22
<form class="space-y-3">
33
<div>
44
<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>
66
</div>
77
</form>
88
</template>
99
<script>
1010
export default {
1111
props: {
12-
value: Object,
12+
value: Object | String,
1313
mode: String
1414
},
15-
data () {
15+
data() {
1616
return {
17+
error: undefined,
1718
localValue: JSON.stringify(this.value, null, 2)
1819
}
1920
},
2021
emits: ['input'],
2122
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+
}
3050
}
3151
}
3252
}

0 commit comments

Comments
 (0)