class AfipWsfe::Wsaa

Authorization class. Handles interactions wiht the WSAA, to provide valid key and signature that will last for a day.

Public Class Methods

new(url=nil) click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 7
def initialize(url=nil)
  @client = AfipWsfe::Client.new(false)
  @endpoint = :wsaa
  @response = nil
  @status = false
end

Public Instance Methods

login() click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 14
def login
  @status, @response = @client.call_endpoint @endpoint, :login_cms, {in0: build_tra}
  parse_response
  @status
end

Private Instance Methods

build_tra() click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 22
def build_tra
  now  = Time.zone.now
  from = now.beginning_of_day.strftime('%FT%T%:z')
  to   = now.end_of_day.strftime('%FT%T%:z')
  id   = now.strftime('%s')

  tra = {
    "header" => {
      "uniqueId"       => id,
      "generationTime" => from,
      "expirationTime" => to
    },
    "service" => "wsfe"
  }.to_xml(root: "loginTicketRequest")

  pkcs7 = OpenSSL::PKCS7.sign(cert, key, tra)
  OpenSSL::PKCS7.write_smime(pkcs7).lines[5..-2].join()
end
cert() click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 56
def cert
  OpenSSL::X509::Certificate.new(read_content(AfipWsfe.cert))
end
key() click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 60
def key
  OpenSSL::PKey::RSA.new(read_content(AfipWsfe.pkey))
end
parse_response() click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 41
def parse_response
  write_yaml(@response["loginTicketResponse"]["credentials"])
end
read_content(my_file) click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 64
def read_content(my_file)
  raise(NullOrInvalidAttribute.new, "No está definido el storage de las keys") unless AfipWsfe.storage
  raise(NullOrInvalidAttribute.new, "No se han proporcionado las keys necesarias") unless my_file
  
  if AfipWsfe.storage == :file
    File.read my_file
  else
    my_file
  end
end
write_yaml(credentials) click to toggle source
# File lib/afip_wsfe/wsaa.rb, line 45
def write_yaml(credentials)
  filename = AuthData.todays_data_file_name
  content = {
    token: credentials["token"],
    sign: credentials["sign"]
  }
  File.open(filename, 'w') { |f|
    f.write content.to_yaml
  }
end