class PkiExpress::PadesSigner

Attributes

certification_level[RW]
custom_signature_field_name[RW]
overwrite_original_file[RW]
reason[RW]
suppress_default_visual_representation[RW]

Public Class Methods

new(config=PkiExpressConfig.new) click to toggle source
Calls superclass method
# File lib/pki_express/pades_signer.rb, line 7
def initialize(config=PkiExpressConfig.new)
  super(config)
  @pdf_to_sign_path = nil
  @vr_json_path = nil
  @overwrite_original_file = false
  @version_manager = VersionManager.new
  @custom_signature_field_name = nil
  @certification_level = nil
  @reason = nil
  @suppress_default_visual_representation = false
end

Public Instance Methods

pdf_to_sign() click to toggle source

region The “pdf_to_sign” accessors

# File lib/pki_express/pades_signer.rb, line 108
def pdf_to_sign
  _get_pdf_to_sign
end
pdf_to_sign=(content_raw) click to toggle source
# File lib/pki_express/pades_signer.rb, line 121
def pdf_to_sign=(content_raw)
  _set_pdf_to_sign(content_raw)
end
pdf_to_sign_base64() click to toggle source
# File lib/pki_express/pades_signer.rb, line 138
def pdf_to_sign_base64
  _get_pdf_to_sign_base64
end
pdf_to_sign_base64=(content_base64) click to toggle source
# File lib/pki_express/pades_signer.rb, line 152
def pdf_to_sign_base64=(content_base64)
  _set_pdf_to_sign_base64(content_base64)
end
pdf_to_sign_path() click to toggle source
# File lib/pki_express/pades_signer.rb, line 171
def pdf_to_sign_path
  _get_pdf_to_sign_path
end
pdf_to_sign_path=(path) click to toggle source
# File lib/pki_express/pades_signer.rb, line 180
def pdf_to_sign_path=(path)
  _set_pdf_to_sign_path(path)
end
sign(get_cert=false) click to toggle source

endregion

# File lib/pki_express/pades_signer.rb, line 197
def sign(get_cert=false)
  unless @pdf_to_sign_path
    raise 'The PDF to be signed was not set'
  end

  unless @overwrite_original_file || @output_file_path
    raise 'The output destination was not set'
  end

  args = [
    @pdf_to_sign_path,
  ]

  # Logic to overwrite original file or use the output file
  if @overwrite_original_file
    args.append('--overwrite')
  else
    args.append(@output_file_path)
  end

  # Verify and add common options between signers.
  verify_and_add_common_options(args)

  if @vr_json_path
    args.append('--visual-rep')
    args.append(@vr_json_path)
  end

  if @custom_signature_field_name
    args.append('--custom-signature-field-name')
    args.append(@custom_signature_field_name)
    # This option can only be used on versions greater than 1.15.0 of the
    # PKI Express.
    @version_manager.require_version('1.15')
  end

  if @certification_level
    args.append('--certification-level')
    args.append(@certification_level)
    # This option can only be used on versions greater than 1.16.0 of the
    # PKI Express.
    @version_manager.require_version('1.16')
  end

  if @suppress_default_visual_representation
    args.append('--suppress-default-visual-rep')
    # This option can only be used on versions greater than 1.13.1 of the
    # PKI Express.
    @version_manager.require_version('1.13.1')
  end

  if @reason
    args.append('--reason')
    args.append(@reason)
    # This option can only be used on versions greater than 1.13 of the
    # PKI Express.
    @version_manager.require_version('1.13')
  end

  if get_cert
    # This option can only be used on versions greater than 1.8.0 of the
    # PKI Express.
    @version_manager.require_version('1.8')

    # Invoke command.
    result = invoke(Commands::SIGN_PADES, args)

    # Parse output and return result.
    model = parse_output(result)
    return PKCertificate.new(model.fetch(:signer))
  else  
    # Invoke command with plain text output (to support PKI Express < 1.3)
    result = invoke_plain(Commands::SIGN_PADES, args)
  end

end
visual_representation() click to toggle source
# File lib/pki_express/pades_signer.rb, line 75
def visual_representation
  _get_visual_representation
end
visual_representation=(vr) click to toggle source
# File lib/pki_express/pades_signer.rb, line 90
def visual_representation=(vr)
  _set_visual_representation(vr)
end
visual_representation_content_raw() click to toggle source

region set_visual_representation

# File lib/pki_express/pades_signer.rb, line 21
def visual_representation_content_raw
  _get_visual_representation_content_raw
end
visual_representation_content_raw=(content_raw) click to toggle source
# File lib/pki_express/pades_signer.rb, line 34
def visual_representation_content_raw=(content_raw)
  _set_visual_representation_content_raw(content_raw)
end
visual_representation_path() click to toggle source
# File lib/pki_express/pades_signer.rb, line 51
def visual_representation_path
  _get_visual_representation_path
end
visual_representation_path=(path) click to toggle source
# File lib/pki_express/pades_signer.rb, line 60
def visual_representation_path=(path)
  _set_visual_representation_path(path)
end

Private Instance Methods

_get_pdf_to_sign() click to toggle source
# File lib/pki_express/pades_signer.rb, line 112
def _get_pdf_to_sign
  unless @pdf_to_sign_path
    return nil
  end

  File.read(@pdf_to_sign_path)
end
_get_pdf_to_sign_base64() click to toggle source
# File lib/pki_express/pades_signer.rb, line 142
def _get_pdf_to_sign_base64
  unless @pdf_to_sign_path
    return nil
  end

  content = File.read(@pdf_to_sign_path)
  Base64.encode64(content)
end
_get_pdf_to_sign_path() click to toggle source
# File lib/pki_express/pades_signer.rb, line 175
def _get_pdf_to_sign_path
  @pdf_to_sign_path
end
_get_visual_representation() click to toggle source
# File lib/pki_express/pades_signer.rb, line 79
def _get_visual_representation
  unless @vr_json_path
    return nil
  end

  content =  File.read(@vr_json_path)
  JSON.parse(content)
end
_get_visual_representation_content_raw() click to toggle source
# File lib/pki_express/pades_signer.rb, line 25
def _get_visual_representation_content_raw
  unless @vr_json_path
    return nil
  end

  File.read(@vr_json_path)
end
_get_visual_representation_path() click to toggle source
# File lib/pki_express/pades_signer.rb, line 55
def _get_visual_representation_path
  @vr_json_path
end
_set_pdf_to_sign(content_raw) click to toggle source
# File lib/pki_express/pades_signer.rb, line 125
def _set_pdf_to_sign(content_raw)
  unless content_raw
    raise 'The provided "pdf_to_sign" is not valid'
  end

  temp_file_path = self.create_temp_file
  File.open(temp_file_path, 'wb') do |f|
    f.write(content_raw)
  end
  @pdf_to_sign_path = temp_file_path
end
_set_pdf_to_sign_base64(content_base64) click to toggle source
# File lib/pki_express/pades_signer.rb, line 156
def _set_pdf_to_sign_base64(content_base64)
  unless content_base64
    raise 'The provided "pdf_to_sign_base64" is not valid'
  end

  begin
    content_raw = Base64.decode64(content_base64)
  rescue Error
    raise 'The provided "pdf_to_sign_base64" is not Base64-encoded'
  end

  _set_pdf_to_sign(content_raw)
end
_set_pdf_to_sign_path(path) click to toggle source
# File lib/pki_express/pades_signer.rb, line 184
def _set_pdf_to_sign_path(path)
  unless path
    raise 'The provided "pdf_to_sign_path" is not valid'
  end
  unless File.exists?(path)
    raise 'The provided "pdf_to_sign_path" does not exist'
  end
  @pdf_to_sign_path = path
end
_set_visual_representation(vr) click to toggle source
# File lib/pki_express/pades_signer.rb, line 94
def _set_visual_representation(vr)
  temp_file_path = self.create_temp_file
  json = JSON.pretty_generate(vr.to_model)
  File.open(temp_file_path, 'w') do |f|
    f.write(json)
  end
  @vr_json_path = temp_file_path
end
_set_visual_representation_content_raw(content_raw) click to toggle source
# File lib/pki_express/pades_signer.rb, line 38
def _set_visual_representation_content_raw(content_raw)
  unless content_raw
    raise 'The provided "visual_representation" is not valid'
  end

  temp_file_path = self.create_temp_file
  File.open(temp_file_path, 'wb') do |f|
    f.write(content_raw)
  end
  @vr_json_path = temp_file_path
end
_set_visual_representation_path(path) click to toggle source
# File lib/pki_express/pades_signer.rb, line 64
def _set_visual_representation_path(path)
  unless path
    raise 'The provided "visual_representation_path" is not valid'
  end
  unless File.exists?(path)
    raise 'The provided "visual_representation_path" does not exist'
  end
  @vr_json_path = path
end