-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSignedDocuments.js
103 lines (95 loc) · 3.7 KB
/
getSignedDocuments.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
#!/usr/bin/env node
var config = require('./config/configuration');
var Promise = require('promise');
var rp = require('request-promise'),
_ = require('lodash'),
jwt = require('jsonwebtoken'),
fs = require('fs'),
moment = require('moment');
module.exports.get_signed_document = function(accountID, envelopeID, Docusign_credentials){
return new Promise(function(resolve, reject) {
var docusign_credentials, envelopeDefnition, accountId, link;
requestAuthenticationToken()
.then((tokenData) => {
docusign_credentials = setDocumentCredentials(tokenData);
})
.then(() =>
getAccounts(docusign_credentials)
)
.then((accounts) => {
let account_info = _.find(accounts.accounts, (account) => account.is_default);
accountId = account_info.account_id;
})
.then(() =>
getDocumentsList(accountId, docusign_credentials, envelopeID )
)
.then((envelopeData) => {
var documentId = envelopeData.envelopeDocuments[0].documentId;
return getDocument(accountId, envelopeID, documentId, docusign_credentials);
})
.then((file) => {
resolve(file);
})
.catch((err) =>
console.log(err)
)
});
}
getDocumentsList = function(accountID, docusign_credentials, envelopeID ) {
return rp.get(`https://demo.docusign.net/restapi/v2/accounts/${accountID}/envelopes/${envelopeID}/documents`,{
json: true,
headers: { Authorization: 'Bearer '+docusign_credentials.token },
followAllRedirects: true
})
.catch(function(err){
console.log(err);
});
};
getDocument = function(accountID,envelopeID,documentID, docusign_credentials) {
return rp.get(`https://demo.docusign.net/restapi/v2/accounts/${accountID}/envelopes/${envelopeID}/documents/${documentID}`,{
headers: { Authorization: 'Bearer '+docusign_credentials.token },
encoding: null
});
};
requestAuthenticationToken = function(){
// create the JWT payload to get the user's token
let payload = {
"iss": config.client_id,
"sub": config.impersonated_user_guid,
"iat": new Date().getTime() / 1000,
"exp": new Date().getTime() / 1000 + 3600,
"aud": config.aud,
"scope": config.permission_scopes,
};
let private_key = fs.readFileSync(config.private_key_file);
let jwt_token = jwt.sign(payload, private_key, {algorithm: 'RS256'});
return rp.post(`${config.authentication_url}/oauth/token`, {
json: true,
form: {
'grant_type': config.jwt_grant,
'assertion': jwt_token
},
followAllRedirects: true
})
};
var setDocumentCredentials = function(tokenData){
var credentials = {
token: tokenData.access_token,
expires: new Date().getTime() + (tokenData.expires_in * 1000)
};
let m = moment(credentials.expires);
//console.log(`The token will expire ${m.fromNow()}. (${m.format()})`);
return credentials;
};
var getAccounts = function(docusign_credentials){
// Call userinfo to get the user's account_id and api_base_url
// See https://docs.docusign.com/esign/guide/authentication/userinfo.html
// If the user or account is fixed for this app then you can
// treat the api_base_url as a constant. It is set per account and changes
// extremely infrequently (less often than once a year)
return rp.get(`${config.authentication_url}/oauth/userinfo`, {
json: true,
headers: { Authorization: 'Bearer '+docusign_credentials.token },
followAllRedirects: true
});
};