Skip to content

Commit 297e440

Browse files
Jwt project (#56)
* jwt console project
1 parent cba20ce commit 297e440

File tree

4 files changed

+799
-0
lines changed

4 files changed

+799
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dsJWTClientId": "{INTEGRATION_KEY_JWT}",
3+
"impersonatedUserGuid": "{IMPERSONATED_USER_ID}",
4+
"privateKeyLocation": "./private.key",
5+
"dsOauthServer": "https://account-d.docusign.com"
6+
}

jwt_console_project/jwtConsole.js

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

0 commit comments

Comments
 (0)