connection.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Exchange::Connection < Sequencer::Unit::Common::Provider::Fallback
  3. uses :ews_config
  4. provides :ews_connection
  5. private
  6. def ews_connection
  7. load_viewpoint_class
  8. Viewpoint::EWSClient.new({
  9. endpoint: config[:endpoint],
  10. type: config[:auth_type],
  11. token: config[:access_token],
  12. user: config[:user],
  13. password: config[:password]
  14. }, additional_opts)
  15. end
  16. def config
  17. @config ||= begin
  18. config = ews_config
  19. if !ews_config
  20. config = ::Import::Exchange.config
  21. if config[:auth_type] == 'oauth'
  22. config = config.merge(Setting.get('exchange_oauth'))
  23. end
  24. end
  25. config
  26. end
  27. end
  28. def additional_opts
  29. @additional_opts ||= begin
  30. http_opts
  31. end
  32. end
  33. def http_opts
  34. return {} if config[:disable_ssl_verify].blank?
  35. {
  36. http_opts: {
  37. ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE
  38. }
  39. }
  40. end
  41. def load_viewpoint_class
  42. return if defined?(Viewpoint::EWS::Connection)
  43. require 'viewpoint'
  44. Viewpoint::EWS::Connection.class_eval do
  45. # ---
  46. # Zammad
  47. # ---
  48. # def initialize(endpoint, opts = {})
  49. # @log = Logging.logger[self.class.name.to_s.to_sym]
  50. # if opts[:user_agent]
  51. # @httpcli = HTTPClient.new(agent_name: opts[:user_agent])
  52. # else
  53. # @httpcli = HTTPClient.new
  54. # end
  55. #
  56. # if opts[:trust_ca]
  57. # @httpcli.ssl_config.clear_cert_store
  58. # opts[:trust_ca].each do |ca|
  59. # @httpcli.ssl_config.add_trust_ca ca
  60. # end
  61. # end
  62. #
  63. # @httpcli.ssl_config.verify_mode = opts[:ssl_verify_mode] if opts[:ssl_verify_mode]
  64. # @httpcli.ssl_config.ssl_version = opts[:ssl_version] if opts[:ssl_version]
  65. # # Up the keep-alive so we don't have to do the NTLM dance as often.
  66. # @httpcli.keep_alive_timeout = 60
  67. # @httpcli.receive_timeout = opts[:receive_timeout] if opts[:receive_timeout]
  68. # @httpcli.connect_timeout = opts[:connect_timeout] if opts[:connect_timeout]
  69. # @endpoint = endpoint
  70. # end
  71. def initialize(auth, opts = {})
  72. @log = Logging.logger[self.class.name.to_s.to_sym]
  73. @httpcli = http_object(opts)
  74. @auth_type = auth[:type]
  75. @auth_token = @auth_type == 'oauth' ? auth[:token] : nil
  76. @endpoint = auth[:endpoint]
  77. end
  78. # ---
  79. def post(xmldoc)
  80. headers = { 'Content-Type' => 'text/xml' }
  81. # ---
  82. # Zammad
  83. # ---
  84. if @auth_type == 'oauth' && @auth_token.present?
  85. headers = headers.merge({ 'Authorization' => "Bearer #{@auth_token}" })
  86. end
  87. # ---
  88. check_response(@httpcli.post(@endpoint, xmldoc, headers))
  89. end
  90. # ---
  91. # Zammad
  92. # ---
  93. private
  94. def http_object(opts)
  95. @httpcli = if opts[:user_agent]
  96. HTTPClient.new(agent_name: opts[:user_agent])
  97. else
  98. HTTPClient.new
  99. end
  100. trust_ca_option(opts)
  101. ssl_config(opts)
  102. timeout_options(opts)
  103. @httpcli
  104. end
  105. def trust_ca_option(opts)
  106. return if opts[:trust_ca].nil?
  107. @httpcli.ssl_config.clear_cert_store
  108. opts[:trust_ca].each do |ca|
  109. @httpcli.ssl_config.add_trust_ca ca
  110. end
  111. end
  112. def ssl_config(opts)
  113. Certificate::ApplySSLCertificates.ensure_fresh_ssl_context
  114. @httpcli.ssl_config.verify_mode = opts[:ssl_verify_mode] if opts[:ssl_verify_mode]
  115. @httpcli.ssl_config.ssl_version = opts[:ssl_version] if opts[:ssl_version]
  116. end
  117. def timeout_options(opts)
  118. # Up the keep-alive so we don't have to do the NTLM dance as often.
  119. @httpcli.keep_alive_timeout = 60
  120. @httpcli.receive_timeout = opts[:receive_timeout] if opts[:receive_timeout]
  121. @httpcli.connect_timeout = opts[:connect_timeout] if opts[:connect_timeout]
  122. end
  123. # ---
  124. end
  125. Viewpoint::EWSClient.class_eval do
  126. # ---
  127. # Zammad
  128. # ---
  129. # def initialize(endpoint, username, password, opts = {})
  130. # # dup all. @see ticket https://github.com/zenchild/Viewpoint/issues/68
  131. # @endpoint = endpoint.dup
  132. # @username = username.dup
  133. # password = password.dup
  134. # opts = opts.dup
  135. # http_klass = opts[:http_class] || Viewpoint::EWS::Connection
  136. # con = http_klass.new(endpoint, opts[:http_opts] || {})
  137. # con.set_auth @username, password
  138. # @ews = SOAP::ExchangeWebService.new(con, opts)
  139. # end
  140. def initialize(auth, opts = {})
  141. auth = auth.dup
  142. @auth_type = auth[:type]
  143. @auth_token = @auth_type == 'oauth' ? auth[:token] : nil
  144. @endpoint = auth[:endpoint]
  145. @username = auth[:user]
  146. password = @auth_type == 'basic' ? auth[:password] : nil
  147. http_klass = opts[:http_class] || Viewpoint::EWS::Connection
  148. connection = http_klass.new(auth, opts[:http_opts] || {})
  149. connection.set_auth(@username, password) if password.present?
  150. @ews = Viewpoint::EWS::SOAP::ExchangeWebService.new(connection, opts)
  151. end
  152. # ---
  153. end
  154. end
  155. end