Class: Fintecture::Faraday::Authentication::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/fintecture/faraday/authentication/connection.rb

Class Method Summary collapse

Class Method Details

.connection(url) ⇒ Object



14
15
16
17
18
19
# File 'lib/fintecture/faraday/authentication/connection.rb', line 14

def connection(url)
  ::Faraday.new(url: url) do |faraday|
    faraday.request :url_encoded
    faraday.adapter ::Faraday.default_adapter
  end
end

.delete(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fintecture/faraday/authentication/connection.rb', line 48

def delete(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil)
  @client = client
  conn = connection(url)

  res = conn.delete do |req|
    req.options.params_encoder = Faraday::DisabledEncoder
    req.headers = req_headers(custom_content_type, bearer, secure_headers, additional_headers, disableAuthorization,
                              method: 'delete', body: req_body, url: url)
    req.body = req_body
  end

  !res.success? ? Fintecture::ApiException.error(res) : res
end

.get(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fintecture/faraday/authentication/connection.rb', line 34

def get(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil)
  @client = client
  conn = connection(url)

  res = conn.get do |req|
    req.options.params_encoder = Faraday::DisabledEncoder
    req.headers = req_headers(custom_content_type, bearer, secure_headers, additional_headers, disableAuthorization,
                              method: 'get', url: url)
    req.body = req_body
  end

  !res.success? ? Fintecture::ApiException.error(res) : res
end

.load_digest(payload) ⇒ Object



101
102
103
# File 'lib/fintecture/faraday/authentication/connection.rb', line 101

def load_digest(payload)
  { 'Digest' => "SHA-256=#{Fintecture::Utils::Crypto.hash_base64(payload)}" }
end

.post(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fintecture/faraday/authentication/connection.rb', line 21

def post(url:, req_body: nil, client: nil, custom_content_type: nil, bearer: nil, secure_headers: false, additional_headers: nil, disableAuthorization: nil)
  @client = client
  conn = connection(url)
  res = conn.post do |req|
    req.options.params_encoder = Faraday::DisabledEncoder
    req.headers = req_headers(custom_content_type, bearer, secure_headers, additional_headers, disableAuthorization,
                              method: 'post', body: req_body, url: url)
    req.body = req_body
  end

  !res.success? ? Fintecture::ApiException.error(res) : res
end

.req_headers(custom_content_type, bearer, secure_headers, additional_headers, disableAuthorization, url:, method: '', body: {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fintecture/faraday/authentication/connection.rb', line 62

def req_headers(custom_content_type, bearer, secure_headers, additional_headers, disableAuthorization, url:, method: '', body: {})
  if !additional_headers.nil? && !additional_headers.is_a?(Hash)
    raise Fintecture::ValidationException, 'additional_headers must be an object'
  end

  client_token = Base64.strict_encode64("#{@client.app_id}:#{@client.app_secret}")

  headers = {
    'Accept' => 'application/json',
    'User-Agent' => "Fintecture Ruby SDK v #{Fintecture::VERSION}",
    'Content-Type' => custom_content_type || 'application/x-www-form-urlencoded'
  }
  headers['Authorization'] = bearer || "Basic #{client_token}" unless disableAuthorization
  headers = headers.merge(additional_headers) unless additional_headers.nil?
  headers.merge(secure_headers ? req_secure_headers(body: body, url: url, method: method) : {})
end

.req_secure_headers(body: {}, url: '', method: '') ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fintecture/faraday/authentication/connection.rb', line 79

def req_secure_headers(body: {}, url: '', method: '')
  payload = (body.instance_of?(String) ? body : body.to_s)
  path_name = URI(url).path
  search_params = URI(url).query

  request_target = search_params ? "#{method.downcase} #{path_name}?#{search_params}" : "#{method.downcase} #{path_name}"
  date = Fintecture::Utils::Date.header_time.to_s
  digest = load_digest(payload)
  x_request_id = Fintecture::Utils::Crypto.generate_uuid

  headers = {
    'Date' => date,
    'X-Request-ID' => x_request_id
  }.merge(payload ? digest : {})

  headers['Signature'] =
    Fintecture::Utils::Crypto.create_signature_header(
      { '(request-target)' => request_target }.merge(headers), @client
    )
  headers
end