Files
docuseal/app/controllers/timestamp_server_controller.rb
T

62 lines
1.9 KiB
Ruby
Raw Normal View History

2023-12-02 18:14:33 +02:00
# frozen_string_literal: true
class TimestampServerController < ApplicationController
2024-05-15 00:21:52 +03:00
HASH_ALGORITHM = 'SHA256'
2023-12-02 18:14:33 +02:00
before_action :build_encrypted_config
authorize_resource :encrypted_config
2024-05-12 19:32:09 +03:00
TimestampError = Class.new(StandardError)
2023-12-02 18:14:33 +02:00
def create
return head :not_found if Docuseal.multitenant?
test_timeserver_url(@encrypted_config.value) if @encrypted_config.value.present?
if @encrypted_config.value.present? ? @encrypted_config.save : @encrypted_config.delete
2024-09-18 19:47:47 +03:00
redirect_back fallback_location: settings_notifications_path, notice: I18n.t('changes_have_been_saved')
2023-12-02 18:14:33 +02:00
else
2024-09-18 19:47:47 +03:00
redirect_back fallback_location: settings_notifications_path, alert: I18n.t('unable_to_save')
2023-12-02 18:14:33 +02:00
end
2024-05-12 19:32:09 +03:00
rescue SocketError, TimestampError, OpenSSL::Timestamp::TimestampError
2024-09-18 19:47:47 +03:00
redirect_back fallback_location: settings_notifications_path, alert: t('invalid_timeserver')
2023-12-02 18:14:33 +02:00
end
private
def test_timeserver_url(url)
2024-05-12 19:32:09 +03:00
req = OpenSSL::Timestamp::Request.new
2024-05-15 00:21:52 +03:00
req.algorithm = HASH_ALGORITHM
req.message_imprint = OpenSSL::Digest.digest(HASH_ALGORITHM, 'test')
2024-05-12 19:32:09 +03:00
uri = Addressable::URI.parse(url)
conn = Faraday.new(uri.origin) do |c|
c.basic_auth(uri.user, uri.password) if uri.password.present?
end
response = conn.post(uri.path, req.to_der,
'content-type' => 'application/timestamp-query')
raise TimestampError if response.status != 200 || response.body.blank?
response
2023-12-02 18:14:33 +02:00
end
def load_encrypted_config
@encrypted_config
end
def build_encrypted_config
@encrypted_config =
EncryptedConfig.find_or_initialize_by(account: current_account,
key: EncryptedConfig::TIMESTAMP_SERVER_URL_KEY)
@encrypted_config.assign_attributes(encrypted_config_params)
end
def encrypted_config_params
params.require(:encrypted_config).permit(:value)
end
end