-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
459 lines (396 loc) · 14.3 KB
/
app.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"strings"
"time"
"examples"
"github.com/golang-jwt/jwt"
"github.com/pkg/browser"
)
// Random String generator made from: https://github.com/Onelinerhub/onelinerhub/blob/main/golang/how-to-generate-random-string.md
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func rand_str(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
var DSAccessToken string
var DSAccountId string
var EnvelopeId string
var EnvelopeDefinition string
var config Config
// For RSA signing method, the key can be any []byte. It is recommended to generate
// a key using crypto/rand or something equivalent. You need the same key for signing
// and validating.
var RSAPrivateKey []byte
func makeDSToken(config Config) (string, error) {
// Create a new JWT claim. Set your integration key, impersonated user GUID, time of issue, expiry time, account server, and required scopes
rawJWT := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
"iss": config.IntegrationKey,
"sub": config.UserImpersonationGUIDJwt,
"iat": time.Now().Unix(),
"exp": time.Now().Unix() + 3600,
"aud": "account-d.docusign.com",
"scope": "signature impersonation",
})
RSAPrivateKey, err := os.ReadFile(config.RSAPrivateKeyJwtLocation)
if err != nil {
log.Fatalf("Error opening file: %s", err)
return "", err
}
// Load the private.key file into JWT library
rsaPrivate, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(RSAPrivateKey))
if err != nil {
log.Fatalf("key update error for: %s", err)
return "", err
}
// Generate the signed JSON Web Token assertion with an RSA private key
tokenString, err := rawJWT.SignedString(rsaPrivate)
//fmt.Println(tokenString, err)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Submit the JWT to the account server and request and access token
resp, err := http.PostForm("https://account-d.docusign.com/oauth/token",
url.Values{
"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"},
"assertion": {tokenString},
})
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
fmt.Printf("Body: %s\n", body)
if strings.Contains(string(body), "consent_required") {
fmt.Println("consent has not been granted to use this account")
var loginUrl = fmt.Sprintf("https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature+impersonation&client_id=%s&redirect_uri=https://developers.docusign.com/platform/auth/consent", config.IntegrationKey)
browser.OpenURL(loginUrl)
fmt.Println("A new browser window has been opened to: %s", loginUrl)
fmt.Println("Waiting for 90 seconds then I'll try to login using JWT again")
time.Sleep(90 * time.Second)
makeDSToken(config)
}
// Done with the request, close it: https://stackoverflow.com/q/18598780/2226328
resp.Body.Close()
// Decode the response to JSON
var token AccessToken
jsonErr := json.Unmarshal(body, &token)
if jsonErr != nil {
log.Fatalf("There was an error decoding the json. err = %s", jsonErr)
return "", jsonErr
}
//fmt.Println(token.Token)
return token.Token, nil
}
// Internal API call to pull in the API account ID GUID used to make all subsequent API calls
func getAPIAccId(DSAccessToken string) (string, error) {
client := &http.Client{}
// Use http.NewRequest in order to set custom headers
req, err := http.NewRequest("GET", "https://account-d.docusign.com/oauth/userinfo", nil)
req.Header.Set("Authorization", "Bearer "+DSAccessToken)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Since http.NewRequest is being used, client.Do is needed to execute the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// fmt.Printf("Body: %s\n", body)
res.Body.Close()
// Decode the response to JSON
var accountId AccountId
jsonErr := json.Unmarshal(body, &accountId)
if jsonErr != nil {
log.Fatalf("There was an error decoding the json. err = %s", jsonErr)
return "", jsonErr
}
//fmt.Println(accountId.Accounts[0].AccountID)
return accountId.Accounts[0].AccountID, nil
}
// Make the envelope definition
func makeEnvelope(signerName string, signerEmail string, ccName string, ccEmail string) string {
envelope := fmt.Sprintf(`{
"emailSubject": "Please sign this document set",
"documents": [{
"documentBase64": "DQoNCg0KCQkJCXRleHQgZG9jDQoNCg0KDQoNCg0KUk0gIwlSTSAjCVJNICMNCg0KDQoNClxzMVwNCg0KLy9hbmNoMSANCgkvL2FuY2gyDQoJCS8vYW5jaDM=",
"documentId": "1",
"fileExtension": "txt",
"name": "NDA"
}],
"recipients": {
"carbonCopies": [
{
"email": "%s",
"name": "%s",
"recipientId": "2",
"routingOrder": "2"
}
],
"signers": [
{
"email": "%s",
"name": "%s",
"recipientId": "1",
"routingOrder": "1",
"tabs": {
"signHereTabs": [{
"documentId": "1",
"name": "SignHereTab",
"pageNumber": "1",
"recipientId": "1",
"tabLabel": "SignHereTab",
"xPosition": "75",
"yPosition": "572"
}]
},
}
]
},
"status": "sent"
}`, ccEmail, ccName, signerEmail, signerName)
return envelope
}
func makeCustomFieldsEnvelope(signerEmail string, signerName string, customerId string) string {
dateTimeString := time.Now().Format("01-02-2006")
envelope := fmt.Sprintf(`{
"emailSubject": "Please review this recent order:",
"documents": [{
"documentBase64": "DQoNCg0KCQkJCXRleHQgZG9jDQoNCg0KDQoNCg0KUk0gIwlSTSAjCVJNICMNCg0KDQoNClxzMVwNCg0KLy9hbmNoMSANCgkvL2FuY2gyDQoJCS8vYW5jaDM=",
"documentId": "1",
"fileExtension": "txt",
"name": "Order Summary %s"
}],
"customFields": {
"textCustomFields": [
{
"name": "trackingNumber",
"required": "false",
"show": "true",
},
{
"name": "shippingDate",
"required": "false",
"show": "true",
},
{
"name": "customerID",
"required": "true",
"show": "true",
"value": "%s"
}
]
},
"recipients": {
"signers": [
{
"email": "%s",
"name": "%s",
"recipientId": "1",
"routingOrder": "1",
"tabs": {
"signHereTabs": [{
"documentId": "1",
"name": "SignHereTab",
"pageNumber": "1",
"recipientId": "1",
"tabLabel": "SignHereTab",
"xPosition": "75",
"yPosition": "572"
}]
},
}
]
},
"status": "sent"
}`, dateTimeString, customerId, signerEmail, signerName)
return envelope
}
// Send an envelope
func sendEnvelope(DSAccessToken string, DSAccountId string, envelopeDefinition string) (string, error) {
client := &http.Client{}
// Use http.NewRequest in order to set custom headers
req, err := http.NewRequest("POST", "https://demo.docusign.net/restapi/v2.1/accounts/"+DSAccountId+"/envelopes", strings.NewReader(envelopeDefinition))
req.Header.Set("Authorization", "Bearer "+DSAccessToken)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Since http.NewRequest is being used, client.Do is needed to execute the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Decode the response to JSON
var envelope EnvelopeID
jsonErr := json.Unmarshal(body, &envelope)
if jsonErr != nil {
log.Fatalf("Request Failed: %s", jsonErr)
return "", jsonErr
}
return envelope.EnvelopeID, nil
}
// Gets envelope custom fields
func getCustomFields(DSAccessToken string, DSAccountId string, envelopeId string) ([]string, error) {
client := &http.Client{}
// Use http.NewRequest in order to set custom headers
req, err := http.NewRequest("GET", "https://demo.docusign.net/restapi/v2.1/accounts/"+DSAccountId+"/envelopes/"+envelopeId+"/custom_fields", nil)
req.Header.Set("Authorization", "Bearer "+DSAccessToken)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return []string{}, err
}
// Since http.NewRequest is being used, client.Do is needed to execute the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return []string{}, err
}
// fmt.Printf("response: %s\n", res)
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return []string{}, err
}
// Decode the response to JSON
var customFields CustomFields
jsonErr := json.Unmarshal(body, &customFields)
if jsonErr != nil {
log.Fatalf("Request Failed: %s", jsonErr)
return []string{}, jsonErr
}
fmt.Print(customFields.TextCustomFields[0].FieldID + " -> tracking id\n")
fmt.Print(customFields.TextCustomFields[1].FieldID + " -> ship date\n")
responseArray := []string{customFields.TextCustomFields[0].FieldID, customFields.TextCustomFields[1].FieldID}
return responseArray, nil
}
// Sets custom field values for an envelope
func setCustomfieldValues(DSAccessToken string, DSAccountId string, envelopeId string, trackingNumberFieldId string, shippingDateFieldId string) (string, error) {
client := &http.Client{}
// For the sake of the example these are arbitrary values,
// on a real application these values should be retreived
// from an orders table on a database
shippingDate := time.Now().Add(time.Hour * 24 * 3).Format("01-02-2006")
trackingNumber := "1Z" + rand_str(16)
requestBody := fmt.Sprintf(`{ textCustomFields: [
{ "fieldId" : "%s",
"value" : "%s" },
{ "fieldId" : "%s",
"value" : "%s" }
]}`, trackingNumberFieldId, trackingNumber, shippingDateFieldId, shippingDate)
// Use http.NewRequest in order to set custom headers
req, err := http.NewRequest("PUT", "https://demo.docusign.net/restapi/v2.1/accounts/"+DSAccountId+"/envelopes/"+envelopeId+"/custom_fields", strings.NewReader(requestBody))
req.Header.Set("Authorization", "Bearer "+DSAccessToken)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Since http.NewRequest is being used, client.Do is needed to execute the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
fmt.Printf("Body: %s\n", body)
return string(body), nil
}
// Voids an envelope
func voidEnvelope(DSAccessToken string, DSAccountId string, EnvelopeID string) (string, error) {
client := &http.Client{}
voidBody := `{
"status": "voided",
"voidedReason": "The reason for voiding the envelope"
}`
// Use http.NewRequest in order to set custom headers
req, err := http.NewRequest("PUT", "https://demo.docusign.net/restapi/v2.1/accounts/"+DSAccountId+"/envelopes/"+EnvelopeID, strings.NewReader(voidBody))
req.Header.Set("Authorization", "Bearer "+DSAccessToken)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
// Since http.NewRequest is being used, client.Do is needed to execute the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("Request Failed: %s", err)
return "", err
}
fmt.Print("Void Envelope Response: " + res.Status + "\n")
return res.Status, err
}
func main() {
fmt.Println("\n\nWelcome to the DocuSign Go Launcher using Authorization Code grant or JWT grant authentication.")
data, err := os.ReadFile("config.json")
if err != nil {
log.Fatalf("Error when opening file: %v", err)
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Error when unmarshaling JSON: %v", err)
}
//fmt.Printf("Config: %+v\n", config)
// we delcared the config variable around line 92
fmt.Println("We're making an Access token using JWT grant authentication.\n")
dSAccessToken, err := makeDSToken(config)
if err != nil {
log.Fatalf("Failed to retrieve token: %s", err)
}
fmt.Printf("access token: %s\n", dSAccessToken)
fmt.Println("\n\nNow we're retrieving your API account ID to make further API calls")
dSAccountId, err := getAPIAccId(dSAccessToken)
if err != nil {
log.Fatalf("Failed to API Account ID token: %s", err)
}
// section 1: send an envelope (and VOID IT)
envelopeDefinition := makeEnvelope(config.SignerName, config.SignerEmail, config.CcName, config.CcEmail)
envelopeId, err := sendEnvelope(dSAccessToken, dSAccountId, envelopeDefinition)
if err != nil {
log.Fatalf("Failed to retrieve token: %s", err)
}
voidEnvelope(dSAccessToken, dSAccountId, envelopeId)
// section 2: BONUS: Let's create an envelope with Custom Fields then update them programmatically
envelopeDefinitionCustomTabs := makeCustomFieldsEnvelope(config.SignerEmail, config.SignerName, "CUSTOMER-"+rand_str(14))
// fmt.Printf(envelopeDefinitionCustomTabs)
envelopeId2, err := sendEnvelope(dSAccessToken, dSAccountId, envelopeDefinitionCustomTabs)
if err != nil {
log.Fatalf("update custom fields request failed: %s", err)
}
// fmt.Print("envelope ID is: " + envelopeId2 + "\n")
// Lets retrieve the Envelope custom Fields ids
customFieldIds, err := getCustomFields(dSAccessToken, dSAccountId, envelopeId2)
// Set the shipping date and tracking number on the envelope
finalRes, err := setCustomfieldValues(dSAccessToken, dSAccountId, envelopeId2, customFieldIds[0], customFieldIds[1])
fmt.Printf("Envelope Custom fields updated successfully: \n%s", finalRes)
}