@@ -3,36 +3,48 @@ const fs = require('fs');
33const path = require ( 'path' ) ;
44
55const settings = require ( '../config/appsettings.json' ) ;
6- const { REDIRECT_URI , BASE_PATH , OAUTH_BASE_PATH , PRIVATE_KEY_FILENAME , EXPIRES_IN , signerClientId, pingUrl, returnUrl, TEST_PDF_FILE , SCOPES } = require ( './constants' ) ;
6+ const { REDIRECT_URI , BASE_PATH , OAUTH_BASE_PATH , PRIVATE_KEY_FILENAME , EXPIRES_IN , SCOPES } = require ( './constants' ) ;
7+
8+ const TEST_TIMEOUT_MS = 10000 ;
79
810const apiClient = new docusign . ApiClient ( {
911 basePath : BASE_PATH ,
1012 oAuthBasePath : OAUTH_BASE_PATH
1113} ) ;
1214
1315const authenticate = async ( ) => {
14- // IMPORTANT NOTE:
15- // the first time you ask for a JWT access token, you should grant access by making the following call
16- // get DocuSign OAuth authorization url:
16+ try {
17+ const privateKeyFile = fs . readFileSync ( path . resolve ( __dirname , PRIVATE_KEY_FILENAME ) ) ;
18+ const res = await apiClient . requestJWTUserToken ( settings . dsJWTClientId , settings . impersonatedUserGuid , SCOPES , privateKeyFile , EXPIRES_IN ) ;
1719
18- const authorizationUrl = apiClient . getJWTUri ( settings . dsJWTClientId , REDIRECT_URI , OAUTH_BASE_PATH ) ;
19- // open DocuSign OAuth authorization url in the browser, login and grant access
20- console . log ( 'OAuth authorization url:' , authorizationUrl ) ;
21- // END OF NOTE
22-
23- const privateKeyFile = fs . readFileSync ( path . resolve ( __dirname , PRIVATE_KEY_FILENAME ) ) ;
24- const res = await apiClient . requestJWTUserToken ( settings . dsJWTClientId , settings . impersonatedUserGuid , SCOPES , privateKeyFile , EXPIRES_IN ) ;
25-
26- const accessToken = res . body . access_token ;
27- apiClient . addDefaultHeader ( 'Authorization' , `Bearer ${ accessToken } ` ) ;
28- const userInfo = await apiClient . getUserInfo ( accessToken ) ;
29-
30- const accountId = userInfo . accounts [ 0 ] . accountId ;
31- const baseUri = userInfo . accounts [ 0 ] . baseUri ;
32- const accountDomain = baseUri . split ( '/v2' ) ;
33- apiClient . setBasePath ( `${ accountDomain [ 0 ] } /restapi` ) ;
20+ const accessToken = res . body . access_token ;
21+ apiClient . addDefaultHeader ( 'Authorization' , `Bearer ${ accessToken } ` ) ;
22+ const userInfo = await apiClient . getUserInfo ( accessToken ) ;
3423
35- return { accessToken, accountId, baseUri } ;
24+ const accountId = userInfo . accounts [ 0 ] . accountId ;
25+
26+ return { accessToken, accountId } ;
27+ } catch ( error ) {
28+ const body = error . response && error . response . body ;
29+
30+ // Determine the source of the error.
31+ if ( body ) {
32+ if ( body . error && body . error === 'consent_required' ) {
33+ // The first time you ask for a JWT access token.
34+ // get DocuSign OAuth authorization url:
35+ const authorizationUrl = apiClient . getJWTUri ( settings . dsJWTClientId , REDIRECT_URI , OAUTH_BASE_PATH ) ;
36+
37+ // open DocuSign OAuth authorization url in the browser, login and grant access
38+ const consentMessage = `You should grant access by making the following call: ${ authorizationUrl } ` ;
39+ console . log ( consentMessage ) ;
40+ throw new Error ( consentMessage ) ;
41+ } else {
42+ // Consent has been granted. Show status code for DocuSign API error.
43+ throw new Error ( `\nAPI problem: Status code ${ error . response . status } , message body:
44+ ${ JSON . stringify ( body , null , 4 ) } \n\n` ) ;
45+ }
46+ }
47+ }
3648} ;
3749
3850const areEqual = ( obj1 , obj2 ) => {
@@ -41,58 +53,4 @@ const areEqual = (obj1, obj2) => {
4153 || JSON . parse ( JSON . stringify ( obj1 ) ) == JSON . parse ( JSON . stringify ( obj2 ) ) ;
4254}
4355
44- const getToken = async function _getToken ( ) {
45- // Data used
46- // dsConfig.dsClientId
47- // dsConfig.impersonatedUserGuid
48- // dsConfig.privateKey
49- // dsConfig.dsOauthServer
50- const privateKeyFile = fs . readFileSync ( path . resolve ( __dirname , PRIVATE_KEY_FILENAME ) ) ;
51-
52- const jwtLifeSec = 10 * 60 , // requested lifetime for the JWT is 10 min
53- dsApi = new docusign . ApiClient ( ) ;
54- dsApi . setOAuthBasePath ( OAUTH_BASE_PATH ) ; // it should be domain only.
55- const results = await dsApi . requestJWTUserToken ( settings . dsClientId ,
56- settings . impersonatedUserGuid , SCOPES , privateKeyFile ,
57- jwtLifeSec ) ;
58-
59- return {
60- accessToken : results . body . access_token ,
61- } ;
62- }
63-
64- const getUserInfo = async function _getUserInfo ( accessToken ) {
65- // Data used:
66- // dsConfig.targetAccountId
67- // dsConfig.dsOauthServer
68- // DsJwtAuth.accessToken
69-
70- const dsApi = new docusign . ApiClient ( )
71- , targetAccountId = settings . targetAccountId
72- , baseUriSuffix = '/restapi' ;
73-
74- dsApi . setOAuthBasePath ( OAUTH_BASE_PATH ) ; // it have to be domain name
75- const results = await dsApi . getUserInfo ( accessToken ) ;
76-
77- let accountInfo ;
78- if ( ! Boolean ( targetAccountId ) ) {
79- // find the default account
80- accountInfo = results . accounts . find ( account =>
81- account . isDefault === "true" ) ;
82- } else {
83- // find the matching account
84- accountInfo = results . accounts . find ( account => account . accountId == targetAccountId ) ;
85- }
86- if ( typeof accountInfo === 'undefined' ) {
87- throw new Error ( `Target account ${ targetAccountId } not found!` ) ;
88- }
89-
90- const accountId = accountInfo . accountId ;
91- const basePath = accountInfo . baseUri + baseUriSuffix ;
92- return {
93- accountId,
94- basePath
95- }
96- }
97-
98- module . exports = { authenticate, areEqual, getToken, getUserInfo }
56+ module . exports = { TEST_TIMEOUT_MS , authenticate, areEqual }
0 commit comments