Class: Digidoc::Client
- Inherits:
-
Object
- Object
- Digidoc::Client
- Defined in:
- lib/digidoc/client.rb
Instance Attribute Summary collapse
-
#embedded_datafiles ⇒ Object
Returns the value of attribute embedded_datafiles.
-
#endpoint_url ⇒ Object
Returns the value of attribute endpoint_url.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#respond_with_nested_struct ⇒ Object
Returns the value of attribute respond_with_nested_struct.
-
#session_code ⇒ Object
Returns the value of attribute session_code.
Instance Method Summary collapse
-
#add_datafile(file, *args) ⇒ Object
Add datafile to DigiDoc container.
-
#authenticate(*args) ⇒ Object
Authentication message.
-
#authentication_status(session_code = self.session_code) ⇒ Object
Authentication status.
-
#close_session(session_code = self.session_code) ⇒ Object
Closes current session.
-
#create_signed_doc(*args) ⇒ Object
Creates DigiDoc container.
- #finalize_signature(*args) ⇒ Object
-
#initialize(endpoint_url = TestEndpointUrl) ⇒ Client
constructor
A new instance of Client.
-
#mobile_sign(*args) ⇒ Object
Sign DigiDoc container.
- #notary(*args) ⇒ Object
- #prepare_signature(*args) ⇒ Object
-
#save_signed_doc(*args, &block) ⇒ Object
Get DigiDoc container.
-
#sign_status(*args) ⇒ Object
Get session status info.
-
#signed_doc_info(*args) ⇒ Object
Get DigiDoc container status.
-
#start_session(*args) ⇒ Object
Starts and holds session.
Constructor Details
#initialize(endpoint_url = TestEndpointUrl) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 |
# File 'lib/digidoc/client.rb', line 30 def initialize(endpoint_url = TestEndpointUrl) self.endpoint_url = endpoint_url || TestEndpointUrl self.respond_with_nested_struct = true self. = [] #self.logger = Logger.new(STDOUT) end |
Instance Attribute Details
#embedded_datafiles ⇒ Object
Returns the value of attribute embedded_datafiles.
28 29 30 |
# File 'lib/digidoc/client.rb', line 28 def end |
#endpoint_url ⇒ Object
Returns the value of attribute endpoint_url.
28 29 30 |
# File 'lib/digidoc/client.rb', line 28 def endpoint_url @endpoint_url end |
#logger ⇒ Object
Returns the value of attribute logger.
28 29 30 |
# File 'lib/digidoc/client.rb', line 28 def logger @logger end |
#respond_with_nested_struct ⇒ Object
Returns the value of attribute respond_with_nested_struct.
28 29 30 |
# File 'lib/digidoc/client.rb', line 28 def respond_with_nested_struct @respond_with_nested_struct end |
#session_code ⇒ Object
Returns the value of attribute session_code.
28 29 30 |
# File 'lib/digidoc/client.rb', line 28 def session_code @session_code end |
Instance Method Details
#add_datafile(file, *args) ⇒ Object
Add datafile to DigiDoc container
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/digidoc/client.rb', line 289 def add_datafile(file, *args) = args.last || {} session_code = .delete(:session_code) || self.session_code filename = .delete(:filename) || File.basename(file.path) mime_type = [:mime_type] || calc_mime_type(file) use_hashcode = false #options.key?(:use_hashcode) || true filename = filename.gsub('/', '-') response = savon_client.call('AddDataFile') do |locals| file_content = Base64.encode64(file.read) # Upload file to webservice if use_hashcode # Calculate sha1 from file datafile = datafile(filename, mime_type, file.size, file_content, .size) self. << datafile hex_sha1 = Digest::SHA1.hexdigest(datafile) digest_value = Base64.encode64(hex_sha1.lines.to_a.pack('H*')) locals. 'Sesscode' => session_code, 'FileName' => filename, 'MimeType' => mime_type, 'ContentType' => 'HASHCODE', 'Size' => file.size, 'DigestType' => 'sha1', 'DigestValue' => digest_value else locals.'Sesscode' => session_code, 'FileName' => filename, 'MimeType' => mime_type, 'ContentType' => 'EMBEDDED_BASE64', 'Size' => file.size, 'Content' => file_content end end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:add_data_file_response] respond_with_hash_or_nested(result) end |
#authenticate(*args) ⇒ Object
Authentication message
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 |
# File 'lib/digidoc/client.rb', line 38 def authenticate(*args) = args.last || {} phone = .delete(:phone) personal_code = .delete(:personal_code) country_code = .delete(:country_code) || 'EE' language = .delete(:language) || 'EST' service_name = .delete(:service_name) || 'Testimine' = .delete(:message_to_display) || 'Tuvastamine' messaging_mode = .delete(:messaging_mode) || 'asynchClientServer' async_configuration = .delete(:async_configuration) || 0 return_cert_data = .key?(:return_cert_data) ? .delete(:return_cer_data) : true return_revocation_data = .key?(:return_revocation_data) ? .delete(:return_revocation_data) : true # SP challenge token sp_challenge = generate_sp_challenge phone = ensure_area_code(phone) self.session_code = nil # Make webservice call response = savon_client.call('MobileAuthenticate') do |locals| locals. 'CountryCode' => country_code, 'PhoneNo' => phone, 'Language' => language, 'ServiceName' => service_name, 'MessageToDisplay' => , 'SPChallenge' => sp_challenge, 'MessagingMode' => messaging_mode, 'AsyncConfiguration' => async_configuration, 'ReturnCertData' => return_cert_data, 'ReturnRevocationData' => return_revocation_data, 'IdCode' => personal_code end if soap_fault?(response) result = response.to_hash[:fault] else result = response.to_hash[:mobile_authenticate_response] self.session_code = result[:sesscode] end respond_with_hash_or_nested(result) end |
#authentication_status(session_code = self.session_code) ⇒ Object
Authentication status
75 76 77 78 79 80 81 82 |
# File 'lib/digidoc/client.rb', line 75 def authentication_status(session_code = self.session_code) response = savon_client.call('GetMobileAuthenticateStatus') do |locals| locals. 'Sesscode' => session_code end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:get_mobile_authenticate_status_response] respond_with_hash_or_nested(result) end |
#close_session(session_code = self.session_code) ⇒ Object
Closes current session
278 279 280 281 282 283 284 285 286 |
# File 'lib/digidoc/client.rb', line 278 def close_session(session_code = self.session_code) response = savon_client.call('CloseSession') do |locals| locals. 'Sesscode' => session_code end self.session_code = nil result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:close_session_response] respond_with_hash_or_nested(result) end |
#create_signed_doc(*args) ⇒ Object
Creates DigiDoc container
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/digidoc/client.rb', line 108 def create_signed_doc(*args) = args.last || {} format = .delete(:format) || :bdoc # vaikimisi bdoc if format == :xml version = '1.3' format = 'DIGIDOC-XML' elsif format == :bdoc version = '2.1' format = 'BDOC' end session_code = .delete(:session_code) || self.session_code response = savon_client.call('CreateSignedDoc') do |locals| locals. 'Sesscode' => session_code, 'Format' => format, 'Version' => version end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:create_signed_doc_response] respond_with_hash_or_nested(result) end |
#finalize_signature(*args) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/digidoc/client.rb', line 153 def finalize_signature(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code signature = .delete(:signature) signature_id = .delete(:signature_id) response = savon_client.call('FinalizeSignature') do |locals| locals. 'Sesscode' => session_code, 'SignatureValue' => signature, 'SignatureId' => signature_id end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:finalize_signature_response] respond_with_hash_or_nested(result) end |
#mobile_sign(*args) ⇒ Object
Sign DigiDoc container
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/digidoc/client.rb', line 183 def mobile_sign(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code phone = .delete(:phone) personal_code = .delete(:personal_code) country_code = .delete(:country_code) || 'EE' country_name = .delete(:country_name) || 'Eesti' language = .delete(:language) || 'EST' service_name = .delete(:service_name) || 'Testimine' = .delete(:message_to_display) || 'Allkirjastamine' messaging_mode = .delete(:messaging_mode) || 'asynchClientServer' async_configuration = .delete(:async_configuration) || 0 return_doc_info = .key?(:return_doc_info) ? .delete(:return_doc_info) : true return_doc_data = .key?(:return_doc_data) ? .delete(:return_doc_data) : true state_or_province = .delete(:state_or_province) role = .delete(:role) city = .delete(:city) postal_code = .delete(:postal_code) phone = ensure_area_code(phone) response = savon_client.call('MobileSign') do |locals| locals. 'Sesscode' => session_code, 'SignersCountry' => country_code, 'CountryName' => country_name, 'SignerPhoneNo' => phone, 'Language' => language, 'ServiceName' => service_name, 'AdditionalDataToBeDisplayed' => , 'MessagingMode' => messaging_mode, 'AsyncConfiguration' => async_configuration, 'ReturnDocInfo' => return_doc_info, 'ReturnDocData' => return_doc_data, 'SignerIDCode' => personal_code, 'Role' => role, 'City' => city, 'StateOrProvince' => state_or_province, 'PostalCode' => postal_code end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:mobile_sign_response] respond_with_hash_or_nested(result) end |
#notary(*args) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/digidoc/client.rb', line 168 def notary(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code signature_id = .delete(:signature_id) response = savon_client.call('GetNotary') do |locals| locals. 'Sesscode' => session_code, 'SignatureId' => signature_id end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:get_notary_response] respond_with_hash_or_nested(result) end |
#prepare_signature(*args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/digidoc/client.rb', line 130 def prepare_signature(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code signers_certificate = .delete(:signers_certificate) signers_token_id = .delete(:signers_token_id) signing_profile = .delete(:signing_profile) country_name = .delete(:country_name) || 'Eesti' state_or_province = .delete(:state_or_province) role = .delete(:role) city = .delete(:city) postal_code = .delete(:postal_code) response = savon_client.call('PrepareSignature') do |locals| locals. 'Sesscode' => session_code, 'SignersCertificate' => signers_certificate, 'SignersTokenId' => signers_token_id, 'Role' => role, 'City' => city, 'State' => state_or_province, 'PostalCode' => postal_code, 'Country' => country_name, 'SigningProfile' => signing_profile end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:prepare_signature_response] respond_with_hash_or_nested(result) end |
#save_signed_doc(*args, &block) ⇒ Object
Get DigiDoc container
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 |
# File 'lib/digidoc/client.rb', line 247 def save_signed_doc(*args, &block) = args.last || {} session_code = .delete(:session_code) || self.session_code response = savon_client.call('GetSignedDoc') do |locals| locals. 'Sesscode' => session_code end if soap_fault?(response) result = respond_with_hash_or_nested(response.to_hash[:fault]) else escaped = Crack::XML.parse(response.http.body).to_hash['SOAP_ENV:Envelope']['SOAP_ENV:Body']['dig:GetSignedDocResponse']['SignedDocData'] digidoc_container = escaped if .present? xmldata = Nokogiri::XML(digidoc_container) xmldata.root.elements.each { |el| el.replace(.shift) if el.name == 'DataFile' } digidoc_container = xmldata.to_xml end format = digidoc_container.starts_with?('<?xml') ? :xml : :bdoc if block_given? yield(digidoc_container, format) else digidoc_container end end end |
#sign_status(*args) ⇒ Object
Get session status info.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/digidoc/client.rb', line 218 def sign_status(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code return_doc_info = .key?(:return_doc_info) ? .delete(:return_doc_info) : false wait_signature = .key?(:wait_signature) ? .delete(:wait_signature) : false response = savon_client.call('GetStatusInfo') do |locals| locals. 'Sesscode' => session_code, 'ReturnDocInfo' => return_doc_info, 'WaitSignature' => wait_signature end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:get_status_info_response] respond_with_hash_or_nested(result) end |
#signed_doc_info(*args) ⇒ Object
Get DigiDoc container status
234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/digidoc/client.rb', line 234 def signed_doc_info(*args) = args.last || {} session_code = .delete(:session_code) || self.session_code response = savon_client.call('GetSignedDocInfo') do |locals| locals. 'Sesscode' => session_code end result = soap_fault?(response) ? response.to_hash[:fault] : response.to_hash[:get_signed_doc_info_response] respond_with_hash_or_nested(result) end |
#start_session(*args) ⇒ Object
Starts and holds session
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/digidoc/client.rb', line 85 def start_session(*args) self.session_code = nil self. = [] = args.last || {} signed_doc_file = .delete(:signed_doc_file) signed_doc_xml = signed_doc_file.read if signed_doc_file signed_doc_xml = Base64.encode64(signed_doc_xml) if signed_doc_xml && signed_doc_xml.starts_with?('PK') response = savon_client.call('StartSession') do |locals| locals. 'bHoldSession' => true, 'SigDocXML' => signed_doc_xml end if soap_fault?(response) result = response.to_hash[:fault] else result = response.to_hash[:start_session_response] self.session_code = result[:sesscode] end respond_with_hash_or_nested(result) end |