Sha256: 6e6b562762711113705bcd03ca03469006ff88c6e7863e2f0daa8fa0d51cc6c8

Contents?: true

Size: 1.13 KB

Versions: 130

Compression:

Stored size: 1.13 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
        ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base || Rails.application.secrets.secret_key_base
      end
    end
  end
end

Version data entries

130 entries across 130 versions & 1 rubygems

Version Path
avo-2.47.0 lib/avo/services/encryption_service.rb
avo-3.0.1.beta17 lib/avo/services/encryption_service.rb
avo-3.0.1.beta18 lib/avo/services/encryption_service.rb
avo-3.0.1.beta19 lib/avo/services/encryption_service.rb
avo-3.0.1.beta15 lib/avo/services/encryption_service.rb
avo-3.0.1.beta16 lib/avo/services/encryption_service.rb
avo-3.0.1.beta14 lib/avo/services/encryption_service.rb
avo-3.0.1.beta12 lib/avo/services/encryption_service.rb
avo-3.0.1.beta13 lib/avo/services/encryption_service.rb
avo-3.0.1.beta10 lib/avo/services/encryption_service.rb
avo-3.0.1.beta11 lib/avo/services/encryption_service.rb
avo-3.0.0.pre19 lib/avo/services/encryption_service.rb
avo-3.0.1.beta8 lib/avo/services/encryption_service.rb
avo-3.0.1.beta9 lib/avo/services/encryption_service.rb
avo-3.0.1.beta6 lib/avo/services/encryption_service.rb
avo-3.0.1.beta7 lib/avo/services/encryption_service.rb
avo-3.0.1.beta5 lib/avo/services/encryption_service.rb
avo-3.0.1.beta2 lib/avo/services/encryption_service.rb
avo-3.0.1.beta4 lib/avo/services/encryption_service.rb
avo-3.0.1.beta3 lib/avo/services/encryption_service.rb