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