Sha256: daee1ae439727805fd18878051089b1aa199ca87ca07739bce36a62d6ee314f6

Contents?: true

Size: 1.32 KB

Versions: 20

Compression:

Stored size: 1.32 KB

Contents

module Scrivito

module BinaryParamVerifier
  InvalidSignature = Class.new(StandardError)

  class << self
    def verify(encrypted_params)
      params = decrypt(encrypted_params)
      expires = DateConversion.deserialize_from_backend(params['expires'])
      raise InvalidSignature if expires && expires < Time.zone.now
      verify_without_expire(encrypted_params)
    end

    def verify_without_expire(encrypted_params)
      params = decrypt(encrypted_params)
      Binary.new(params['binary_id'], params['expires'].nil?,
          transformation_definition: params['transformation_definition'],
          obj_id: params['obj_id'])
    end

    def generate(binary)
      params = {
        binary_id: binary.id,
        obj_id: binary.obj_id,
        transformation_definition: binary.transformation_definition,
      }
      if binary.private?
        params[:expires] = DateConversion.serialize_for_backend(Time.zone.now + 1.hour)
      end
      message_verifier.generate(params)
    end

    private

    def decrypt(encrypted_params)
      message_verifier.verify(encrypted_params)
    rescue ActiveSupport::MessageVerifier::InvalidSignature
      raise InvalidSignature
    end

    def message_verifier
      ActiveSupport::MessageVerifier.new(
        Rails.application.secrets.secret_key_base, serializer: JSON)
    end
  end
end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
scrivito_sdk-1.9.1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.9.1.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.9.0 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.9.0.rc2 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.9.0.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.8.1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.8.1.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.8.0 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.8.0.rc2 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.8.0.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.7.0 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.7.0.rc2 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.1.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.7.0.rc1 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.0 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.0.rc5 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.0.rc4 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.0.rc3 lib/scrivito/binary_param_verifier.rb
scrivito_sdk-1.6.0.rc2 lib/scrivito/binary_param_verifier.rb