Class: Cfdi40::SatCsd

Inherits:
Object
  • Object
show all
Defined in:
lib/cfdi40/sat_csd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



5
6
7
# File 'lib/cfdi40/sat_csd.rb', line 5

def private_key
  @private_key
end

#x509_certObject (readonly)

Returns the value of attribute x509_cert.



5
6
7
# File 'lib/cfdi40/sat_csd.rb', line 5

def x509_cert
  @x509_cert
end

Instance Method Details

#cert64Object



62
63
64
65
66
# File 'lib/cfdi40/sat_csd.rb', line 62

def cert64
  return unless x509_cert

  Base64.strict_encode64 x509_cert.to_der
end

#cert64=(data) ⇒ Object

Loads certficate encoded in Base64. Certs with Base64 encoding are used in CFDIs



18
19
20
# File 'lib/cfdi40/sat_csd.rb', line 18

def cert64=(data)
  self.cert_der = Base64.decode64(data)
end

#cert_der=(data) ⇒ Object



12
13
14
# File 'lib/cfdi40/sat_csd.rb', line 12

def cert_der=(data)
  @x509_cert = OpenSSL::X509::Certificate.new(data)
end

#cert_path=(path) ⇒ Object



7
8
9
10
# File 'lib/cfdi40/sat_csd.rb', line 7

def cert_path=(path)
  @cert_path = path
  @x509_cert = OpenSSL::X509::Certificate.new(File.read(path))
end

#load_private_key(key_path, key_pass) ⇒ Object



22
23
24
25
# File 'lib/cfdi40/sat_csd.rb', line 22

def load_private_key(key_path, key_pass)
  key_pem = key_to_pem(File.read(key_path))
  @private_key = OpenSSL::PKey::RSA.new(key_pem, key_pass)
end

#nameObject



41
42
43
44
45
46
47
48
# File 'lib/cfdi40/sat_csd.rb', line 41

def name
  return unless subject_data

  subject_name = subject_data.select { |data| data[0] == "name" }.first
  return unless subject_name

  subject_name[1]
end

#no_certificadoObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cfdi40/sat_csd.rb', line 50

def no_certificado
  return unless x509_cert

  s = ""
  x509_cert.serial.to_s(16).chars.each_with_index do |c, i|
    next if i.even?

    s += c
  end
  s
end

#rfcObject



32
33
34
35
36
37
38
39
# File 'lib/cfdi40/sat_csd.rb', line 32

def rfc
  return unless subject_data

  unique_identifier = subject_data.select { |data| data[0] == "x500UniqueIdentifier" }.first
  return unless unique_identifier

  unique_identifier[1].split(" / ").first
end

#set_private_key(key_data, key_pass = nil) ⇒ Object



27
28
29
30
# File 'lib/cfdi40/sat_csd.rb', line 27

def set_private_key(key_data, key_pass = nil)
  key_pem = (pem_format?(key_data) ? key_data : key_to_pem(key_data))
  @private_key = OpenSSL::PKey::RSA.new(key_pem, key_pass)
end

#valid_pair?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/cfdi40/sat_csd.rb', line 68

def valid_pair?
  return false unless x509_cert && private_key

  x509_cert.check_private_key private_key
end