-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathjwtConsole.js
113 lines (96 loc) · 3.59 KB
/
jwtConsole.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const docusign = require('docusign-esign');
const signingViaEmail = require('../lib/eSignature/examples/signingViaEmail');
const fs = require('fs');
const path = require('path');
const prompt = require('prompt-sync')();
const jwtConfig = require('./jwtConfig.json');
const { ProvisioningInformation } = require('docusign-esign');
const demoDocsPath = path.resolve(__dirname, '../demo_documents');
const doc2File = 'World_Wide_Corp_Battle_Plan_Trafalgar.docx';
const doc3File = 'World_Wide_Corp_lorem.pdf';
const SCOPES = [
'signature', 'impersonation'
];
function getConsent() {
var urlScopes = SCOPES.join('+');
// Construct consent URL
var redirectUri = 'https://developers.docusign.com/platform/auth/consent';
var consentUrl = `${jwtConfig.dsOauthServer}/oauth/auth?response_type=code&` +
`scope=${urlScopes}&client_id=${jwtConfig.dsJWTClientId}&` +
`redirect_uri=${redirectUri}`;
console.log('Open the following URL in your browser to grant consent to the application:');
console.log(consentUrl);
console.log('Consent granted? \n 1)Yes \n 2)No');
let consentGranted = prompt('');
if (consentGranted === '1'){
return true;
} else {
console.error('Please grant consent!');
process.exit();
}
}
async function authenticate(){
const jwtLifeSec = 10 * 60; // requested lifetime for the JWT is 10 min
const dsApi = new docusign.ApiClient();
dsApi.setOAuthBasePath(jwtConfig.dsOauthServer.replace('https://', '')); // it should be domain only.
let rsaKey = fs.readFileSync(jwtConfig.privateKeyLocation);
try {
const results = await dsApi.requestJWTUserToken(jwtConfig.dsJWTClientId,
jwtConfig.impersonatedUserGuid, SCOPES, rsaKey,
jwtLifeSec);
const accessToken = results.body.access_token;
// get user info
const userInfoResults = await dsApi.getUserInfo(accessToken);
// use the default account
let userInfo = userInfoResults.accounts.find(account =>
account.isDefault === 'true');
return {
accessToken: results.body.access_token,
apiAccountId: userInfo.accountId,
basePath: `${userInfo.baseUri}/restapi`
};
} catch (e) {
console.log(e);
let body = e.response && e.response.data;
// Determine the source of the error
if (body) {
// The user needs to grant consent
if (body.error && body.error === 'consent_required') {
if (getConsent()){ return authenticate(); };
} else {
// Consent has been granted. Show status code for DocuSign API error
this._debug_log(`\nAPI problem: Status code ${e.response.status}, message body:
${JSON.stringify(body, null, 4)}\n\n`);
}
}
}
}
function getArgs(apiAccountId, accessToken, basePath){
signerEmail = prompt("Enter the signer's email address: ");
signerName = prompt("Enter the signer's name: ");
ccEmail = prompt("Enter the carbon copy's email address: ");
ccName = prompt("Enter the carbon copy's name: ");
const envelopeArgs = {
signerEmail: signerEmail,
signerName: signerName,
ccEmail: ccEmail,
ccName: ccName,
status: 'sent',
doc2File: path.resolve(demoDocsPath, doc2File),
doc3File: path.resolve(demoDocsPath, doc3File)
};
const args = {
accessToken: accessToken,
basePath: basePath,
accountId: apiAccountId,
envelopeArgs: envelopeArgs
};
return args;
}
async function main(){
let accountInfo = await authenticate();
let args = getArgs(accountInfo.apiAccountId, accountInfo.accessToken, accountInfo.basePath);
let envelopeId = await signingViaEmail.sendEnvelope(args);
console.log(envelopeId);
}
main();