Sha256: eb3e47d8c8ee4365d918c50f5f66127ae0dd7ca9190e36f32fee2903b0514915

Contents?: true

Size: 1.45 KB

Versions: 73

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module Messages
    module Rotator # :nodoc:
      def initialize(*, **options)
        super

        @options   = options
        @rotations = []
      end

      def rotate(*secrets, **options)
        @rotations << build_rotation(*secrets, @options.merge(options))
      end

      module Encryptor
        include Rotator

        def decrypt_and_verify(*args, on_rotation: nil, **options)
          super
        rescue MessageEncryptor::InvalidMessage, MessageVerifier::InvalidSignature
          run_rotations(on_rotation) { |encryptor| encryptor.decrypt_and_verify(*args, options) } || raise
        end

        private
          def build_rotation(secret = @secret, sign_secret = @sign_secret, options)
            self.class.new(secret, sign_secret, options)
          end
      end

      module Verifier
        include Rotator

        def verified(*args, on_rotation: nil, **options)
          super || run_rotations(on_rotation) { |verifier| verifier.verified(*args, options) }
        end

        private
          def build_rotation(secret = @secret, options)
            self.class.new(secret, options)
          end
      end

      private
        def run_rotations(on_rotation)
          @rotations.find do |rotation|
            if message = yield(rotation) rescue next
              on_rotation.call if on_rotation
              return message
            end
          end
        end
    end
  end
end

Version data entries

73 entries across 65 versions & 10 rubygems

Version Path
activesupport-5.2.8.1 lib/active_support/messages/rotator.rb
activesupport-5.2.8 lib/active_support/messages/rotator.rb
activesupport-5.2.7.1 lib/active_support/messages/rotator.rb
activesupport-5.2.7 lib/active_support/messages/rotator.rb
activesupport-5.2.6.3 lib/active_support/messages/rotator.rb
activesupport-5.2.6.2 lib/active_support/messages/rotator.rb
activesupport-5.2.6.1 lib/active_support/messages/rotator.rb
activesupport-5.2.6 lib/active_support/messages/rotator.rb
activesupport-5.2.4.6 lib/active_support/messages/rotator.rb
activesupport-5.2.5 lib/active_support/messages/rotator.rb
activesupport-5.2.4.5 lib/active_support/messages/rotator.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.1/lib/active_support/messages/rotator.rb
activesupport-5.2.4.4 lib/active_support/messages/rotator.rb
activesupport-5.2.4.3 lib/active_support/messages/rotator.rb
activesupport-6.0.2.2 lib/active_support/messages/rotator.rb
activesupport-5.2.4.2 lib/active_support/messages/rotator.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/messages/rotator.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/messages/rotator.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/messages/rotator.rb
activesupport-6.0.2.1 lib/active_support/messages/rotator.rb