|
| 1 | +const express = require('express'); |
| 2 | +const dotenv = require('dotenv'); |
| 3 | +const cors = require('cors'); |
| 4 | +const fs = require('node:fs'); |
| 5 | + |
| 6 | +const app = express(); |
| 7 | +dotenv.config(); |
| 8 | + |
| 9 | +app.use(cors()) |
| 10 | +// Middleware to handle raw body data |
| 11 | +app.use(express.raw({ type: '*/*' })); |
| 12 | + |
| 13 | +const defaultHeader = { |
| 14 | + 'Content-Type': "application/json", |
| 15 | + 'x-api-key': process.env.API_KEY, |
| 16 | + 'api-version': '1.0' |
| 17 | +}; |
| 18 | + |
| 19 | +// Admin Token + ApiKey are needed for approving |
| 20 | +const adminHeader = { |
| 21 | + 'Content-Type': "application/json", |
| 22 | + 'x-api-key': process.env.API_KEY, |
| 23 | + 'X-Incode-Hardware-Id': process.env.ADMIN_TOKEN, |
| 24 | + 'api-version': '1.0' |
| 25 | +}; |
| 26 | + |
| 27 | +// Receives the information about a faceMatch attempt and verifies |
| 28 | +// if it was correct and has not been tampered. |
| 29 | +app.post('/verify', async (req, res) => { |
| 30 | + /** Get parameters from body */ |
| 31 | + const faceMatchData = JSON.parse(req.body.toString()); |
| 32 | + const {transactionId, token, interviewToken} = faceMatchData; |
| 33 | + const verificationParams = { transactionId, token, interviewToken }; |
| 34 | + |
| 35 | + let response={}; |
| 36 | + try{ |
| 37 | + /** Run Call against incode API **/ |
| 38 | + const verifyAttemptUrl = `${process.env.API_URL}/omni/authentication/verify`; |
| 39 | + response = await doPost(verifyAttemptUrl, verificationParams, adminHeader); |
| 40 | + } catch(e) { |
| 41 | + console.log(e.message); |
| 42 | + res.status(500).send({success:false, error: e.message}); |
| 43 | + return; |
| 44 | + } |
| 45 | + log = { |
| 46 | + timestamp: new Date().toISOString().slice(0, 19).replace('T', ' '), |
| 47 | + data: {verificationParams, response} |
| 48 | + } |
| 49 | + res.status(200).send(response); |
| 50 | + |
| 51 | + // Write to a log so you can debug it. |
| 52 | + console.log(log); |
| 53 | +}); |
| 54 | + |
| 55 | +// if it was correct and has not been tampered. |
| 56 | +app.post('/sign', async (req, res) => { |
| 57 | + /** Receive contract and token as parameters */ |
| 58 | + const signParamsData = JSON.parse(req.body.toString()); |
| 59 | + const { interviewToken, base64Contract } = signParamsData; |
| 60 | + |
| 61 | + /** Prepare the authorization header that will be used in calls to incode */ |
| 62 | + sessionHeader = {...defaultHeader}; |
| 63 | + sessionHeader['X-Incode-Hardware-Id'] = interviewToken; |
| 64 | + |
| 65 | + let response = {}; |
| 66 | + try{ |
| 67 | + /** Get URL where to upload the contract */ |
| 68 | + const generateDocumentUploadUrl = `${process.env.API_URL}/omni/es/generateDocumentUploadUrl`; |
| 69 | + const documentURLData = await doPost(generateDocumentUploadUrl, { token:interviewToken }, sessionHeader); |
| 70 | + const {referenceId, preSignedUrl} = documentURLData |
| 71 | + |
| 72 | + /** Upload contract to AWS presigned url */ |
| 73 | + const binary = Buffer.from(base64Contract, 'base64'); |
| 74 | + const uploadResponse = await fetch(preSignedUrl, { |
| 75 | + method: "PUT", |
| 76 | + headers: {"Content-Type": "application/pdf"}, |
| 77 | + body: binary, |
| 78 | + }) |
| 79 | + if (!uploadResponse.ok) { |
| 80 | + throw new Error('Uploading contract failed with code ' + uploadResponse.status) |
| 81 | + } |
| 82 | + |
| 83 | + /** Sign the document */ |
| 84 | + const signURL = `${process.env.API_URL}/omni/es/process/sign`; |
| 85 | + const signData = await doPost(signURL, |
| 86 | + { |
| 87 | + "documentRef": referenceId, |
| 88 | + "userConsented": true |
| 89 | + }, |
| 90 | + sessionHeader |
| 91 | + ); |
| 92 | + const {success} = signData |
| 93 | + if (!success) { throw new Error('Sign failed');} |
| 94 | + |
| 95 | + /** Fetch all signed document references */ |
| 96 | + const documentsSignedURL = `${process.env.API_URL}/omni/es/documents/signed`; |
| 97 | + const documentsSignedData = await doGet(documentsSignedURL, {}, sessionHeader); |
| 98 | + const {documents} = documentsSignedData |
| 99 | + |
| 100 | + /** This endpoint returns all documents, find the one just signed matching by referenceId*/ |
| 101 | + const justSigned = documents.find(document => document.documentRef=== referenceId) |
| 102 | + |
| 103 | + /** Return referenceId and documentUrl */ |
| 104 | + const {documentRef, documentUrl} = justSigned |
| 105 | + response = {referenceId, documentUrl} |
| 106 | + |
| 107 | + } catch(e) { |
| 108 | + console.log(e.message); |
| 109 | + res.status(500).send({success:false, error: e.message}); |
| 110 | + return; |
| 111 | + } |
| 112 | + log = { |
| 113 | + timestamp: new Date().toISOString().slice(0, 19).replace('T', ' '), |
| 114 | + data: {signParamsData, response} |
| 115 | + } |
| 116 | + res.status(200).send(response); |
| 117 | + // Write to a log so you can debug it. |
| 118 | + console.log(log); |
| 119 | +}); |
| 120 | + |
| 121 | + |
| 122 | +app.get('*', function(req, res){ |
| 123 | + res.status(404).json({error: `Cannot GET ${req.url}`}); |
| 124 | +}); |
| 125 | + |
| 126 | +app.post('*', function(req, res){ |
| 127 | + res.status(404).json({error: `Cannot POST ${req.url}`}); |
| 128 | +}); |
| 129 | + |
| 130 | +// Utility functions |
| 131 | +const doPost = async (url, bodyparams, headers) => { |
| 132 | + try { |
| 133 | + const response = await fetch(url, { method: 'POST', body: JSON.stringify(bodyparams), headers}); |
| 134 | + if (!response.ok) { |
| 135 | + //console.log(response.json()); |
| 136 | + throw new Error('Request failed with code ' + response.status) |
| 137 | + } |
| 138 | + return response.json(); |
| 139 | + } catch(e) { |
| 140 | + console.log({url, bodyparams, headers}) |
| 141 | + throw new Error('HTTP Post Error: ' + e.message) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +const doGet = async (url, params, headers) => { |
| 146 | + try { |
| 147 | + const response = await fetch(`${url}?` + new URLSearchParams(params), {method: 'GET', headers}); |
| 148 | + if (!response.ok) { |
| 149 | + //console.log(await response.json()); |
| 150 | + throw new Error('Request failed with code ' + response.status) |
| 151 | + } |
| 152 | + return response.json(); |
| 153 | + } catch(e) { |
| 154 | + console.log({url, params, headers}) |
| 155 | + throw new Error('HTTP Get Error: ' + e.message) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +// Listen for HTTP |
| 161 | +const httpPort = 3000; |
| 162 | + |
| 163 | +app.listen(httpPort, () => { |
| 164 | + console.log(`HTTP listening on: http://localhost:${httpPort}/`); |
| 165 | +}); |
| 166 | + |
| 167 | +module.exports = app; |
0 commit comments