optimize tsa settings controller

pull/289/head
Pete Matsyburka 1 year ago
parent 55d9511c01
commit 0137bdbe4c

@ -4,6 +4,8 @@ class TimestampServerController < ApplicationController
before_action :build_encrypted_config before_action :build_encrypted_config
authorize_resource :encrypted_config authorize_resource :encrypted_config
TimestampError = Class.new(StandardError)
def create def create
return head :not_found if Docuseal.multitenant? return head :not_found if Docuseal.multitenant?
@ -14,25 +16,29 @@ class TimestampServerController < ApplicationController
else else
redirect_back fallback_location: settings_notifications_path, alert: 'Unable to save' redirect_back fallback_location: settings_notifications_path, alert: 'Unable to save'
end end
rescue HexaPDF::Error, SocketError, Submissions::TimestampHandler::TimestampError, OpenSSL::Timestamp::TimestampError rescue SocketError, TimestampError, OpenSSL::Timestamp::TimestampError
redirect_back fallback_location: settings_notifications_path, alert: 'Invalid Timeserver' redirect_back fallback_location: settings_notifications_path, alert: 'Invalid Timeserver'
end end
private private
def test_timeserver_url(url) def test_timeserver_url(url)
pdf = HexaPDF::Document.new req = OpenSSL::Timestamp::Request.new
pdf.pages.add req.algorithm = 'SHA512'
req.message_imprint = 'test'
pkcs = Accounts.load_signing_pkcs(current_account)
uri = Addressable::URI.parse(url)
pdf.sign(StringIO.new,
reason: 'Test', conn = Faraday.new(uri.origin) do |c|
certificate: pkcs.certificate, c.basic_auth(uri.user, uri.password) if uri.password.present?
key: pkcs.key, end
signature_size: 10_000,
certificate_chain: pkcs.ca_certs || [], response = conn.post(uri.path, req.to_der,
timestamp_handler: Submissions::TimestampHandler.new(tsa_url: url)) 'content-type' => 'application/timestamp-query')
raise TimestampError if response.status != 200 || response.body.blank?
response
end end
def load_encrypted_config def load_encrypted_config

Loading…
Cancel
Save