-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathverification_request.rb
172 lines (146 loc) · 5.25 KB
/
verification_request.rb
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
# frozen_string_literal: true
require 'erb'
require 'faraday'
require 'rexml/document'
require 'rexml/xpath'
require 'securerandom'
require 'retries'
module Proofing
module Aamva
module Request
class VerificationRequest
CONTENT_TYPE = 'application/soap+xml;charset=UTF-8'
DEFAULT_VERIFICATION_URL =
'https://verificationservices-cert.aamva.org:18449/dldv/2.1/online'
SOAP_ACTION =
'"http://aamva.org/dldv/wsdl/2.1/IDLDVService21/VerifyDriverLicenseData"'
extend Forwardable
attr_reader :config, :body, :headers, :url
def initialize(config:, applicant:, session_id:, auth_token:)
@config = config
@applicant = applicant
@transaction_id = session_id
@auth_token = auth_token
@url = verification_url
@body = build_request_body
@headers = build_request_headers
end
def send
Response::VerificationResponse.new(
http_client.post(url, body, headers) do |req|
req.options.context = { service_name: 'aamva_verification' }
end,
)
rescue Faraday::TimeoutError, Faraday::ConnectionFailed => err
message = "AAMVA raised #{err.class} waiting for verification response: #{err.message}"
raise ::Proofing::TimeoutError, message
end
def verification_url
config.verification_url || DEFAULT_VERIFICATION_URL
end
private
attr_reader :applicant, :transaction_id, :auth_token
def http_client
Faraday.new(request: { open_timeout: timeout, timeout: timeout }) do |faraday|
faraday.request :instrumentation, name: 'request_metric.faraday'
faraday.adapter :net_http
end
end
def add_user_provided_data_to_body
document = REXML::Document.new(body)
user_provided_data_map.each do |xpath, data|
REXML::XPath.first(document, xpath).add_text(data)
end
add_optional_element(
'nc:AddressDeliveryPointText',
value: applicant.address2,
document:,
after: '//aa:Address/nc:AddressDeliveryPointText',
)
add_optional_element(
'aa:DriverLicenseIssueDate',
value: applicant.state_id_data.state_id_issued,
document:,
inside: '//dldv:verifyDriverLicenseDataRequest',
)
add_optional_element(
'aa:DriverLicenseExpirationDate',
value: applicant.state_id_data.state_id_expiration,
document:,
inside: '//dldv:verifyDriverLicenseDataRequest',
)
@body = document.to_s
end
def add_optional_element(name, value:, document:, inside: nil, after: nil)
return if value.blank?
el = REXML::Element.new(name)
el.text = value
if inside
REXML::XPath.first(document, inside).add_element(el)
elsif after
sibling = REXML::XPath.first(document, after)
sibling.parent.insert_after(sibling, el)
end
end
def build_request_body
renderer = ERB.new(request_body_template)
@body = renderer.result(binding)
add_user_provided_data_to_body
end
def build_request_headers
{
'SOAPAction' => SOAP_ACTION,
'Content-Type' => CONTENT_TYPE,
'Content-Length' => body.length.to_s,
}
end
def message_destination_id
# Note: AAMVA uses this field to route the request to the appropriate state DMV.
# We are required to use 'P6' as the jurisdiction when we make requests
# in the AAMVA CERT/Test environment.
return 'P6' if config.cert_enabled.to_s == 'true'
applicant.state_id_data.state_id_jurisdiction
end
def request_body_template
template_file_path = Rails.root.join(
'app',
'services',
'proofing',
'aamva',
'request',
'templates',
'verify.xml.erb',
)
File.read(template_file_path)
end
def transaction_locator_id
applicant.uuid
end
def user_provided_data_map
{
'//nc:IdentificationID' => state_id_number,
'//aa:MessageDestinationId' => message_destination_id,
'//nc:PersonGivenName' => applicant.first_name,
'//nc:PersonSurName' => applicant.last_name,
'//aa:PersonBirthDate' => applicant.dob,
'//nc:AddressDeliveryPointText' => applicant.address1,
'//nc:LocationCityName' => applicant.city,
'//nc:LocationStateUsPostalServiceCode' => applicant.state,
'//nc:LocationPostalCode' => applicant.zipcode,
}
end
def state_id_number
case applicant.state_id_data.state_id_jurisdiction
when 'SC'
applicant.state_id_data.state_id_number.rjust(8, '0')
else
applicant.state_id_data.state_id_number
end
end
def timeout
(config.verification_request_timeout || 5).to_f
end
end
end
end
end