Sha256: 36ad383ba47a38ab5bde7539369daa4f6e3fd8d2e6f930102ec02fa24cfaa595

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

module Avo
  module Services
    class EncryptionService
      attr_reader :message
      attr_reader :purpose
      attr_reader :crypt

      class << self
        def encrypt(message:, purpose:)
          new(message: message, purpose: purpose).encrypt
        end

        def decrypt(message:, purpose:)
          new(message: message, purpose: purpose).decrypt
        end
      end

      def initialize(message:, purpose:)
        @message = message
        @purpose = purpose
        @crypt = ActiveSupport::MessageEncryptor.new(encryption_key)
      end

      def encrypt
        crypt.encrypt_and_sign(message, purpose: purpose)
      end

      def decrypt
        crypt.decrypt_and_verify(message, purpose: purpose)
      end

      private

      def encryption_key
        secret_key_base[0..31]
      rescue
        # This will fail the decryption process.
        # It's here only to keep Avo from crashing
        SecureRandom.random_bytes(32)
      end

      def secret_key_base
        Rails.application.secrets.secret_key_base || ENV['SECRET_KEY_BASE']
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
avo-2.14.3.pre.7.polytranslations1 lib/avo/services/encryption_service.rb
avo-2.14.3.pre.6.nosprockets lib/avo/services/encryption_service.rb
avo-2.14.3.pre.5.nosprockets lib/avo/services/encryption_service.rb
avo-2.14.3.pre.4.tosqlfix lib/avo/services/encryption_service.rb
avo-2.14.3.pre.3.jsbundling lib/avo/services/encryption_service.rb
avo-2.14.3.pre.2.tailwindcss lib/avo/services/encryption_service.rb
avo-2.14.3.pre.1.branding lib/avo/services/encryption_service.rb
avo-2.14.2 lib/avo/services/encryption_service.rb
avo-2.14.2.pre.1 lib/avo/services/encryption_service.rb
avo-2.14.1 lib/avo/services/encryption_service.rb
avo-2.14.1.pre.1 lib/avo/services/encryption_service.rb