class Afip::Wsaa

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

Public Class Methods

login(service = "wsfe") click to toggle source

Main method for authentication and authorization. When successful, produces the yaml file with auth data.

# File lib/Afip/wsaa.rb, line 9
def self.login(service = "wsfe")
  tra   = build_tra(service)
  cms   = build_cms(tra)
  req   = build_request(cms)
  auth  = call_wsaa(req)

  write_yaml(auth)
end

Protected Class Methods

build_cms(tra) click to toggle source

Builds the CMS @return [String] cms

# File lib/Afip/wsaa.rb, line 44
def self.build_cms(tra)
  cms = `echo '#{ tra }' |
    #{ Afip.openssl_bin } cms -sign -in /dev/stdin -signer #{ Afip.cert } -inkey #{ Afip.pkey } -nodetach \
            -outform der |
    #{ Afip.openssl_bin } base64 -e`
  return cms
end
build_request(cms) click to toggle source

Builds the CMS request to log in to the server @return [String] the cms body

# File lib/Afip/wsaa.rb, line 55
    def self.build_request(cms)
      request = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://wsaa.view.sua.dvadac.desein.afip.gov">
  <SOAP-ENV:Body>
    <ns1:loginCms>
      <ns1:in0>
#{ cms }
      </ns1:in0>
    </ns1:loginCms>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML
      return request
    end
build_tra(service) click to toggle source

Builds the xml for the 'Ticket de Requerimiento de Acceso' @return [String] containing the request body

# File lib/Afip/wsaa.rb, line 22
    def self.build_tra service
      @now = (Time.now) - 120
      @from = @now.strftime('%FT%T%:z')
      @to   = (@from.to_time.end_of_day).strftime('%FT%T%:z')
      @id   = @now.strftime('%s')
      tra  = <<-EOF

<loginTicketRequest version="1.0">
  <header>
    <uniqueId>#{ @id }</uniqueId>
    <generationTime>#{ @from }</generationTime>
    <expirationTime>#{ @to }</expirationTime>
  </header>
  <service>#{service}</service>
</loginTicketRequest>
EOF
      return tra
    end
call_wsaa(req) click to toggle source

Calls the WSAA with the request built by build_request @return [Array] with the token and signature

# File lib/Afip/wsaa.rb, line 74
def self.call_wsaa(req)
  response = `echo '#{ req }' | curl -k -s -H 'Content-Type: application/soap+xml; action=""' -d @- #{ Afip::AuthData.wsaa_url }`
  pp response
  response = CGI::unescapeHTML(response)
  token = response.scan(/\<token\>(.+)\<\/token\>/).first.first
  sign  = response.scan(/\<sign\>(.+)\<\/sign\>/).first.first
  return [token, sign]
end
write_yaml(certs) click to toggle source

Writes the token and signature to a YAML file in the /tmp directory

# File lib/Afip/wsaa.rb, line 85
    def self.write_yaml(certs)
      yml = <<-YML
      token: #{certs[0]}
      sign: #{certs[1]}
      YML
    `echo '#{ yml }' > /tmp/#{Afip::AuthData.environment.to_s}_Afip_#{ Afip.cuit }_#{ Time.new.strftime('%Y_%m_%d') }.yml`
    end