Sha256: b55915db2a1a009edde87932db9ad6a9258a74228b662ebf0490349684db6506

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

# frozen-string-literal: true
require "action_controller"
require "action_view"
require "active_support/message_encryptor"

require "encrypted_form_fields/version"
require "encrypted_form_fields/dfs"
require "encrypted_form_fields/encrypted_parameters"
require "encrypted_form_fields/helpers/form_builder"
require "encrypted_form_fields/railtie" if defined?(Rails)

module EncryptedFormFields
  class << self
    def secret_key_base=(key)
      @encryptor = nil
      @secret_key_base = key
    end

    attr_reader :secret_key_base

    def secret_token=(key)
      @encryptor = nil
      @secret_token = key
    end

    attr_reader :secret_token

    def prefix_name(name)
      first, rest = name.split("[", 2)
      rest = "[" + rest if rest
      "_encrypted[#{first}]#{rest}"
    end

    delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor

    # Decrypt encrypted parameters object
    def decrypt_parameters(params)
      Dfs.traverse(params || {}) do |value|
        EncryptedFormFields.decrypt_and_verify(value)
      end
    end

    # Encrypt hash values
    def encrypt_parameters(hash = {})
      Dfs.traverse(hash, &method(:encrypt_and_sign))
    end

    private

    def encryptor
      @encryptor ||= begin
        key = ActiveSupport::KeyGenerator.new(secret_token).generate_key(secret_key_base, 32)
        ActiveSupport::MessageEncryptor.new(key)
      end
    end
  end
end

ActionController::Base.send(:include, EncryptedFormFields::EncryptedParameters)
ActionView::Helpers::FormBuilder.send(:include, EncryptedFormFields::Helpers::FormBuilder)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
encrypted_form_fields-1.0.0 lib/encrypted_form_fields.rb
encrypted_form_fields-0.2.4 lib/encrypted_form_fields.rb
encrypted_form_fields-0.2.3 lib/encrypted_form_fields.rb