This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.rb
64 lines (52 loc) · 1.7 KB
/
client.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
require 'faraday'
require 'kiji/signer'
require 'kiji/api'
module Kiji
class Client
include Kiji::API
attr_accessor :cert, :private_key, :access_key,
:software_id, :api_end_point,
:basic_auth_id, :basic_auth_password
def initialize
@software_id = software_id
@api_end_point = api_end_point
@basic_auth_id = basic_auth_id
@basic_auth_password = basic_auth_password
yield(self) if block_given?
end
private
def connection
Faraday.new(url: @api_end_point) do |c|
# c.response :logger
c.adapter Faraday.default_adapter
c.request(*authentication_middleware_keys, @basic_auth_id, @basic_auth_password) unless @basic_auth_id.nil?
c.headers['User-Agent'] = 'SmartHR v0.0.1'
c.headers['x-eGovAPI-SoftwareID'] = software_id
c.headers['x-eGovAPI-AccessKey'] = access_key unless access_key.nil?
end
end
def authentication_middleware_keys
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0.0')
%i[authorization basic]
else
[:basic_auth]
end
end
def sign(appl_data)
raise 'Please specify cert & private_key' if cert.nil? || private_key.nil?
doc = appl_data.to_xml(save_with: 0)
signer = Signer.new(doc) do |s|
s.cert = cert
s.private_key = private_key
s.digest_algorithm = :sha256
s.signature_digest_algorithm = :sha256
end
signer.security_node = signer.document.root
signer.document.xpath('/DataRoot/ApplData').each do |node|
signer.digest!(node, id: '#ApplData')
end
signer.sign!(issuer_serial: true)
signer
end
end
end