Class: BlockChyp::BlockChypClient

Inherits:
Object
  • Object
show all
Defined in:
lib/blockchyp_client.rb

Overview

base class for the blockchyp generated blockchyp client

Direct Known Subclasses

BlockChyp

Instance Attribute Summary collapse

Instance Method Summary collapse

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
34
# 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'
  @dashboard_host = 'https://dashboard.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_keyObject (readonly)

Returns the value of attribute api_key.



36
37
38
# File 'lib/blockchyp_client.rb', line 36

def api_key
  @api_key
end

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



37
38
39
# File 'lib/blockchyp_client.rb', line 37

def bearer_token
  @bearer_token
end

#dashboard_hostObject

Returns the value of attribute dashboard_host.



42
43
44
# File 'lib/blockchyp_client.rb', line 42

def dashboard_host
  @dashboard_host
end

#gateway_hostObject

Returns the value of attribute gateway_host.



40
41
42
# File 'lib/blockchyp_client.rb', line 40

def gateway_host
  @gateway_host
end

#gateway_timeoutObject

Returns the value of attribute gateway_timeout.



45
46
47
# File 'lib/blockchyp_client.rb', line 45

def gateway_timeout
  @gateway_timeout
end

#httpsObject

Returns the value of attribute https.



43
44
45
# File 'lib/blockchyp_client.rb', line 43

def https
  @https
end

#offline_cache_enabledObject

Returns the value of attribute offline_cache_enabled.



47
48
49
# File 'lib/blockchyp_client.rb', line 47

def offline_cache_enabled
  @offline_cache_enabled
end

#offline_fixed_keyObject (readonly)

Returns the value of attribute offline_fixed_key.



39
40
41
# File 'lib/blockchyp_client.rb', line 39

def offline_fixed_key
  @offline_fixed_key
end

#route_cacheObject

Returns the value of attribute route_cache.



50
51
52
# File 'lib/blockchyp_client.rb', line 50

def route_cache
  @route_cache
end

#route_cache_locationObject

Returns the value of attribute route_cache_location.



49
50
51
# File 'lib/blockchyp_client.rb', line 49

def route_cache_location
  @route_cache_location
end

#route_cache_ttlObject

Returns the value of attribute route_cache_ttl.



44
45
46
# File 'lib/blockchyp_client.rb', line 44

def route_cache_ttl
  @route_cache_ttl
end

#signing_keyObject (readonly)

Returns the value of attribute signing_key.



38
39
40
# File 'lib/blockchyp_client.rb', line 38

def signing_key
  @signing_key
end

#terminal_connect_timeoutObject

Returns the value of attribute terminal_connect_timeout.



48
49
50
# File 'lib/blockchyp_client.rb', line 48

def terminal_connect_timeout
  @terminal_connect_timeout
end

#terminal_timeoutObject

Returns the value of attribute terminal_timeout.



46
47
48
# File 'lib/blockchyp_client.rb', line 46

def terminal_timeout
  @terminal_timeout
end

#test_gateway_hostObject

Returns the value of attribute test_gateway_host.



41
42
43
# File 'lib/blockchyp_client.rb', line 41

def test_gateway_host
  @test_gateway_host
end

Instance Method Details

#compute_hmac(tsp, nonce) ⇒ Object



65
66
67
68
69
# File 'lib/blockchyp_client.rb', line 65

def compute_hmac(tsp, nonce)
  canonical_string = api_key + bearer_token + tsp + nonce

  OpenSSL::HMAC.hexdigest('SHA256', CryptoUtils.hex2bin(signing_key), canonical_string)
end

#dashboard_request(method, path, request = nil) ⇒ Object



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
199
200
201
202
203
204
# File 'lib/blockchyp_client.rb', line 174

def dashboard_request(method, path, request = nil)
  uri = resolve_dashboard_uri(path, request)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.instance_of?(URI::HTTPS)
  timeout = get_timeout(request, 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.message
  end
end

#decrypt(cipher_text) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/blockchyp_client.rb', line 324

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_keyObject



342
343
344
# File 'lib/blockchyp_client.rb', line 342

def derive_offline_key
  Digest::SHA256.digest offline_fixed_key + signing_key
end

#encrypt(plain_text) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/blockchyp_client.rb', line 313

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



354
355
356
357
358
359
360
# File 'lib/blockchyp_client.rb', line 354

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/blockchyp_client.rb', line 246

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.message
  end
end

#generate_error_response(msg) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/blockchyp_client.rb', line 81

def generate_error_response(msg)
  {
    success: false,
    error: msg,
    responseDescription: msg
  }
end

#generate_gateway_headersObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/blockchyp_client.rb', line 52

def generate_gateway_headers
  nonce = CryptoUtils.generate_nonce
  tsp = CryptoUtils.timestamp

  sig = compute_hmac(tsp, nonce)

  {
    'Nonce' => nonce,
    'Timestamp' => tsp,
    'Authorization' => 'Dual ' + bearer_token + ':' + api_key + ':' + sig
  }
end

#get_http_request(method, uri) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/blockchyp_client.rb', line 146

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



408
409
410
411
412
413
414
# File 'lib/blockchyp_client.rb', line 408

def get_timeout(request, default)
  if request.nil? || !request.key?(:timeout) || request[:timeout].zero?
    return default
  end

  request[:timeout]
end

#read_offline_cacheObject



389
390
391
392
393
394
395
396
397
398
# File 'lib/blockchyp_client.rb', line 389

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



346
347
348
349
350
351
352
# File 'lib/blockchyp_client.rb', line 346

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_dashboard_uri(path, request) ⇒ Object



77
78
79
# File 'lib/blockchyp_client.rb', line 77

def resolve_dashboard_uri(path, request)
  URI.parse(dashboard_host + path)
end

#resolve_gateway_uri(path, request) ⇒ Object



71
72
73
74
75
# File 'lib/blockchyp_client.rb', line 71

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/blockchyp_client.rb', line 278

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/blockchyp_client.rb', line 159

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



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/blockchyp_client.rb', line 362

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/blockchyp_client.rb', line 89

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



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
# File 'lib/blockchyp_client.rb', line 104

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.message
  end
end

#update_offline_cache(route_cache_entry) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/blockchyp_client.rb', line 297

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

#upload_request(path, request, content) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/blockchyp_client.rb', line 206

def upload_request(path, request, content)
  uri = resolve_dashboard_uri(path, request)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.instance_of?(URI::HTTPS)
  timeout = get_timeout(request, gateway_timeout)
  http.open_timeout = timeout
  http.read_timeout = timeout

  req = get_http_request("POST", uri)
  req.body = content
  req['User-Agent'] = user_agent
  req['Content-Type'] = 'application/octet-stream'

  if request[:fileName]
    req['X-Upload-File-Name'] = request[:fileName]
  end

  if request[:uploadId]
    req['X-Upload-ID'] = request[:uploadId]
  end

  if request[:fileSize]
    req['X-File-Size'] = request[:fileSize]
    req['Content-Length'] = request[:fileSize]
  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.message
  end
end

#user_agentObject



400
401
402
403
404
405
406
# File 'lib/blockchyp_client.rb', line 400

def user_agent
  if defined? VERSION
    "BlockChyp-Ruby/#{VERSION}"
  else
    'BlockChyp-Ruby'
  end
end