Sha256: 86d04934d3d12cbc1d2a2dc049812f42c47153608e1b20753576b1ef647f2791

Contents?: true

Size: 1.46 KB

Versions: 26

Compression:

Stored size: 1.46 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

26 entries across 26 versions & 3 rubygems

Version Path
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/messages/rotator.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/messages/rotator.rb
activesupport-6.0.6.1 lib/active_support/messages/rotator.rb
activesupport-6.0.6 lib/active_support/messages/rotator.rb
activesupport-6.0.5.1 lib/active_support/messages/rotator.rb
activesupport-6.0.5 lib/active_support/messages/rotator.rb
activesupport-6.0.4.8 lib/active_support/messages/rotator.rb
activesupport-6.0.4.7 lib/active_support/messages/rotator.rb
activesupport-6.0.4.6 lib/active_support/messages/rotator.rb
activesupport-6.0.4.5 lib/active_support/messages/rotator.rb
activesupport-6.0.4.4 lib/active_support/messages/rotator.rb
activesupport-6.0.4.3 lib/active_support/messages/rotator.rb
activesupport-6.0.4.2 lib/active_support/messages/rotator.rb
activesupport-6.0.4.1 lib/active_support/messages/rotator.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.4/lib/active_support/messages/rotator.rb
activesupport-6.0.4 lib/active_support/messages/rotator.rb
activesupport-6.0.3.7 lib/active_support/messages/rotator.rb
activesupport-6.0.3.6 lib/active_support/messages/rotator.rb
activesupport-6.0.3.5 lib/active_support/messages/rotator.rb
activesupport-6.0.3.4 lib/active_support/messages/rotator.rb