Skip to content

Commit 441c139

Browse files
committed
2 parents 4caddac + 56fb30a commit 441c139

File tree

7 files changed

+317
-186
lines changed

7 files changed

+317
-186
lines changed

lib/eSignature/controllers/eg001EmbeddedSigning.js

Lines changed: 113 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,115 +4,116 @@
44
* @author DocuSign
55
*/
66

7-
const path = require("path");
8-
const { sendEnvelopeForEmbeddedSigning } = require("../../../embeddedSigning");
9-
const validator = require("validator");
10-
const { getExampleByNumber } = require("../../manifestService");
11-
const dsConfig = require("../../../config/index.js").config;
12-
13-
const eg001EmbeddedSigning = exports;
14-
const exampleNumber = 1;
15-
const eg = `eg00${exampleNumber}`; // This example reference.
16-
const mustAuthenticate = "/ds/mustAuthenticate";
17-
const minimumBufferMin = 3;
18-
const signerClientId = 1000; // The id of the signer within this application.
19-
const demoDocsPath = path.resolve(__dirname, "../../../demo_documents");
20-
const pdf1File = "World_Wide_Corp_lorem.pdf";
21-
const dsReturnUrl = dsConfig.appUrl + "/ds-return";
22-
const dsPingUrl = dsConfig.appUrl + "/"; // Url that will be pinged by the DocuSign signing via Ajax
23-
24-
/**
25-
* Create the envelope, the embedded signing, and then redirect to the DocuSign signing
26-
* @param {object} req Request obj
27-
* @param {object} res Response obj
28-
*/
29-
eg001EmbeddedSigning.createController = async (req, res) => {
30-
// Step 1. Check the token
31-
// At this point we should have a good token. But we
32-
// double-check here to enable a better UX to the user.
33-
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
34-
if (!isTokenOK) {
35-
req.flash("info", "Sorry, you need to re-authenticate.");
36-
// Save the current operation so it will be resumed after authentication
37-
req.dsAuth.setEg(req, eg);
38-
return res.redirect(mustAuthenticate);
39-
}
40-
41-
// Step 2. Call the worker method
42-
const { body } = req;
43-
const envelopeArgs = {
44-
signerEmail: validator.escape(body.signerEmail),
45-
signerName: validator.escape(body.signerName),
46-
signerClientId: signerClientId,
47-
dsReturnUrl: dsReturnUrl,
48-
dsPingUrl: dsPingUrl,
49-
docFile: path.resolve(demoDocsPath, pdf1File),
50-
};
51-
const args = {
52-
accessToken: req.user.accessToken,
53-
basePath: req.session.basePath,
54-
accountId: req.session.accountId,
55-
envelopeArgs: envelopeArgs,
56-
};
57-
let results = null;
58-
59-
try {
60-
results = await sendEnvelopeForEmbeddedSigning(args);
61-
} catch (error) {
62-
const errorBody = error && error.response && error.response.body;
63-
// we can pull the DocuSign error code and message from the response body
64-
const errorCode = errorBody && errorBody.errorCode;
65-
const errorMessage = errorBody && errorBody.message;
66-
// In production, may want to provide customized error messages and
67-
// remediation advice to the user.
68-
res.render("pages/error", { err: error, errorCode, errorMessage });
69-
}
70-
if (results) {
71-
// Redirect the user to the embedded signing
72-
// Don't use an iFrame!
73-
// State can be stored/recovered using the framework's session or a
74-
// query parameter on the returnUrl (see the makeRecipientViewRequest method)
75-
res.redirect(results.redirectUrl);
76-
}
77-
};
78-
79-
/**
80-
* Form page for this application
81-
*/
82-
eg001EmbeddedSigning.getController = (req, res) => {
83-
console.log(req.dsAuth);
84-
// Check that the authentication token is ok with a long buffer time.
85-
// If needed, now is the best time to ask the user to authenticate
86-
// since they have not yet entered any information into the form.
87-
const isTokenOK = req.dsAuth.checkToken();
88-
if (!isTokenOK) {
89-
// Save the current operation so it will be resumed after authentication
90-
req.dsAuth.setEg(req, eg);
91-
return res.redirect(mustAuthenticate);
92-
}
93-
94-
const example = getExampleByNumber(res.locals.manifest, exampleNumber);
95-
if (res.locals.quickACG) {
96-
res.render("pages/examples/quickEmbeddedSigning", {
97-
eg: eg,
98-
csrfToken: req.csrfToken(),
99-
title: "Use embedded signing",
100-
sourceFile: path.basename(__filename),
101-
sourceUrl:
102-
"https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js",
103-
documentation: dsConfig.documentation + eg,
104-
showDoc: dsConfig.documentation,
105-
});
106-
} else {
107-
res.render("pages/examples/eg001EmbeddedSigning", {
108-
eg: eg,
109-
csrfToken: req.csrfToken(),
110-
example: example,
111-
sourceFile: path.basename(__filename),
112-
sourceUrl:
113-
"https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js",
114-
documentation: dsConfig.documentation + eg,
115-
showDoc: dsConfig.documentation,
116-
});
117-
}
118-
};
7+
const path = require("path");
8+
const { sendEnvelopeForEmbeddedSigning } = require("../../../embeddedSigning");
9+
const validator = require("validator");
10+
const { getExampleByNumber } = require("../../manifestService");
11+
const dsConfig = require("../../../config/index.js").config;
12+
13+
const eg001EmbeddedSigning = exports;
14+
const exampleNumber = 1;
15+
const eg = `eg00${exampleNumber}`; // This example reference.
16+
const mustAuthenticate = "/ds/mustAuthenticate";
17+
const minimumBufferMin = 3;
18+
const signerClientId = 1000; // The id of the signer within this application.
19+
const demoDocsPath = path.resolve(__dirname, "../../../demo_documents");
20+
const pdf1File = "World_Wide_Corp_lorem.pdf";
21+
const dsReturnUrl = dsConfig.appUrl + "/ds-return";
22+
const dsPingUrl = dsConfig.appUrl + "/"; // Url that will be pinged by the DocuSign signing via Ajax
23+
24+
/**
25+
* Create the envelope, the embedded signing, and then redirect to the DocuSign signing
26+
* @param {object} req Request obj
27+
* @param {object} res Response obj
28+
*/
29+
eg001EmbeddedSigning.createController = async (req, res) => {
30+
// Step 1. Check the token
31+
// At this point we should have a good token. But we
32+
// double-check here to enable a better UX to the user.
33+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
34+
if (!isTokenOK) {
35+
req.flash("info", "Sorry, you need to re-authenticate.");
36+
// Save the current operation so it will be resumed after authentication
37+
req.dsAuth.setEg(req, eg);
38+
return res.redirect(mustAuthenticate);
39+
}
40+
41+
// Step 2. Call the worker method
42+
const { body } = req;
43+
const envelopeArgs = {
44+
signerEmail: validator.escape(body.signerEmail),
45+
signerName: validator.escape(body.signerName),
46+
signerClientId: signerClientId,
47+
dsReturnUrl: dsReturnUrl,
48+
dsPingUrl: dsPingUrl,
49+
docFile: path.resolve(demoDocsPath, pdf1File),
50+
};
51+
const args = {
52+
accessToken: req.user.accessToken,
53+
basePath: req.session.basePath,
54+
accountId: req.session.accountId,
55+
envelopeArgs: envelopeArgs,
56+
};
57+
let results = null;
58+
59+
try {
60+
results = await sendEnvelopeForEmbeddedSigning(args);
61+
} catch (error) {
62+
const errorBody = error && error.response && error.response.body;
63+
// we can pull the DocuSign error code and message from the response body
64+
const errorCode = errorBody && errorBody.errorCode;
65+
const errorMessage = errorBody && errorBody.message;
66+
// In production, may want to provide customized error messages and
67+
// remediation advice to the user.
68+
res.render("pages/error", { err: error, errorCode, errorMessage });
69+
}
70+
if (results) {
71+
// Redirect the user to the embedded signing
72+
// Don't use an iFrame!
73+
// State can be stored/recovered using the framework's session or a
74+
// query parameter on the returnUrl (see the makeRecipientViewRequest method)
75+
res.redirect(results.redirectUrl);
76+
}
77+
};
78+
79+
/**
80+
* Form page for this application
81+
*/
82+
eg001EmbeddedSigning.getController = (req, res) => {
83+
console.log(req.dsAuth);
84+
// Check that the authentication token is ok with a long buffer time.
85+
// If needed, now is the best time to ask the user to authenticate
86+
// since they have not yet entered any information into the form.
87+
const isTokenOK = req.dsAuth.checkToken();
88+
if (!isTokenOK) {
89+
// Save the current operation so it will be resumed after authentication
90+
req.dsAuth.setEg(req, eg);
91+
return res.redirect(mustAuthenticate);
92+
}
93+
94+
const example = getExampleByNumber(res.locals.manifest, exampleNumber);
95+
if (res.locals.quickACG) {
96+
res.render("pages/examples/quickEmbeddedSigning", {
97+
eg: eg,
98+
csrfToken: req.csrfToken(),
99+
title: "Use embedded signing",
100+
example: example,
101+
sourceFile: path.basename(__filename),
102+
sourceUrl:
103+
"https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js",
104+
documentation: dsConfig.documentation + eg,
105+
showDoc: dsConfig.documentation,
106+
});
107+
} else {
108+
res.render("pages/examples/eg001EmbeddedSigning", {
109+
eg: eg,
110+
csrfToken: req.csrfToken(),
111+
example: example,
112+
sourceFile: path.basename(__filename),
113+
sourceUrl:
114+
"https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js",
115+
documentation: dsConfig.documentation + eg,
116+
showDoc: dsConfig.documentation,
117+
});
118+
}
119+
};

quick_acg/package-lock.json

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

quick_acg/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"passport": "^0.4.0",
3232
"passport-docusign": "^1.1.0",
3333
"path": "^0.12.7",
34-
"validator": "^13.7.0"
34+
"validator": "^13.7.0",
35+
"node-fetch": "2.6.7"
3536
},
3637
"devDependencies": {
3738
"chai": "^4.2.0",

quick_acg/quickACG.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const flash = require('express-flash');
1717
const helmet = require('helmet'); // https://expressjs.com/en/advanced/best-practice-security.html
1818
const moment = require('moment');
1919
const csrf = require('csurf'); // https://www.npmjs.com/package/csurf
20+
const { getManifest } = require('../lib/manifestService');
2021

2122
const eg001 = require('../lib/eSignature/controllers/eg001EmbeddedSigning');
2223

@@ -63,6 +64,14 @@ let app = express()
6364
req.dsAuth = req.dsAuthCodeGrant;
6465
next()
6566
})
67+
.use(async (req, res, next) => {
68+
let manifestUrl = dsConfig.eSignManifestUrl;
69+
70+
const manifest = await getManifest(manifestUrl);
71+
res.locals.manifest = manifest;
72+
73+
next();
74+
})
6675
.use(csrfProtection) // CSRF protection for the following routes
6776
// Routes
6877
.get('/', redirectEg001)
@@ -138,4 +147,4 @@ if (!dsConfig.allowSilentAuthentication) {
138147
}
139148
}
140149

141-
passport.use(docusignStrategy);
150+
passport.use(docusignStrategy);

0 commit comments

Comments
 (0)