pgp_controller.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Integration::PGPController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def key_list
  5. model_index_render(PGPKey, params)
  6. end
  7. def key_show
  8. model_show_render(PGPKey, params)
  9. end
  10. def key_download
  11. key = PGPKey.find(params[:id])
  12. if %w[1 true].include?(params[:secret])
  13. raise Exceptions::UnprocessableEntity, __('This is not a private PGP key.') if !key.secret
  14. return send_data(
  15. key.key,
  16. filename: "#{key.fingerprint}.asc",
  17. type: 'text/plain',
  18. disposition: 'attachment'
  19. )
  20. end
  21. send_data(
  22. export(key),
  23. filename: "#{key.fingerprint}.pub.asc",
  24. type: 'text/plain',
  25. disposition: 'attachment'
  26. )
  27. end
  28. def key_add
  29. PGPKey.params_cleanup! params
  30. model_create_render(PGPKey, params)
  31. end
  32. def key_delete
  33. model_destroy_render(PGPKey, params)
  34. end
  35. def status
  36. if !SecureMailing::PGP.required_version?
  37. error = __('gpg (GnuPG) 2.2.0 or newer is required')
  38. end
  39. render json: { error: error }.compact
  40. end
  41. def search
  42. security_options = SecureMailing::PGP::SecurityOptions.new(ticket: params[:ticket], article: params[:article]).process
  43. result = {
  44. type: 'PGP',
  45. encryption: map_result(security_options.encryption),
  46. sign: map_result(security_options.signing),
  47. }
  48. render json: result
  49. end
  50. private
  51. def map_result(method_result)
  52. {
  53. success: method_result.possible?,
  54. comment: method_result.message,
  55. commentPlaceholders: method_result.message_placeholders,
  56. }
  57. end
  58. def export(key)
  59. SecureMailing::PGP::Tool.new.with_private_keyring do |pgp_tool|
  60. pgp_tool.import(key.key)
  61. pgp_tool.export(key.fingerprint).stdout
  62. end
  63. end
  64. end