Sha256: d932a337fa86bd6a21523c13b78f39d521b4f52c9b66a237c8213602ae518716

Contents?: true

Size: 1006 Bytes

Versions: 24

Compression:

Stored size: 1006 Bytes

Contents

module Avo
  module Services
    class EncryptionService
      class << self
        def encrypt(...)
          new(...).encrypt
        end

        def decrypt(...)
          new(...).decrypt
        end
      end

      def initialize(message:, purpose:, **kwargs)
        @message = message
        @purpose = purpose
        @crypt = ActiveSupport::MessageEncryptor.new(encryption_key, **kwargs)
      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
        ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base || Rails.application.secrets.secret_key_base
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
avo-3.2.3 lib/avo/services/encryption_service.rb
avo-3.0.1.beta23 lib/avo/services/encryption_service.rb
avo-3.0.1.beta20 lib/avo/services/encryption_service.rb
avo-3.0.1.beta21 lib/avo/services/encryption_service.rb
avo-3.0.1.beta22 lib/avo/services/encryption_service.rb
avo-3.0.1.beta24 lib/avo/services/encryption_service.rb
avo-3.2.2 lib/avo/services/encryption_service.rb
avo-3.2.1 lib/avo/services/encryption_service.rb
avo-3.2.0 lib/avo/services/encryption_service.rb
avo-3.1.7 lib/avo/services/encryption_service.rb
avo-3.1.6 lib/avo/services/encryption_service.rb
avo-3.1.5 lib/avo/services/encryption_service.rb
avo-3.1.4 lib/avo/services/encryption_service.rb
avo-3.1.3 lib/avo/services/encryption_service.rb
avo-3.1.2 lib/avo/services/encryption_service.rb
avo-3.1.1 lib/avo/services/encryption_service.rb
avo-3.1.0 lib/avo/services/encryption_service.rb
avo-3.0.8 lib/avo/services/encryption_service.rb
avo-3.0.7 lib/avo/services/encryption_service.rb
avo-3.0.6 lib/avo/services/encryption_service.rb