Sha256: 1695dc0981a9418f59ecc2924e075a2c4a01a1b78f71df74447c58a12b5a3933

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true, encoding: ASCII-8BIT

module CouchbaseOrm
    module Encrypt
        def encode_encrypted_attributes
            attributes.map do |key, value|
                type = self.class.attribute_types[key.to_s]
                if type.is_a?(CouchbaseOrm::Types::Encrypted)
                    next unless value
                    raise "Can not serialize value #{value} of type '#{value.class}' for Tanker encrypted attribute" unless value.is_a?(String)
                    ["encrypted$#{key}", {
                        alg: type.alg,
                        ciphertext: value
                    }]
                else
                    [key,value]
                end
            end.compact.to_h
        end

        def decode_encrypted_attributes(attributes)
            attributes.map do |key, value|
                key = key.to_s
                if key.start_with?('encrypted$')
                    key = key.gsub('encrypted$', '')
                    value = value.with_indifferent_access[:ciphertext]
                end
                [key, value]
            end.to_h
        end


        def to_json(*args, **kwargs)
            as_json.to_json(*args, **kwargs)
        end

        def as_json(*args, **kwargs)
            super(*args, **kwargs).map do |key, value|
                type = self.class.attribute_types[key.to_s]
                if type.is_a?(CouchbaseOrm::Types::Encrypted) && value
                    raise "Can not serialize value #{value} of type '#{value.class}' for encrypted attribute" unless value.is_a?(String)
                end
                [key, value]
            end.to_h.with_indifferent_access
        end

    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
couchbase-orm-2.0.3 lib/couchbase-orm/encrypt.rb
couchbase-orm-2.0.2 lib/couchbase-orm/encrypt.rb
couchbase-orm-2.0.1 lib/couchbase-orm/encrypt.rb
couchbase-orm-2.0.0 lib/couchbase-orm/encrypt.rb