Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLM-1121 | Preview schema #1362

Merged
merged 4 commits into from
Dec 13, 2022
Merged
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
68 changes: 68 additions & 0 deletions backend/vc-management-service/management-service-swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,41 @@ paths:
description: Internal server error
schema:
$ref: '#/definitions/Error'
/v1/schema/preview:
post:
tags:
- preview
summary: create schema preview
consumes:
- application/json
produces:
- application/pdf
operationId: schemaPreviewV1
parameters:
- in: body
name: body
required: true
schema:
$ref: '#/definitions/GetSchemaPreview'
responses:
'200':
description: OK
content:
application/pdf:
schema:
type: file
'400':
description: Invalid input
schema:
$ref: '#/definitions/Error'
'401':
description: Unauthorized
schema:
$ref: '#/definitions/Error'
'500':
description: Internal server error
schema:
$ref: '#/definitions/Error'
/v1/schema/{schemaId}:
get:
tags:
Expand Down Expand Up @@ -324,6 +359,39 @@ definitions:
required:
- name
- schema
GetSchemaPreview:
type: object
properties:
credentialTemplate:
type: object
properties:
context:
type: array
items:
type: string
type:
type: array
items:
type: string
credentialSubject:
type: object
issuer:
type: string
minLength: 1
issuanceDate:
type: string
minLength: 1
evidence:
type: object
data:
type: object
template:
type: string
minLength: 1
required:
- credentialTemplate
- data
- template
SchemaList:
type: object
properties:
Expand Down
8 changes: 7 additions & 1 deletion backend/vc-management-service/src/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const IS_MINIO = process.env.IS_MINIO === 'true';
const MINIO_BUCKET_NAME = process.env.MINIO_BUCKET_NAME || 'contexts';
const IS_CLOUD_STORAGE = process.env.IS_CLOUD_STORAGE || false;
const STORAGE_REGION = process.env.STORAGE_REGION;
const SUNBIRD_CERIFICATE_SIGNER_URL = process.env.SUNBIRD_CERIFICATE_SIGNER_URL || "";
const SUNBIRD_CERIFICATE_API_URL = process.env.SUNBIRD_CERIFICATE_API_URL || "";
const MINIO_REGISTRY_BUCKET = process.env.MINIO_REGISTRY_BUCKET || 'issuance';
module.exports = {
SUNBIRD_REGISTRY_URL,
MINIO_URL,
Expand All @@ -26,6 +29,7 @@ module.exports = {
MINIO_SECRETKEY,
MINIO_USESSL,
MINIO_BUCKET_NAME,
MINIO_REGISTRY_BUCKET,
REDIS_URL,
REDIS_ENABLED,
PORT,
Expand All @@ -38,5 +42,7 @@ module.exports = {
ROOT_URL,
IS_MINIO,
IS_CLOUD_STORAGE,
STORAGE_REGION
STORAGE_REGION,
SUNBIRD_CERIFICATE_SIGNER_URL,
SUNBIRD_CERIFICATE_API_URL
}
56 changes: 53 additions & 3 deletions backend/vc-management-service/src/controllers/schema.controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
const sunbirdRegistryService = require('../services/sunbird.service')
const {getFormData} = require("../utils/utils");
const {TENANT_NAME} = require("../configs/config");
const {TENANT_NAME,MINIO_REGISTRY_BUCKET,IS_MINIO,MINIO_PORT,MINIO_URL,MINIO_USESSL,MINIO_ACCESSKEY,MINIO_SECRETKEY} = require("../configs/config");
const {MINIO_URL_SCHEME, SUNBIRD_SCHEMA_ADD_URL, SUNBIRD_SCHEMA_UPDATE_URL, SUNBIRD_GET_SCHEMA_URL} = require("../configs/constants");
const axios = require("axios");
const {CustomError} = require("../models/error");
const {addMandatoryFields, validateSchema, updateSchemaTemplateUrls} = require('../utils/schema.utils')
const minio = require('minio');

let minioClient;
(async function() {
try {
let minioOptions = {
endPoint: MINIO_URL,
useSSL: MINIO_USESSL,
accessKey: MINIO_ACCESSKEY,
secretKey:MINIO_SECRETKEY
}
if(IS_MINIO) {
minioOptions = {
port: parseInt(MINIO_PORT),
...minioOptions
}
}
minioClient = new minio.Client(minioOptions);
ajay-beehyv marked this conversation as resolved.
Show resolved Hide resolved
} catch(err) {
console.error(err);
}
})();


async function createSchema(req, res) {
try {
Expand Down Expand Up @@ -104,11 +126,39 @@ async function updateTemplateUrls(req, res) {
});
}
}
async function previewSchema(req,res){
try {
const token = req.header("Authorization");
const {credentialTemplate, data, template} = req.body;
const createCertReq = {
credentialTemplate: credentialTemplate,
data : data
}
console.log("Create certificate request: ",createCertReq);
const createCertResp = await sunbirdRegistryService.createCertBySigner(createCertReq,token);
console.log("Signed certificate: ",createCertResp);
let templateSignedUrl = await minioClient.presignedGetObject(MINIO_REGISTRY_BUCKET, template, 24*60*60);
console.log("templateSignedUrl : ",templateSignedUrl)
const getCertReq = {
certificate: JSON.stringify(createCertResp),
templateUrl: templateSignedUrl
}
const acceptType = "application/pdf"
const getCert = await sunbirdRegistryService.getCertByApi(getCertReq,token,acceptType);
getCert.data.pipe(res);
}catch (err){
console.error(err);
res.status(err?.response?.status || err?.status || 500).json({
message: err?.response?.data || err?.message || err
});
}
}

module.exports = {
createSchema,
getSchema,
updateSchema,
updateTemplate,
updateTemplateUrls
updateTemplateUrls,
previewSchema
}
1 change: 1 addition & 0 deletions backend/vc-management-service/src/routes/schema.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const schemaController = require('../controllers/schema.controller');
const router = express.Router();

router.post(`/`, [tokenValidationMiddleware, addRoleIfNotPresent], schemaController.createSchema)
router.post(`/preview`, [tokenValidationMiddleware ], schemaController.previewSchema)
router.get(`/:schemaId?`, [tokenValidationMiddleware], schemaController.getSchema)
router.put(`/:schemaId/updateTemplate`, [tokenValidationMiddleware, upload.single('files')], schemaController.updateTemplate)
router.put(`/:schemaId/updateTemplateUrl`, [tokenValidationMiddleware], schemaController.updateTemplateUrls)
Expand Down
23 changes: 22 additions & 1 deletion backend/vc-management-service/src/services/sunbird.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ const getTransaction = async (transactionId, token) => {
});
}

const createCertBySigner = async (body,token) => {
const url = config.SUNBIRD_CERIFICATE_SIGNER_URL + "/sign" ;
return axios.post(url, body,{headers:{Authorization: token}})
.then(res => res.data)
.catch(error => {
console.log(error);
throw error;
});
}

const getCertByApi = async (body,token, acceptType) => {
const url = config.SUNBIRD_CERIFICATE_API_URL + "/api/v1/certificate" ;
return axios.post(url,body, {responseType: "stream",headers:{Authorization:token, Accept: acceptType}})
.catch(error =>{
console.log(error);
throw error;
});
}

module.exports = {
createTenant,
createEntity,
Expand All @@ -118,5 +137,7 @@ module.exports = {
updateEntity,
getEntity,
getTransaction,
getTenantId
getTenantId,
createCertBySigner,
getCertByApi
}
1 change: 1 addition & 0 deletions backend/vc-management-service/src/utils/schema.utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {MINIO_URL_SCHEME, MANDATORY_FIELDS, MANDATORY_EVIDENCE_FIELDS, SUNBIRD_SCHEMA_ADD_URL, SUNBIRD_SCHEMA_UPDATE_URL, SUNBIRD_GET_SCHEMA_URL} = require("../configs/constants");
const {CustomError} = require("../models/error");
const sunbirdRegistryService = require('../services/sunbird.service')
const axios = require('axios');
ajay-beehyv marked this conversation as resolved.
Show resolved Hide resolved


async function updateSchemaTemplateUrls(urlMap, schemaId, token) {
Expand Down
3 changes: 3 additions & 0 deletions docker-compose-vc-issuance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ services:
# MINIO_ACCESSKEY: 'AKIAZ4W766BK5Z3WC65T'
# MINIO_SECRETKEY: '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'
MINIO_BUCKET_NAME: 'divoc-vc-storage'
MINIO_REGISTRY_BUCKET: 'issuance'
ROOT_URL: ''
REDIS_ENABLED: 'true'
REDIS_URL: 'redis://redis:6379'
Expand All @@ -211,6 +212,8 @@ services:
SUNBIRD_REGISTRY_FRONTEND_CLIENT: 'registry-frontend'
KEYCLOCK_TOKEN_TYPE_REFRESH_TOKEN: 'urn:ietf:params:oauth:token-type:refresh_token'
KEYCLOCK_GRANT_TYPE_TOKEN_EXCHANGE: 'urn:ietf:params:oauth:grant-type:token-exchange'
SUNBIRD_CERIFICATE_SIGNER_URL: 'http://sunbird-certificate-signer:8079'
SUNBIRD_CERIFICATE_API_URL: 'http://sunbird-certificate-api:8078'
depends_on:
vc-registry:
condition: service_started
Expand Down