Skip to content

Commit

Permalink
Support uploading credentials in JWT format
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 12, 2024
1 parent f37cdb7 commit 033414d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
6 changes: 3 additions & 3 deletions web/src/admin/credentials/UploadCredential.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
title="Upload a Credential JSON" type="add">

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

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

<div class="mt-4">
<upload-credential-form mode="new" :value="credential" @input="(credentialPaste)=> {credential = credentialPaste}"/>
<upload-credential-form mode="new" :value="credential" @input="(credentialPaste)=> {credential = credentialPaste}"/>
</div>
</modal-window>
</template>
Expand All @@ -32,7 +32,7 @@ export default {
return {
apiError: '',
subjectID: '',
credential: {}
credential: undefined,
}
},
methods: {
Expand Down
42 changes: 31 additions & 11 deletions web/src/admin/credentials/UploadCredentialForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,51 @@
<form class="space-y-3">
<div>
<div v-if="error" class="text-red-500">{{ error }}</div>
<textarea v-model="localValue" rows="10" cols="30"></textarea>
<textarea v-model="localValue" rows="10" cols="30" placeholder="Verifiable Credential as JSON or JWT"></textarea>
</div>
</form>
</template>
<script>
export default {
props: {
value: Object,
value: Object | String,
mode: String
},
data () {
data() {
return {
error: undefined,
localValue: JSON.stringify(this.value, null, 2)
}
},
emits: ['input'],
watch: {
localValue (newValue) {
try {
this.error = '';
let parsedInput = JSON.parse(newValue)
this.$emit('input', parsedInput);
} catch (e) {
this.error = 'Invalid JSON format';
}
localValue(newValue) {
// if starts with a {, assume JSON. Otherwise, parse as JWT.
this.error = '';
if (newValue.startsWith('{')) {
try {
let parsedInput = JSON.parse(newValue)
this.$emit('input', parsedInput);
} catch (e) {
this.error = 'Invalid JSON format';
}
} else {
// simplistic parsing as form of validation
let parts = newValue.split('.');
try {
if (parts.length !== 3) {
this.error = 'Invalid JWT format';
} else {
// first and second part should be base64 encoded JSON
JSON.parse(atob(parts[0]));
JSON.parse(atob(parts[1]));
}
this.$emit('input', newValue);
} catch (e) {
console.log(e);
this.error = 'Invalid JWT format';
}
}
}
}
}
Expand Down

0 comments on commit 033414d

Please sign in to comment.