Skip to content

Commit 8872d17

Browse files
Jwt project (#64)
jwt_console_project
1 parent 40ca338 commit 8872d17

File tree

4 files changed

+136
-5
lines changed

4 files changed

+136
-5
lines changed

app/controllers/e_sign/eg002_signing_via_email_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ def create
1010
signer_name: param_gsub(params['signerName']),
1111
cc_email: param_gsub(params['ccEmail']),
1212
cc_name: param_gsub(params['ccName']),
13-
status: 'sent'
13+
status: 'sent',
14+
doc_docx: File.join('data', Rails.application.config.doc_docx),
15+
doc_pdf: File.join('data', Rails.application.config.doc_pdf)
1416
}
1517
args = {
1618
account_id: session['ds_account_id'],

app/services/e_sign/eg002_signing_via_email_service.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ def make_envelope(envelope_args)
4242
doc1_b64 = Base64.encode64(create_document1(envelope_args))
4343
# Read files 2 and 3 from a local directory
4444
# The reads could raise an exception if the file is not available!
45-
doc_docx = Rails.application.config.doc_docx
46-
doc2_b64 = Base64.encode64(File.binread(File.join('data', doc_docx)))
47-
doc_pdf = Rails.application.config.doc_pdf
48-
doc3_b64 = Base64.encode64(File.binread(File.join('data', doc_pdf)))
45+
doc2_b64 = Base64.encode64(File.binread(envelope_args[:doc_docx]))
46+
doc3_b64 = Base64.encode64(File.binread(envelope_args[:doc_pdf]))
4947

5048
# Create the document models
5149
document1 = DocuSign_eSign::Document.new(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jwt_integration_key: {INTEGRATION_KEY_JWT}
2+
impersonated_user_guid: {IMPERSONATED_USER_ID}
3+
authorization_server: account-d.docusign.com
4+
pdf_filename: '../data/World_Wide_Corp_lorem.pdf'

jwt_console_project/jwt_console.rb

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
require 'bundler/inline'
2+
3+
gemfile do
4+
source 'https://rubygems.org'
5+
gem 'docusign_esign',' ~> 3.15.0'
6+
end
7+
8+
class ESign
9+
end
10+
11+
require 'docusign_esign'
12+
require_relative '../app/services/api_creator'
13+
require_relative '../app/services/e_sign/eg002_signing_via_email_service.rb'
14+
require 'yaml'
15+
16+
$SCOPES = [
17+
"signature", "impersonation"
18+
]
19+
20+
def load_config_data
21+
config_file_path = 'jwt_config.yml'
22+
begin
23+
config_file_contents = File.read(config_file_path)
24+
rescue Errno::ENOENT
25+
$stderr.puts "missing config file"
26+
raise
27+
end
28+
YAML.load(config_file_contents)
29+
end
30+
31+
def get_consent
32+
url_scopes = $SCOPES.join('+');
33+
# Construct consent URL
34+
redirect_uri = "https://developers.docusign.com/platform/auth/consent";
35+
consent_url = "https://#{CONFIG["authorization_server"]}/oauth/auth?response_type=code&" +
36+
"scope=#{url_scopes}&client_id=#{CONFIG["jwt_integration_key"]}&" +
37+
"redirect_uri=#{redirect_uri}"
38+
39+
puts "Open the following URL in your browser to grant consent to the application:"
40+
puts consent_url
41+
puts "Consent granted? \n 1)Yes \n 2)No"
42+
continue = gets;
43+
if continue.chomp == "1"
44+
return true;
45+
else
46+
puts "Please grant consent"
47+
exit
48+
end
49+
end
50+
51+
def authenticate
52+
configuration = DocuSign_eSign::Configuration.new
53+
configuration.debugging = true
54+
api_client = DocuSign_eSign::ApiClient.new(configuration)
55+
api_client.set_oauth_base_path(CONFIG["authorization_server"])
56+
57+
rsa_pk = 'docusign_private_key.txt'
58+
begin
59+
token = api_client.request_jwt_user_token(CONFIG["jwt_integration_key"], CONFIG["impersonated_user_guid"], rsa_pk, expires_in=3600, $SCOPES)
60+
user_info_response = api_client.get_user_info(token.access_token)
61+
account = user_info_response.accounts.find(&:is_default)
62+
63+
account_info = {
64+
access_token: token.access_token,
65+
account_id: account.account_id,
66+
base_path: account.base_uri
67+
}
68+
account_info
69+
rescue OpenSSL::PKey::RSAError => exception
70+
Rails.logger.error exception.inspect
71+
if File.read(rsa_pk).starts_with? '{RSA_PRIVATE_KEY}'
72+
fail "Please add your private RSA key to: #{rsa_pk}"
73+
else
74+
raise
75+
end
76+
rescue DocuSign_eSign::ApiError => exception
77+
body = JSON.parse(exception.response_body)
78+
if body['error'] == "consent_required"
79+
authenticate if get_consent
80+
else
81+
puts "API Error"
82+
puts body['error']
83+
puts body['message']
84+
exit
85+
end
86+
end
87+
end
88+
89+
def get_args(apiAccountId, accessToken, basePath)
90+
puts "Enter the signer's email address: "
91+
signerEmail = gets.chomp
92+
puts "Enter the signer's name: "
93+
signerName = gets.chomp
94+
puts "Enter the carbon copy's email address: "
95+
ccSignerEmail = gets.chomp
96+
puts "Enter the carbon copy's name: "
97+
ccSignerName = gets.chomp
98+
99+
envelope_args = {
100+
signer_email: signerEmail,
101+
signer_name: signerName,
102+
cc_email: ccSignerEmail,
103+
cc_name: ccSignerName,
104+
status: 'sent',
105+
doc_docx: '../data/World_Wide_Corp_Battle_Plan_Trafalgar.docx',
106+
doc_pdf: '../data/World_Wide_Corp_lorem.pdf'
107+
}
108+
args = {
109+
account_id: apiAccountId,
110+
base_path: basePath,
111+
access_token: accessToken,
112+
envelope_args: envelope_args
113+
}
114+
115+
return args
116+
end
117+
118+
def main
119+
load_config_data
120+
account_info = authenticate
121+
args = get_args(account_info[:account_id], account_info[:access_token], account_info[:base_path])
122+
results = ESign::Eg002SigningViaEmailService.new(args).worker
123+
puts "Successfully sent envelope with envelope ID: #{results['envelope_id']}"
124+
end
125+
126+
CONFIG = load_config_data
127+
main

0 commit comments

Comments
 (0)