|
1 |
| -require 'yaml' |
2 |
| - |
3 |
| -module JwtAuth |
4 |
| - class JwtCreator |
5 |
| - include ApiCreator |
6 |
| - |
7 |
| - attr_reader :session, :api_client, :state |
8 |
| - |
9 |
| - # Docusign authorization URI to obtain individual consent |
10 |
| - # https://developers.docusign.com/platform/auth/jwt/jwt-get-token |
11 |
| - # https://developers.docusign.com/platform/auth/consent/obtaining-individual-consent/ |
12 |
| - def self.consent_url(state, api) |
13 |
| - # GET /oauth/auth |
14 |
| - # This endpoint is used to obtain consent and is the first step in several authentication flows. |
15 |
| - # https://developers.docusign.com/platform/auth/reference/obtain-consent |
16 |
| - scope = 'signature impersonation' if %w[eSignature Monitor].include?(api) |
17 |
| - scope = 'signature impersonation dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms' if api == 'Rooms' |
18 |
| - scope = 'signature impersonation click.manage click.send' if api == 'Click' |
19 |
| - scope = 'signature impersonation organization_read group_read permission_read user_read user_write account_read domain_read identity_provider_read user_data_redact asset_group_account_read asset_group_account_clone_write asset_group_account_clone_read' if api == 'Admin' |
20 |
| - scope = 'signature webforms_read webforms_instance_read webforms_instance_write' if api == 'WebForms' |
21 |
| - scope = 'signature aow_manage' if api == 'Maestro' |
22 |
| - |
23 |
| - base_uri = "#{Rails.configuration.authorization_server}/oauth/auth" |
24 |
| - response_type = 'code' |
25 |
| - scopes = ERB::Util.url_encode(scope) # https://developers.docusign.com/platform/auth/reference/scopes/ |
26 |
| - client_id = Rails.configuration.jwt_integration_key |
27 |
| - redirect_uri = "#{Rails.configuration.app_url}/auth/docusign/callback" |
28 |
| - consent_url = "#{base_uri}?response_type=#{response_type}&scope=#{scopes}&client_id=#{client_id}&state=#{state}&redirect_uri=#{redirect_uri}" |
29 |
| - Rails.logger.info "==> Obtain Consent Grant required: #{consent_url}" |
30 |
| - consent_url |
31 |
| - end |
32 |
| - |
33 |
| - def initialize(session) |
34 |
| - @session = session |
35 |
| - scope = 'signature impersonation' |
36 |
| - @client_module = DocuSign_eSign |
37 |
| - if session[:api] == 'Rooms' |
38 |
| - scope = "#{scope} signature dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms" |
39 |
| - @client_module = DocuSign_Rooms |
40 |
| - end |
41 |
| - if session[:api] == 'Click' |
42 |
| - scope = 'signature click.manage click.send' |
43 |
| - @client_module = DocuSign_Click |
44 |
| - end |
45 |
| - @client_module = DocuSign_Monitor if session[:api] == 'Monitor' |
46 |
| - if session[:api] == 'Admin' |
47 |
| - scope = 'signature organization_read group_read permission_read user_read user_write account_read domain_read identity_provider_read user_data_redact asset_group_account_read asset_group_account_clone_write asset_group_account_clone_read' |
48 |
| - @client_module = DocuSign_Admin |
49 |
| - end |
50 |
| - if session[:api] == 'WebForms' |
51 |
| - scope = 'signature webforms_read webforms_instance_read webforms_instance_write' |
52 |
| - @client_module = DocuSign_WebForms |
53 |
| - end |
54 |
| - |
55 |
| - @scope = scope |
56 |
| - @api_client = create_initial_api_client(host: Rails.configuration.aud, client_module: @client_module, debugging: false) |
57 |
| - end |
58 |
| - |
59 |
| - # @return [Boolean] `true` if the token was successfully updated, `false` if consent still needs to be grant'ed |
60 |
| - def check_jwt_token |
61 |
| - rsa_pk = docusign_rsa_private_key_file |
62 |
| - begin |
63 |
| - # docusign_esign: POST /oauth/token |
64 |
| - # This endpoint enables you to exchange an authorization code or JWT token for an access token. |
65 |
| - # https://developers.docusign.com/platform/auth/reference/obtain-access-token |
66 |
| - token = api_client.request_jwt_user_token(Rails.configuration.jwt_integration_key, Rails.configuration.impersonated_user_guid, rsa_pk, 3600, @scope) |
67 |
| - rescue OpenSSL::PKey::RSAError => e |
68 |
| - Rails.logger.error e.inspect |
69 |
| - raise "Please add your private RSA key to: #{rsa_pk}" if File.read(rsa_pk).starts_with? '{RSA_PRIVATE_KEY}' |
70 |
| - |
71 |
| - raise |
72 |
| - rescue @client_module::ApiError => e |
73 |
| - Rails.logger.warn e.inspect |
74 |
| - |
75 |
| - return false if e.response_body.nil? |
76 |
| - |
77 |
| - body = JSON.parse(e.response_body) |
78 |
| - |
79 |
| - if body['error'] == 'consent_required' |
80 |
| - false |
81 |
| - else |
82 |
| - details = <<~TXT |
83 |
| - See: https://support.docusign.com/articles/DocuSign-Developer-Support-FAQs#Troubleshoot-JWT-invalid_grant |
84 |
| - or https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-code-grant#troubleshooting-errors |
85 |
| - or try enabling `configuration.debugging = true` in the initialize method above for more logging output |
86 |
| - TXT |
87 |
| - raise "JWT response error: `#{body}`. #{details}" |
88 |
| - end |
89 |
| - else |
90 |
| - update_account_info(token) |
91 |
| - true |
92 |
| - end |
93 |
| - end |
94 |
| - |
95 |
| - private |
96 |
| - |
97 |
| - def update_account_info(token) |
98 |
| - # docusign_esign: GET /oauth/userinfo |
99 |
| - # This endpoint returns information on the caller, including their name, email, account, and organizational information. |
100 |
| - # The response includes the base_uri needed to interact with the Docusign APIs. |
101 |
| - # https://developers.docusign.com/platform/auth/reference/user-info |
102 |
| - user_info_response = api_client.get_user_info(token.access_token) |
103 |
| - accounts = user_info_response.accounts |
104 |
| - target_account_id = Rails.configuration.target_account_id |
105 |
| - account = get_account(accounts, target_account_id) |
106 |
| - store_data(token, user_info_response, account) |
107 |
| - |
108 |
| - api_client.config.host = account.base_uri |
109 |
| - Rails.logger.info "==> JWT: Received token for impersonated user which will expire in: #{token.expires_in.to_i.seconds / 1.hour} hour at: #{Time.at(token.expires_in.to_i.seconds.from_now)}" |
110 |
| - end |
111 |
| - |
112 |
| - def store_data(token, user_info, account) |
113 |
| - session[:ds_access_token] = token.access_token |
114 |
| - session[:ds_expires_at] = token.expires_in.to_i.seconds.from_now.to_i |
115 |
| - session[:ds_user_name] = user_info.name |
116 |
| - session[:ds_account_id] = account.account_id |
117 |
| - session[:ds_base_path] = account.base_uri |
118 |
| - session[:ds_account_name] = account.account_name |
119 |
| - end |
120 |
| - |
121 |
| - def get_account(accounts, target_account_id) |
122 |
| - if target_account_id.present? |
123 |
| - return accounts.find { |acct| acct.account_id == target_account_id } |
124 |
| - raise "The user does not have access to account #{target_account_id}" |
125 |
| - else |
126 |
| - accounts.find(&:is_default) |
127 |
| - end |
128 |
| - end |
129 |
| - |
130 |
| - def docusign_rsa_private_key_file |
131 |
| - File.join(Rails.root, 'config', 'docusign_private_key.txt') |
132 |
| - end |
133 |
| - end |
134 |
| -end |
| 1 | +require 'yaml' |
| 2 | + |
| 3 | +module JwtAuth |
| 4 | + class JwtCreator |
| 5 | + include ApiCreator |
| 6 | + |
| 7 | + attr_reader :session, :api_client, :state |
| 8 | + |
| 9 | + # Docusign authorization URI to obtain individual consent |
| 10 | + # https://developers.docusign.com/platform/auth/jwt/jwt-get-token |
| 11 | + # https://developers.docusign.com/platform/auth/consent/obtaining-individual-consent/ |
| 12 | + def self.consent_url(state, api) |
| 13 | + # GET /oauth/auth |
| 14 | + # This endpoint is used to obtain consent and is the first step in several authentication flows. |
| 15 | + # https://developers.docusign.com/platform/auth/reference/obtain-consent |
| 16 | + scope = 'signature impersonation' if %w[eSignature Monitor].include?(api) |
| 17 | + scope = 'signature impersonation dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms' if api == 'Rooms' |
| 18 | + scope = 'signature impersonation click.manage click.send' if api == 'Click' |
| 19 | + scope = 'signature impersonation organization_read group_read permission_read user_read user_write account_read domain_read identity_provider_read user_data_redact asset_group_account_read asset_group_account_clone_write asset_group_account_clone_read organization_sub_account_write organization_sub_account_read' if api == 'Admin' |
| 20 | + scope = 'signature webforms_read webforms_instance_read webforms_instance_write' if api == 'WebForms' |
| 21 | + scope = 'signature aow_manage' if api == 'Maestro' |
| 22 | + |
| 23 | + base_uri = "#{Rails.configuration.authorization_server}/oauth/auth" |
| 24 | + response_type = 'code' |
| 25 | + scopes = ERB::Util.url_encode(scope) # https://developers.docusign.com/platform/auth/reference/scopes/ |
| 26 | + client_id = Rails.configuration.jwt_integration_key |
| 27 | + redirect_uri = "#{Rails.configuration.app_url}/auth/docusign/callback" |
| 28 | + consent_url = "#{base_uri}?response_type=#{response_type}&scope=#{scopes}&client_id=#{client_id}&state=#{state}&redirect_uri=#{redirect_uri}" |
| 29 | + Rails.logger.info "==> Obtain Consent Grant required: #{consent_url}" |
| 30 | + consent_url |
| 31 | + end |
| 32 | + |
| 33 | + def initialize(session) |
| 34 | + @session = session |
| 35 | + scope = 'signature impersonation' |
| 36 | + @client_module = DocuSign_eSign |
| 37 | + if session[:api] == 'Rooms' |
| 38 | + scope = "#{scope} signature dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms" |
| 39 | + @client_module = DocuSign_Rooms |
| 40 | + end |
| 41 | + if session[:api] == 'Click' |
| 42 | + scope = 'signature click.manage click.send' |
| 43 | + @client_module = DocuSign_Click |
| 44 | + end |
| 45 | + @client_module = DocuSign_Monitor if session[:api] == 'Monitor' |
| 46 | + if session[:api] == 'Admin' |
| 47 | + scope = 'signature organization_read group_read permission_read user_read user_write account_read domain_read identity_provider_read user_data_redact asset_group_account_read asset_group_account_clone_write asset_group_account_clone_read organization_sub_account_write organization_sub_account_read' |
| 48 | + @client_module = DocuSign_Admin |
| 49 | + end |
| 50 | + if session[:api] == 'WebForms' |
| 51 | + scope = 'signature webforms_read webforms_instance_read webforms_instance_write' |
| 52 | + @client_module = DocuSign_WebForms |
| 53 | + end |
| 54 | + |
| 55 | + @scope = scope |
| 56 | + @api_client = create_initial_api_client(host: Rails.configuration.aud, client_module: @client_module, debugging: false) |
| 57 | + end |
| 58 | + |
| 59 | + # @return [Boolean] `true` if the token was successfully updated, `false` if consent still needs to be grant'ed |
| 60 | + def check_jwt_token |
| 61 | + rsa_pk = docusign_rsa_private_key_file |
| 62 | + begin |
| 63 | + # docusign_esign: POST /oauth/token |
| 64 | + # This endpoint enables you to exchange an authorization code or JWT token for an access token. |
| 65 | + # https://developers.docusign.com/platform/auth/reference/obtain-access-token |
| 66 | + token = api_client.request_jwt_user_token(Rails.configuration.jwt_integration_key, Rails.configuration.impersonated_user_guid, rsa_pk, 3600, @scope) |
| 67 | + rescue OpenSSL::PKey::RSAError => e |
| 68 | + Rails.logger.error e.inspect |
| 69 | + raise "Please add your private RSA key to: #{rsa_pk}" if File.read(rsa_pk).starts_with? '{RSA_PRIVATE_KEY}' |
| 70 | + |
| 71 | + raise |
| 72 | + rescue @client_module::ApiError => e |
| 73 | + Rails.logger.warn e.inspect |
| 74 | + |
| 75 | + return false if e.response_body.nil? |
| 76 | + |
| 77 | + body = JSON.parse(e.response_body) |
| 78 | + |
| 79 | + if body['error'] == 'consent_required' |
| 80 | + false |
| 81 | + else |
| 82 | + details = <<~TXT |
| 83 | + See: https://support.docusign.com/articles/DocuSign-Developer-Support-FAQs#Troubleshoot-JWT-invalid_grant |
| 84 | + or https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-code-grant#troubleshooting-errors |
| 85 | + or try enabling `configuration.debugging = true` in the initialize method above for more logging output |
| 86 | + TXT |
| 87 | + raise "JWT response error: `#{body}`. #{details}" |
| 88 | + end |
| 89 | + else |
| 90 | + update_account_info(token) |
| 91 | + true |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + private |
| 96 | + |
| 97 | + def update_account_info(token) |
| 98 | + # docusign_esign: GET /oauth/userinfo |
| 99 | + # This endpoint returns information on the caller, including their name, email, account, and organizational information. |
| 100 | + # The response includes the base_uri needed to interact with the Docusign APIs. |
| 101 | + # https://developers.docusign.com/platform/auth/reference/user-info |
| 102 | + user_info_response = api_client.get_user_info(token.access_token) |
| 103 | + accounts = user_info_response.accounts |
| 104 | + target_account_id = Rails.configuration.target_account_id |
| 105 | + account = get_account(accounts, target_account_id) |
| 106 | + store_data(token, user_info_response, account) |
| 107 | + |
| 108 | + api_client.config.host = account.base_uri |
| 109 | + Rails.logger.info "==> JWT: Received token for impersonated user which will expire in: #{token.expires_in.to_i.seconds / 1.hour} hour at: #{Time.at(token.expires_in.to_i.seconds.from_now)}" |
| 110 | + end |
| 111 | + |
| 112 | + def store_data(token, user_info, account) |
| 113 | + session[:ds_access_token] = token.access_token |
| 114 | + session[:ds_expires_at] = token.expires_in.to_i.seconds.from_now.to_i |
| 115 | + session[:ds_user_name] = user_info.name |
| 116 | + session[:ds_account_id] = account.account_id |
| 117 | + session[:ds_base_path] = account.base_uri |
| 118 | + session[:ds_account_name] = account.account_name |
| 119 | + end |
| 120 | + |
| 121 | + def get_account(accounts, target_account_id) |
| 122 | + if target_account_id.present? |
| 123 | + return accounts.find { |acct| acct.account_id == target_account_id } |
| 124 | + raise "The user does not have access to account #{target_account_id}" |
| 125 | + else |
| 126 | + accounts.find(&:is_default) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def docusign_rsa_private_key_file |
| 131 | + File.join(Rails.root, 'config', 'docusign_private_key.txt') |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments