|
1 |
| -#!/usr/bin/env node |
2 |
| - |
3 |
| -const express = require('express'); |
4 |
| -const session = require('express-session'); // https://github.com/expressjs/session |
5 |
| -const bodyParser = require('body-parser'); |
6 |
| -const cookieParser = require('cookie-parser'); |
7 |
| -const MemoryStore = require('memorystore')(session); // https://github.com/roccomuso/memorystore |
8 |
| -const path = require('path'); |
9 |
| -const DSAuthCodeGrant = require('../lib/DSAuthCodeGrant'); |
10 |
| -const passport = require('passport'); |
11 |
| -const DocusignStrategy = require('passport-docusign'); |
12 |
| -const docOptions = require('../config/documentOptions.json'); |
13 |
| -const docNames = require('../config/documentNames.json'); |
14 |
| -const dsConfig = require('../config/index.js').config; |
15 |
| -const commonControllers = require('../lib/commonControllers'); |
16 |
| -const flash = require('express-flash'); |
17 |
| -const helmet = require('helmet'); // https://expressjs.com/en/advanced/best-practice-security.html |
18 |
| -const moment = require('moment'); |
19 |
| -const csrf = require('csurf'); // https://www.npmjs.com/package/csurf |
20 |
| -const { getManifest } = require('../lib/manifestService'); |
21 |
| - |
22 |
| -const eg001 = require('../lib/eSignature/controllers/eg001EmbeddedSigning'); |
23 |
| -const eg041 = require('../lib/eSignature/controllers/eg041CFREmbeddedSigning'); |
24 |
| - |
25 |
| -const PORT = process.env.PORT || 3000; |
26 |
| -const HOST = process.env.HOST || 'localhost'; |
27 |
| -const max_session_min = 180; |
28 |
| -const csrfProtection = csrf({ cookie: true }); |
29 |
| - |
30 |
| -let hostUrl = 'http://' + HOST + ':' + PORT; |
31 |
| -if (dsConfig.appUrl !== '' && dsConfig.appUrl !== '{APP_URL}') { hostUrl = dsConfig.appUrl; } |
32 |
| - |
33 |
| -let app = express() |
34 |
| - .use(helmet()) |
35 |
| - .use(express.static(path.join(__dirname, 'public'))) |
36 |
| - .use(cookieParser()) |
37 |
| - .use(session({ |
38 |
| - secret: dsConfig.sessionSecret, |
39 |
| - name: 'ds-launcher-session', |
40 |
| - cookie: { maxAge: max_session_min * 60000 }, |
41 |
| - saveUninitialized: true, |
42 |
| - resave: true, |
43 |
| - store: new MemoryStore({ |
44 |
| - checkPeriod: 86400000 // prune expired entries every 24h |
45 |
| - }) |
46 |
| - })) |
47 |
| - .use(passport.initialize()) |
48 |
| - .use(passport.session()) |
49 |
| - .use(bodyParser.urlencoded({ extended: true })) |
50 |
| - .use((req, res, next) => { |
51 |
| - res.locals.user = req.user; |
52 |
| - res.locals.session = req.session; |
53 |
| - res.locals.dsConfig = { ...dsConfig, docOptions: docOptions, docNames: docNames}; |
54 |
| - res.locals.quickACG = true; |
55 |
| - res.locals.hostUrl = hostUrl; // Used by DSAuthCodeGrant#logout |
56 |
| - next(); |
57 |
| - }) // Send user info to views |
58 |
| - .use(flash()) |
59 |
| - .set('views', path.join(__dirname, '../views')) |
60 |
| - .set('view engine', 'ejs') |
61 |
| - // Add an instance of DSAuthCodeGrant to req |
62 |
| - .use((req, res, next) => { |
63 |
| - req.dsAuthCodeGrant = new DSAuthCodeGrant(req); |
64 |
| - req.dsAuth = req.dsAuthCodeGrant; |
65 |
| - next(); |
66 |
| - }) |
67 |
| - .use(async (req, res, next) => { |
68 |
| - let manifestUrl = dsConfig.codeExamplesManifest; |
69 |
| - |
70 |
| - const manifest = await getManifest(manifestUrl); |
71 |
| - res.locals.manifest = manifest; |
72 |
| - |
73 |
| - next(); |
74 |
| - }) |
75 |
| - .use(csrfProtection) // CSRF protection for the following routes |
76 |
| - // Routes |
77 |
| - .get('/', redirectEg001) |
78 |
| - .get('/eg001', eg001.getController) |
79 |
| - .post('/eg001', eg001.createController) |
80 |
| - .get('/eg041', eg041.getController) |
81 |
| - .post('/eg041', eg041.createController) |
82 |
| - |
83 |
| - .get('/ds/mustAuthenticate', redirectLogin) |
84 |
| - .get('/ds/login', commonControllers.login) |
85 |
| - .get('/ds-return', redirectReturn) |
86 |
| - .get('/ds/callback', [dsLoginCB1, dsLoginCB2]) // OAuth callbacks. See below |
87 |
| -; |
88 |
| - |
89 |
| -function redirectEg001(req, res) { return res.redirect('/eg001'); } |
90 |
| -function redirectLogin(req, res) { return res.redirect('/ds/login'); } |
91 |
| -function redirectReturn(req, res) { return res.redirect('/eg001'); } |
92 |
| -function dsLoginCB1(req, res, next) { req.dsAuthCodeGrant.oauth_callback1(req, res, next); } |
93 |
| -function dsLoginCB2(req, res, next) { req.dsAuthCodeGrant.oauth_callback2(req, res, next); } |
94 |
| - |
95 |
| -if (dsConfig.dsClientId && dsConfig.dsClientId !== '{CLIENT_ID}' && |
96 |
| - dsConfig.dsClientSecret && dsConfig.dsClientSecret !== '{CLIENT_SECRET}') { |
97 |
| - app.listen(PORT); |
98 |
| - console.log(`Listening on ${PORT}`); |
99 |
| - console.log(`Ready! Open ${hostUrl}`); |
100 |
| -} else { |
101 |
| - console.log(`PROBLEM: You need to set the clientId (Integrator Key), and perhaps other settings as well. |
102 |
| -You can set them in the configuration file config/appsettings.json or set environment variables.\n`); |
103 |
| - process.exit(); // We're not using exit code of 1 to avoid extraneous npm messages. |
104 |
| -} |
105 |
| - |
106 |
| -// Passport session setup. |
107 |
| -// To support persistent login sessions, Passport needs to be able to |
108 |
| -// serialize users into and deserialize users out of the session. Typically, |
109 |
| -// this will be as simple as storing the user ID when serializing, and finding |
110 |
| -// the user by ID when deserializing. However, since this example does not |
111 |
| -// have a database of user records, the complete DocuSign profile is serialized |
112 |
| -// and deserialized. |
113 |
| -passport.serializeUser(function(user, done) { done(null, user); }); |
114 |
| -passport.deserializeUser(function(obj, done) { done(null, obj); }); |
115 |
| - |
116 |
| -let scope = ['signature']; |
117 |
| -// Configure passport for DocusignStrategy |
118 |
| -let docusignStrategy = new DocusignStrategy({ |
119 |
| - production: dsConfig.production, |
120 |
| - clientID: dsConfig.dsClientId, |
121 |
| - scope: scope.join(' '), |
122 |
| - clientSecret: dsConfig.dsClientSecret, |
123 |
| - callbackURL: hostUrl + '/ds/callback', |
124 |
| - state: true // automatic CSRF protection. |
125 |
| - // See https://github.com/jaredhanson/passport-oauth2/blob/master/lib/state/session.js |
126 |
| - }, |
127 |
| - function _processDsResult(accessToken, refreshToken, params, profile, done) { |
128 |
| - // The params arg will be passed additional parameters of the grant. |
129 |
| - // See https://github.com/jaredhanson/passport-oauth2/pull/84 |
130 |
| - // |
131 |
| - // Here we're just assigning the tokens to the account object |
132 |
| - // We store the data in DSAuthCodeGrant.getDefaultAccountInfo |
133 |
| - let user = profile; |
134 |
| - user.accessToken = accessToken; |
135 |
| - user.refreshToken = refreshToken; |
136 |
| - user.expiresIn = params.expires_in; |
137 |
| - user.tokenExpirationTimestamp = moment().add(user.expiresIn, 's'); // The dateTime when the access token will expire |
138 |
| - return done(null, user); |
139 |
| - } |
140 |
| -); |
141 |
| - |
142 |
| -/** |
143 |
| - * The DocuSign OAuth default is to allow silent authentication. |
144 |
| - * An additional OAuth query parameter is used to not allow silent authentication |
145 |
| - */ |
146 |
| -if (!dsConfig.allowSilentAuthentication) { |
147 |
| - // See https://stackoverflow.com/a/32877712/64904 |
148 |
| - docusignStrategy.authorizationParams = function(options) { |
149 |
| - return { prompt: 'login' }; |
150 |
| - }; |
151 |
| -} |
152 |
| - |
153 |
| -passport.use(docusignStrategy); |
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const express = require('express'); |
| 4 | +const session = require('express-session'); // https://github.com/expressjs/session |
| 5 | +const bodyParser = require('body-parser'); |
| 6 | +const cookieParser = require('cookie-parser'); |
| 7 | +const MemoryStore = require('memorystore')(session); // https://github.com/roccomuso/memorystore |
| 8 | +const path = require('path'); |
| 9 | +const DSAuthCodeGrant = require('../lib/DSAuthCodeGrant'); |
| 10 | +const passport = require('passport'); |
| 11 | +const DocusignStrategy = require('passport-docusign'); |
| 12 | +const docOptions = require('../config/documentOptions.json'); |
| 13 | +const docNames = require('../config/documentNames.json'); |
| 14 | +const dsConfig = require('../config/index.js').config; |
| 15 | +const commonControllers = require('../lib/commonControllers'); |
| 16 | +const flash = require('express-flash'); |
| 17 | +const helmet = require('helmet'); // https://expressjs.com/en/advanced/best-practice-security.html |
| 18 | +const moment = require('moment'); |
| 19 | +const csrf = require('csurf'); // https://www.npmjs.com/package/csurf |
| 20 | +const { getManifest } = require('../lib/manifestService'); |
| 21 | + |
| 22 | +const eg001 = require('../lib/eSignature/controllers/eg001EmbeddedSigning'); |
| 23 | +const eg041 = require('../lib/eSignature/controllers/eg041CFREmbeddedSigning'); |
| 24 | + |
| 25 | +const PORT = process.env.PORT || 3000; |
| 26 | +const HOST = process.env.HOST || 'localhost'; |
| 27 | +const max_session_min = 180; |
| 28 | +const csrfProtection = csrf({ cookie: true }); |
| 29 | + |
| 30 | +let hostUrl = 'http://' + HOST + ':' + PORT; |
| 31 | +if (dsConfig.appUrl !== '' && dsConfig.appUrl !== '{APP_URL}') { hostUrl = dsConfig.appUrl; } |
| 32 | + |
| 33 | +let app = express() |
| 34 | + .use(helmet()) |
| 35 | + .use(express.static(path.join(__dirname, 'public'))) |
| 36 | + .use(cookieParser()) |
| 37 | + .use(session({ |
| 38 | + secret: dsConfig.sessionSecret, |
| 39 | + name: 'ds-launcher-session', |
| 40 | + cookie: { maxAge: max_session_min * 60000 }, |
| 41 | + saveUninitialized: true, |
| 42 | + resave: true, |
| 43 | + store: new MemoryStore({ |
| 44 | + checkPeriod: 86400000 // prune expired entries every 24h |
| 45 | + }) |
| 46 | + })) |
| 47 | + .use(passport.initialize()) |
| 48 | + .use(passport.session()) |
| 49 | + .use(bodyParser.urlencoded({ extended: true })) |
| 50 | + .use((req, res, next) => { |
| 51 | + res.locals.user = req.user; |
| 52 | + res.locals.session = req.session; |
| 53 | + res.locals.dsConfig = { ...dsConfig, docOptions: docOptions, docNames: docNames}; |
| 54 | + res.locals.quickACG = true; |
| 55 | + res.locals.hostUrl = hostUrl; // Used by DSAuthCodeGrant#logout |
| 56 | + next(); |
| 57 | + }) // Send user info to views |
| 58 | + .use(flash()) |
| 59 | + .set('views', path.join(__dirname, '../views')) |
| 60 | + .set('view engine', 'ejs') |
| 61 | + // Add an instance of DSAuthCodeGrant to req |
| 62 | + .use((req, res, next) => { |
| 63 | + req.dsAuthCodeGrant = new DSAuthCodeGrant(req); |
| 64 | + req.dsAuth = req.dsAuthCodeGrant; |
| 65 | + next(); |
| 66 | + }) |
| 67 | + .use(async (req, res, next) => { |
| 68 | + let manifestUrl = dsConfig.codeExamplesManifest; |
| 69 | + |
| 70 | + const manifest = await getManifest(manifestUrl); |
| 71 | + res.locals.manifest = manifest; |
| 72 | + |
| 73 | + next(); |
| 74 | + }) |
| 75 | + .use(csrfProtection) // CSRF protection for the following routes |
| 76 | + // Routes |
| 77 | + .get('/', redirectEg001) |
| 78 | + .get('/eg001', eg001.getController) |
| 79 | + .post('/eg001', eg001.createController) |
| 80 | + .get('/eg041', eg041.getController) |
| 81 | + .post('/eg041', eg041.createController) |
| 82 | + |
| 83 | + .get('/ds/mustAuthenticate', redirectLogin) |
| 84 | + .get('/ds/login', commonControllers.login) |
| 85 | + .get('/ds-return', redirectReturn) |
| 86 | + .get('/ds/callback', [dsLoginCB1, dsLoginCB2]) // OAuth callbacks. See below |
| 87 | +; |
| 88 | + |
| 89 | +function redirectEg001(req, res) { return res.redirect('/eg001'); } |
| 90 | +function redirectLogin(req, res) { return res.redirect('/ds/login'); } |
| 91 | +function redirectReturn(req, res) { return res.redirect('/eg001'); } |
| 92 | +function dsLoginCB1(req, res, next) { req.dsAuthCodeGrant.oauth_callback1(req, res, next); } |
| 93 | +function dsLoginCB2(req, res, next) { req.dsAuthCodeGrant.oauth_callback2(req, res, next); } |
| 94 | + |
| 95 | +if (dsConfig.dsClientId && dsConfig.dsClientId !== '{CLIENT_ID}' && |
| 96 | + dsConfig.dsClientSecret && dsConfig.dsClientSecret !== '{CLIENT_SECRET}') { |
| 97 | + app.listen(PORT); |
| 98 | + console.log(`Listening on ${PORT}`); |
| 99 | + console.log(`Ready! Open ${hostUrl}`); |
| 100 | +} else { |
| 101 | + console.log(`PROBLEM: You need to set the clientId (Integrator Key), and perhaps other settings as well. |
| 102 | +You can set them in the configuration file config/appsettings.json or set environment variables.\n`); |
| 103 | + process.exit(); // We're not using exit code of 1 to avoid extraneous npm messages. |
| 104 | +} |
| 105 | + |
| 106 | +// Passport session setup. |
| 107 | +// To support persistent login sessions, Passport needs to be able to |
| 108 | +// serialize users into and deserialize users out of the session. Typically, |
| 109 | +// this will be as simple as storing the user ID when serializing, and finding |
| 110 | +// the user by ID when deserializing. However, since this example does not |
| 111 | +// have a database of user records, the complete DocuSign profile is serialized |
| 112 | +// and deserialized. |
| 113 | +passport.serializeUser(function(user, done) { done(null, user); }); |
| 114 | +passport.deserializeUser(function(obj, done) { done(null, obj); }); |
| 115 | + |
| 116 | +let scope = ['signature']; |
| 117 | +// Configure passport for DocusignStrategy |
| 118 | +const docusignStrategyOptions = { |
| 119 | + production: dsConfig.production, |
| 120 | + clientID: dsConfig.dsClientId, |
| 121 | + scope: scope.join(' '), |
| 122 | + clientSecret: dsConfig.dsClientSecret, |
| 123 | + callbackURL: hostUrl + '/ds/callback', |
| 124 | + state: true // automatic CSRF protection. |
| 125 | + // See https://github.com/jaredhanson/passport-oauth2/blob/master/lib/state/session.js |
| 126 | +}; |
| 127 | +function processDsResult(accessToken, refreshToken, params, profile, done) { |
| 128 | + // The params arg will be passed additional parameters of the grant. |
| 129 | + // See https://github.com/jaredhanson/passport-oauth2/pull/84 |
| 130 | + // |
| 131 | + // Here we're just assigning the tokens to the account object |
| 132 | + // We store the data in DSAuthCodeGrant.getDefaultAccountInfo |
| 133 | + let user = profile; |
| 134 | + user.accessToken = accessToken; |
| 135 | + user.refreshToken = refreshToken; |
| 136 | + user.expiresIn = params.expires_in; |
| 137 | + user.tokenExpirationTimestamp = moment().add(user.expiresIn, 's'); // The dateTime when the access token will expire |
| 138 | + return done(null, user); |
| 139 | +} |
| 140 | +const docusignStrategy = new DocusignStrategy(docusignStrategyOptions, processDsResult); |
| 141 | +const docusignStrategyPKCE = new DocusignStrategy({ |
| 142 | + ...docusignStrategyOptions, |
| 143 | + pkce: true |
| 144 | + }, |
| 145 | + processDsResult |
| 146 | +); |
| 147 | + |
| 148 | +/** |
| 149 | + * The DocuSign OAuth default is to allow silent authentication. |
| 150 | + * An additional OAuth query parameter is used to not allow silent authentication |
| 151 | + */ |
| 152 | +if (!dsConfig.allowSilentAuthentication) { |
| 153 | + // See https://stackoverflow.com/a/32877712/64904 |
| 154 | + docusignStrategy.authorizationParams = function(options) { |
| 155 | + return { prompt: 'login' }; |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +passport.use('docusign', docusignStrategy); |
| 160 | +passport.use('docusign_pkce', docusignStrategyPKCE); |
0 commit comments