Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
// The arguments that should be passed to the VC generator.
"generatorOptions":"",

// This section overrides the above binary settings in favor of a
// REST API for credential issuance and presentation
"restapi": {
// URL of REST API
"baseUrl": "https://example.com",
// Issuance endpoint
"generator": "/credentials/issue",
// Presentation endpoint
"presentationGenerator": "/presentations/prove",
// Options to be based in request body
"generatorOptions": {},
// Authorization token type
"oauthTokenType": "Bearer",
// Authorization token
"oauthToken": ""
},

// Add section features you are explicitly not supporting
// For example, to exclude all features, you would:
// "sectionsNotSupported": ["basic", ""schema", "refresh", "evidence", "status", "tou", "ldp", "jwt", "zkp"]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@decentralized-identity/did-auth-jose": "^0.1.13"
},
"devDependencies": {
"axios": "^1.5.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"mocha": "^6.1.4"
Expand Down
77 changes: 76 additions & 1 deletion test/vc-data-model-1.0/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,39 @@
'use strict';
const path = require('path');
const util = require('util');
const fs = require('fs');
const axios = require('axios');

const exec = util.promisify(require('child_process').exec);

async function generate(file, options) {
if (options.restapi) {
return await generateFromRestapi(file, options.restapi);
} else {
return await generateFromBinary(file, options);
}
}

async function generateFromRestapi(file, options) {
const url = `${options.baseUrl}${options.generator}`;
const fileContent = fs.readFileSync(path.join(__dirname, 'input', file), 'utf8');
const dataToIssue = createIssuanceData(fileContent, options.generatorOptions);
const axiosOptions = {
timeout: 2000,
headers: {
'Content-Type': 'application/json',
'Authorization': `${options.oauthTokenType} ${options.oauthToken}`
}
}
try {
const response = await axios.post(url, dataToIssue, axiosOptions);
return response.data?.verifiableCredential ? response.data.verifiableCredential : {};
} catch (error) {
throw new Error('Failed to get valid response');
}
}

async function generateFromBinary(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.generator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
Expand All @@ -32,8 +61,34 @@ async function generateJwt(file, options) {
return stdout;
}


async function generatePresentation(file, options) {
if (options.restapi) {
return await generatePresentationFromRestapi(file, options.restapi);
} else {
return await generatePresentationFromBinary(file, options);
}
}

async function generatePresentationFromRestapi(file, options) {
const fileContent = fs.readFileSync(path.join(__dirname, 'input', file), 'utf8');
const dataToIssue = createPresentationData(fileContent, options.generatorOptions);
const url = `${options.baseUrl}${options.presentationGenerator}`;
const axiosOptions = {
timeout: 2000,
headers: {
'Content-Type': 'application/json',
'Authorization': `${options.oauthTokenType} ${options.oauthToken}`
}
}
try {
const response = await axios.post(url, dataToIssue, axiosOptions);
return response.data?.verifiablePresentation ? response.data.verifiablePresentation : {};
} catch (error) {
throw new Error('Failed to get valid response');
}
}

async function generatePresentationFromBinary(file, options) {
options = options || {};
const {stdout, stderr} = await exec(options.presentationGenerator + ' ' +
options.generatorOptions + ' ' + path.join(__dirname, 'input', file));
Expand Down Expand Up @@ -78,6 +133,26 @@ const RFC3339regex = new RegExp('^(\\d{4})-(0[1-9]|1[0-2])-' +
'(\\.[0-9]+)?(Z|(\\+|-)([01][0-9]|2[0-3]):' +
'([0-5][0-9]))$', 'i');

/**
* Transform a test input JSON-LD file to a payload for a REST credentials/issue
* @param {String} unsigned_jsonld A string of an unsigned credential JSON-LD
* @param {Object} options The arguments that should be passed to the VC generator
* @returns {String} A JSON-LD string to be used against REST APIs
*/
function createIssuanceData(unsigned_jsonld, options) {
return JSON.stringify({ credential: JSON.parse(unsigned_jsonld), options });
}

/**
* Transform a test input JSON-LD file to a payload for a REST presentation/prove
* @param {String} unsigned_jsonld A string of an unsigned presentation JSON-LD
* @param {String} options The arguments that should be passed to the VP generator
* @returns {String} A JSON-LD string to be used against REST APIs
*/
function createPresentationData(unsigned_jsonld, options) {
return JSON.stringify({ presentation: JSON.parse(unsigned_jsonld), options });
}

module.exports = {
generate,
generatePresentation,
Expand Down