Class: Arca::WSAA

Inherits:
Object
  • Object
show all
Defined in:
lib/arca/wsaa.rb

Constant Summary collapse

WSDL =
{
  development: "https://wsaahomo.afip.gov.ar/ws/services/LoginCms?wsdl",
  production: "https://wsaa.afip.gov.ar/ws/services/LoginCms?wsdl",
  test: "#{Root}/test/fixtures/wsaa/wsaa.wsdl"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WSAA

Returns a new instance of WSAA.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/arca/wsaa.rb', line 15

def initialize(options = {})
  @env = (options[:env] || :test).to_sym
  @key = options[:key]
  @cert = options[:cert]
  @service = options[:service] || "wsfe"
  @ttl = options[:ttl] || 2400
  @cuit = options[:cuit]
  @client = Client.new Hash(options[:savon]).reverse_merge(wsdl: WSDL[@env])
  @store = options[:store]
  @ta_path = options[:ta_path] || default_ta_path
end

Instance Attribute Details

#certObject (readonly)

Returns the value of attribute cert.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def cert
  @cert
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def client
  @client
end

#cuitObject (readonly)

Returns the value of attribute cuit.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def cuit
  @cuit
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def env
  @env
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def key
  @key
end

#serviceObject (readonly)

Returns the value of attribute service.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def service
  @service
end

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def store
  @store
end

#taObject (readonly)

Returns the value of attribute ta.



7
8
9
# File 'lib/arca/wsaa.rb', line 7

def ta
  @ta
end

Instance Method Details

#authObject



65
66
67
68
# File 'lib/arca/wsaa.rb', line 65

def auth
  ta = obtener_y_cachear_ta
  { token: ta[:token], sign: ta[:sign] }
end

#codificar_tra(pkcs7) ⇒ Object



46
47
48
# File 'lib/arca/wsaa.rb', line 46

def codificar_tra(pkcs7)
  pkcs7.to_pem.lines.to_a[1..-2].join
end

#firmar_tra(tra, key, crt) ⇒ Object



40
41
42
43
44
# File 'lib/arca/wsaa.rb', line 40

def firmar_tra(tra, key, crt)
  key = OpenSSL::PKey::RSA.new key
  crt = OpenSSL::X509::Certificate.new crt
  OpenSSL::PKCS7.sign crt, key, tra
end

#generar_tra(service, ttl) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/arca/wsaa.rb', line 27

def generar_tra(service, ttl)
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  xml.loginTicketRequest version: 1 do
    xml.header do
      xml.uniqueId Time.now.to_i
      xml.generationTime xsd_datetime Time.now - ttl
      xml.expirationTime xsd_datetime Time.now + ttl
    end
    xml.service service
  end
end

#loginObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/arca/wsaa.rb', line 54

def 
  response = @client.request :login_cms, in0: tra(@key, @cert, @service, @ttl)
  ta = Nokogiri::XML(Nokogiri::XML(response.to_xml).text)
  {
    token: ta.css("token").text,
    sign: ta.css("sign").text,
    generation_time: from_xsd_datetime(ta.css("generationTime").text),
    expiration_time: from_xsd_datetime(ta.css("expirationTime").text)
  }
end

#tra(key, cert, service, ttl) ⇒ Object



50
51
52
# File 'lib/arca/wsaa.rb', line 50

def tra(key, cert, service, ttl)
  codificar_tra firmar_tra(generar_tra(service, ttl), key, cert)
end