Class: BlockChyp::BlockChypClient
- Inherits:
-
Object
- Object
- BlockChyp::BlockChypClient
- Defined in:
- lib/blockchyp_client.rb
Overview
base class for the blockchyp generated blockchyp client
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#bearer_token ⇒ Object
readonly
Returns the value of attribute bearer_token.
-
#gateway_host ⇒ Object
Returns the value of attribute gateway_host.
-
#gateway_timeout ⇒ Object
Returns the value of attribute gateway_timeout.
-
#https ⇒ Object
Returns the value of attribute https.
-
#offline_cache_enabled ⇒ Object
Returns the value of attribute offline_cache_enabled.
-
#offline_fixed_key ⇒ Object
readonly
Returns the value of attribute offline_fixed_key.
-
#route_cache ⇒ Object
Returns the value of attribute route_cache.
-
#route_cache_location ⇒ Object
Returns the value of attribute route_cache_location.
-
#route_cache_ttl ⇒ Object
Returns the value of attribute route_cache_ttl.
-
#signing_key ⇒ Object
readonly
Returns the value of attribute signing_key.
-
#terminal_connect_timeout ⇒ Object
Returns the value of attribute terminal_connect_timeout.
-
#terminal_timeout ⇒ Object
Returns the value of attribute terminal_timeout.
-
#test_gateway_host ⇒ Object
Returns the value of attribute test_gateway_host.
Instance Method Summary collapse
- #compute_hmac(tsp, nonce) ⇒ Object
- #decrypt(cipher_text) ⇒ Object
- #derive_offline_key ⇒ Object
- #encrypt(plain_text) ⇒ Object
- #evict(terminal_name) ⇒ Object
- #gateway_request(method, path, request = nil, relay = false) ⇒ Object
- #generate_error_response(msg) ⇒ Object
- #generate_gateway_headers ⇒ Object
- #get_http_request(method, uri) ⇒ Object
- #get_timeout(request, default) ⇒ Object
-
#initialize(api_key, bearer_token, signing_key) ⇒ BlockChypClient
constructor
A new instance of BlockChypClient.
- #read_offline_cache ⇒ Object
- #request_route_from_gateway(terminal_name) ⇒ Object
- #resolve_gateway_uri(path, request) ⇒ Object
- #resolve_terminal_route(terminal_name) ⇒ Object
- #resolve_terminal_uri(route, path) ⇒ Object
- #route_cache_get(terminal_name, stale) ⇒ Object
- #route_terminal_request(method, terminal_path, gateway_path, request) ⇒ Object
- #terminal_request(method, route, path, request, open_retry) ⇒ Object
- #update_offline_cache(route_cache_entry) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(api_key, bearer_token, signing_key) ⇒ BlockChypClient
Returns a new instance of BlockChypClient.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/blockchyp_client.rb', line 18 def initialize(api_key, bearer_token, signing_key) @api_key = api_key @bearer_token = bearer_token @signing_key = signing_key @gateway_host = 'https://api.blockchyp.com' @test_gateway_host = 'https://test.blockchyp.com' @https = false @route_cache_location = File.join(Dir.tmpdir, '.blockchyp_route') @route_cache_ttl = 60 @gateway_timeout = 20 @terminal_timeout = 120 @terminal_connect_timeout = 5 @offline_cache_enabled = true @route_cache = {} @offline_fixed_key = 'cb22789c9d5c344a10e0474f134db39e25eb3bbf5a1b1a5e89b507f15ea9519c' end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
35 36 37 |
# File 'lib/blockchyp_client.rb', line 35 def api_key @api_key end |
#bearer_token ⇒ Object (readonly)
Returns the value of attribute bearer_token.
36 37 38 |
# File 'lib/blockchyp_client.rb', line 36 def bearer_token @bearer_token end |
#gateway_host ⇒ Object
Returns the value of attribute gateway_host.
39 40 41 |
# File 'lib/blockchyp_client.rb', line 39 def gateway_host @gateway_host end |
#gateway_timeout ⇒ Object
Returns the value of attribute gateway_timeout.
43 44 45 |
# File 'lib/blockchyp_client.rb', line 43 def gateway_timeout @gateway_timeout end |
#https ⇒ Object
Returns the value of attribute https.
41 42 43 |
# File 'lib/blockchyp_client.rb', line 41 def https @https end |
#offline_cache_enabled ⇒ Object
Returns the value of attribute offline_cache_enabled.
45 46 47 |
# File 'lib/blockchyp_client.rb', line 45 def offline_cache_enabled @offline_cache_enabled end |
#offline_fixed_key ⇒ Object (readonly)
Returns the value of attribute offline_fixed_key.
38 39 40 |
# File 'lib/blockchyp_client.rb', line 38 def offline_fixed_key @offline_fixed_key end |
#route_cache ⇒ Object
Returns the value of attribute route_cache.
48 49 50 |
# File 'lib/blockchyp_client.rb', line 48 def route_cache @route_cache end |
#route_cache_location ⇒ Object
Returns the value of attribute route_cache_location.
47 48 49 |
# File 'lib/blockchyp_client.rb', line 47 def route_cache_location @route_cache_location end |
#route_cache_ttl ⇒ Object
Returns the value of attribute route_cache_ttl.
42 43 44 |
# File 'lib/blockchyp_client.rb', line 42 def route_cache_ttl @route_cache_ttl end |
#signing_key ⇒ Object (readonly)
Returns the value of attribute signing_key.
37 38 39 |
# File 'lib/blockchyp_client.rb', line 37 def signing_key @signing_key end |
#terminal_connect_timeout ⇒ Object
Returns the value of attribute terminal_connect_timeout.
46 47 48 |
# File 'lib/blockchyp_client.rb', line 46 def terminal_connect_timeout @terminal_connect_timeout end |
#terminal_timeout ⇒ Object
Returns the value of attribute terminal_timeout.
44 45 46 |
# File 'lib/blockchyp_client.rb', line 44 def terminal_timeout @terminal_timeout end |
#test_gateway_host ⇒ Object
Returns the value of attribute test_gateway_host.
40 41 42 |
# File 'lib/blockchyp_client.rb', line 40 def test_gateway_host @test_gateway_host end |
Instance Method Details
#compute_hmac(tsp, nonce) ⇒ Object
63 64 65 66 67 |
# File 'lib/blockchyp_client.rb', line 63 def compute_hmac(tsp, nonce) canonical_string = api_key + bearer_token + tsp + nonce OpenSSL::HMAC.hexdigest('SHA256', CryptoUtils.hex2bin(signing_key), canonical_string) end |
#decrypt(cipher_text) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/blockchyp_client.rb', line 246 def decrypt(cipher_text) return cipher_text if cipher_text.nil? || cipher_text.empty? tokens = cipher_text.split(':') return cipher_text if tokens[0].nil? || tokens[1].nil? iv = Base64.decode64(tokens[0]) cp = Base64.decode64(tokens[1]) decipher = OpenSSL::Cipher::AES256.new(:CBC) decipher.decrypt decipher.key = derive_offline_key decipher.iv = iv decipher.update(cp) + decipher.final end |
#derive_offline_key ⇒ Object
264 265 266 |
# File 'lib/blockchyp_client.rb', line 264 def derive_offline_key Digest::SHA256.digest offline_fixed_key + signing_key end |
#encrypt(plain_text) ⇒ Object
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/blockchyp_client.rb', line 235 def encrypt(plain_text) return plain_text if plain_text.nil? || plain_text.empty? cipher = OpenSSL::Cipher::AES256.new(:CBC) cipher.encrypt cipher.key = derive_offline_key iv = cipher.random_iv Base64.encode64(iv) + ':' + Base64.encode64(cipher.update(plain_text) + cipher.final) end |
#evict(terminal_name) ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/blockchyp_client.rb', line 276 def evict(terminal_name) route_cache.delete(api_key + terminal_name) offline_cache = read_offline_cache offline_cache.delete(api_key + terminal_name) File.write(route_cache_location, route_cache.to_json) end |
#gateway_request(method, path, request = nil, relay = false) ⇒ Object
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 |
# File 'lib/blockchyp_client.rb', line 168 def gateway_request(method, path, request = nil, relay = false) uri = resolve_gateway_uri(path, request) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.instance_of?(URI::HTTPS) timeout = get_timeout(request, relay ? terminal_timeout : gateway_timeout) http.open_timeout = timeout http.read_timeout = timeout req = get_http_request(method, uri) req['User-Agent'] = user_agent unless request.nil? json = request.to_json req['Content-Type'] = 'application/json' req['Content-Length'] = json.length req.body = json end headers = generate_gateway_headers headers.each do |key, value| req[key] = value end response = http.request(req) if response.is_a?(Net::HTTPSuccess) JSON.parse(response.body, symbolize_names: true) else raise response. end end |
#generate_error_response(msg) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/blockchyp_client.rb', line 75 def generate_error_response(msg) { success: false, error: msg, responseDescription: msg } end |
#generate_gateway_headers ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/blockchyp_client.rb', line 50 def generate_gateway_headers nonce = CryptoUtils.generate_nonce tsp = CryptoUtils. sig = compute_hmac(tsp, nonce) { 'Nonce' => nonce, 'Timestamp' => tsp, 'Authorization' => 'Dual ' + bearer_token + ':' + api_key + ':' + sig } end |
#get_http_request(method, uri) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/blockchyp_client.rb', line 140 def get_http_request(method, uri) case method when 'GET' Net::HTTP::Get.new(uri.request_uri) when 'PUT' Net::HTTP::Put.new(uri.request_uri) when 'POST' Net::HTTP::Post.new(uri.request_uri) when 'DELETE' Net::HTTP::Delete.new(uri.request_uri) end end |
#get_timeout(request, default) ⇒ Object
330 331 332 333 334 335 336 |
# File 'lib/blockchyp_client.rb', line 330 def get_timeout(request, default) if request.nil? || !request.key?(:timeout) || request[:timeout].zero? return default end request[:timeout] end |
#read_offline_cache ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/blockchyp_client.rb', line 311 def read_offline_cache if File.file?(route_cache_location) config_file = File.open(route_cache_location) content = config_file.read return JSON.parse(content, symbolize_names: true) end {} end |
#request_route_from_gateway(terminal_name) ⇒ Object
268 269 270 271 272 273 274 |
# File 'lib/blockchyp_client.rb', line 268 def request_route_from_gateway(terminal_name) route = gateway_request('GET', '/api/terminal-route?terminal=' + CGI.escape(terminal_name)) if !route.nil? && !route[:ipAddress].empty? route[:exists] = true end route end |
#resolve_gateway_uri(path, request) ⇒ Object
69 70 71 72 73 |
# File 'lib/blockchyp_client.rb', line 69 def resolve_gateway_uri(path, request) url = request.nil? || !request[:test] ? gateway_host : test_gateway_host URI.parse(path.nil? ? url : url + path) end |
#resolve_terminal_route(terminal_name) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/blockchyp_client.rb', line 200 def resolve_terminal_route(terminal_name) route = route_cache_get(terminal_name, false) if route.nil? route = request_route_from_gateway(terminal_name) if route.nil? return route end ttl = Time.now.utc + (route_cache_ttl * 60) route_cache_entry = {} route_cache_entry[:route] = route route_cache_entry[:ttl] = ttl @route_cache[api_key + terminal_name] = route_cache_entry update_offline_cache(route_cache_entry) end route end |
#resolve_terminal_uri(route, path) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/blockchyp_client.rb', line 153 def resolve_terminal_uri(route, path) url = if https 'https://' else 'http://' end url += route[:ipAddress] port = if https ':8443' else ':8080' end URI.parse(url + port + path) end |
#route_cache_get(terminal_name, stale) ⇒ Object
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 |
# File 'lib/blockchyp_client.rb', line 284 def route_cache_get(terminal_name, stale) route_cache_entry = @route_cache[api_key + terminal_name] if route_cache_entry.nil? && offline_cache_enabled offline_cache = read_offline_cache route_cache_entry = offline_cache[@api_key + terminal_name] end if route_cache_entry route = route_cache_entry[:route] tx_creds = route[:transientCredentials] tx_creds[:apiKey] = decrypt(tx_creds[:apiKey]) tx_creds[:bearerToken] = decrypt(tx_creds[:bearerToken]) tx_creds[:signingKey] = decrypt(tx_creds[:signingKey]) route[:transientCredentials] = tx_creds route_cache_entry[:route] = route raw_ttl = route_cache_entry[:ttl] ttl = raw_ttl.instance_of?(Time) ? raw_ttl : Time.parse(route_cache_entry[:ttl]) if stale || Time.new < ttl route_cache_entry[:route] end end end |
#route_terminal_request(method, terminal_path, gateway_path, request) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/blockchyp_client.rb', line 83 def route_terminal_request(method, terminal_path, gateway_path, request) unless request.key?(:terminalName) return gateway_request(method, gateway_path, request) end route = resolve_terminal_route(request[:terminalName]) if !route return generate_error_response('Unkown Terminal') elsif route[:cloudRelayEnabled] return gateway_request(method, gateway_path, request, relay: true) end terminal_request(method, route, terminal_path, request, true) end |
#terminal_request(method, route, path, request, open_retry) ⇒ Object
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 |
# File 'lib/blockchyp_client.rb', line 98 def terminal_request(method, route, path, request, open_retry) uri = resolve_terminal_uri(route, path) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.instance_of?(URI::HTTPS) timeout = get_timeout(request, terminal_timeout) http.open_timeout = timeout http.read_timeout = timeout tx_creds = route[:transientCredentials] wrapped_request = { apiKey: tx_creds[:apiKey], bearerToken: tx_creds[:bearerToken], signingKey: tx_creds[:signingKey], request: request } req = get_http_request(method, uri) req['User-Agent'] = user_agent json = wrapped_request.to_json req['Content-Type'] = 'application/json' req['Content-Length'] = json.length req.body = json begin response = http.request(req) rescue Net::OpenTimeout, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ENETUNREACH if open_retry evict(route[:terminalName]) route = resolve_terminal_route(route[:terminalName]) return terminal_request(method, route, path, request, false) end raise end if response.is_a?(Net::HTTPSuccess) JSON.parse(response.body, symbolize_names: true) else raise response. end end |
#update_offline_cache(route_cache_entry) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/blockchyp_client.rb', line 219 def update_offline_cache(route_cache_entry) if offline_cache_enabled offline_cache = read_offline_cache offline_entry = route_cache_entry.clone route = route_cache_entry[:route].clone tx_creds = route[:transientCredentials].clone tx_creds[:apiKey] = encrypt(tx_creds[:apiKey]) tx_creds[:bearerToken] = encrypt(tx_creds[:bearerToken]) tx_creds[:signingKey] = encrypt(tx_creds[:signingKey]) route[:transientCredentials] = tx_creds offline_entry[:route] = route offline_cache[api_key + route[:terminalName]] = offline_entry File.write(route_cache_location, offline_cache.to_json) end end |