-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
158 lines (137 loc) · 4.54 KB
/
app.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
require 'nokogiri'
require 'sinatra'
require 'json'
require 'securerandom'
require 'prometheus/client'
if ENV['NEW_RELIC_LICENSE_KEY'] && ENV['NEW_RELIC_APP_NAME']
require 'newrelic_rpm'
puts 'enabling newrelic'
end
module LoginGov
class FakeVendorServer < Sinatra::Base
# Prometheus init
prometheus = Prometheus::Client.registry
http_connections = Prometheus::Client::Gauge.new(:http_connections, docstring: 'current HTTP connections')
prometheus.register(http_connections)
http_connections.set(0)
def fixture(path)
File.read(File.join(__dir__, 'fixtures', path))
end
set :logging, true
set :dump_errors, true
set :raise_errors, true
set :show_exceptions, :after_handler
# AAMVA
post '/Authentication/Authenticate.svc' do
doc = Nokogiri::XML(request.body.read)
action = doc.at_xpath('//ns:Action', ns: 'http://www.w3.org/2005/08/addressing').text
case action
when 'http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT'
sleep ENV['AAMVA_SECURITY_TOKEN_DELAY'].to_f
fixture 'aamva/security_token_response.xml'
when 'http://aamva.org/authentication/3.1.0/IAuthenticationService/Authenticate'
sleep ENV['AAMVA_AUTHENTICATION_TOKEN_DELAY'].to_f
fixture 'aamva/authentication_token_response.xml'
end
end
# AAMVA
post '/dldv/2.1/online' do
sleep ENV['AAMVA_VERIFICATION_DELAY'].to_f
fixture 'aamva/verification_response.xml'
end
# Acuant
post '/AssureIDService/Document/Instance' do
sleep ENV['ACUANT_CREATE_DOCUMENT_DELAY'].to_f
# body is a JSON atom
SecureRandom.hex.to_json
end
# Acuant
post '/AssureIDService/Document/:instance_id/Image' do
''
end
# The way the Acuant Client encodes images does not play well with sinatra's parameter parsing
error Sinatra::BadRequest do
sleep ENV['ACUANT_UPLOAD_IMAGE_DELAY'].to_f
status 200
''
end
# Acuant
post '/api/v1/facematch' do
sleep ENV['ACUANT_FACEMATCH_DELAY'].to_f
fixture 'acuant/facial_match_response.json'
end
# Acuant
get '/AssureIDService/Document/:instance_id' do
sleep ENV['ACUANT_GET_RESULTS_DELAY'].to_f
fixture 'acuant/get_results_response.json'
end
# LexisNexis TrueID
post '/restws/identity/v3/accounts/:account_number/workflows/:workflow_name/conversations' do
case params[:workflow_name]
when /TrueID/
sleep ENV['LEXISNEXIS_TRUE_ID_DELAY'].to_f
fixture 'lexisnexis/true_id_response.json'
end
end
# LexisNexis
post "/restws/identity/v2/:account_number/:workflow_name/conversation" do
case params[:workflow_name]
when /instant.verify/
sleep ENV['LEXISNEXIS_INSTANT_VERIFY_DELAY'].to_f
fixture 'lexisnexis/instant_verify_response.json'
when /phonefinder/
sleep ENV['LEXISNEXIS_PHONE_FINDER_DELAY'].to_f
fixture 'lexisnexis/phone_finder_response.json'
end
end
# USPS IPPaaS auth
post "/oauth/authenticate" do
sleep ENV['USPS_IPPAAS_AUTH_DELAY'].to_f
content_type 'application/json'
fixture 'usps/ippaas_oauth_authenticate_response.json'
end
# USPS IPPaaS
post "/ivs-ippaas-api/IPPRest/resources/rest/getProofingResults" do
sleep ENV['USPS_IPPAAS_GETPROOFINGRESULTS_DELAY'].to_f
case ENV['USPS_IPPAAS_GETPROOFINGRESULTS_OUTCOME']
when "missing_enrollment_code"
status 400
content_type 'application/json'
fixture 'usps/ippaas_getproofingresults_missingenrollmentcode_response.json'
else
content_type 'application/json'
fixture 'usps/ippaas_getproofingresults_response.json'
end
end
# USPS IPPaaS
post "/ivs-ippaas-api/IPPRest/resources/rest/optInIPPApplicant" do
sleep ENV['USPS_IPPAAS_OPTINIPPAPPLICANT_DELAY'].to_f
content_type 'application/json'
fixture 'usps/ippaas_optinippapplicant_response.json'
end
# health
get '/health' do
begin
sockstatdata = File.read('/proc/net/sockstat')
connections = /TCP: inuse (?<inuse>\d+) /.match(sockstatdata)[:inuse].to_i
http_connections.set(connections)
rescue
http_connections.set(1)
end
status 200
end
end
class HandleBadEncodingMiddleware
def initialize(app)
@app = app
end
def call(env)
begin
Rack::Utils.parse_nested_query(env['QUERY_STRING'].to_s)
rescue Rack::Utils::InvalidParameterError
env['QUERY_STRING'] = ''
end
@app.call(env)
end
end
end