module Rahasia::Model

Model for ActiveRecord extension github.com/ankane/lockbox/blob/80adc597b27172b5e420471db5888b8f962ad829/lib/lockbox/model.rb

Public Instance Methods

default_string() click to toggle source
# File lib/rahasia/model.rb, line 20
def default_string
  "--encrypted:#{SecureRandom.hex(16)}---"
end
define_attributes(attributes) click to toggle source
# File lib/rahasia/model.rb, line 24
def define_attributes(attributes)
  attributes.each do |name|
    encrypted_attribute = "#{name}_encrypted"
    decrypt_method_name = "decrypt_#{encrypted_attribute}"

    define_decrypt_method_name(decrypt_method_name)
    define_decrypt(name, encrypted_attribute, decrypt_method_name)
    define_setter(name)
  end
end
define_decrypt(name, encrypted_attribute, decrypt_method_name) click to toggle source

refresh_token token

Calls superclass method
# File lib/rahasia/model.rb, line 63
def define_decrypt(name, encrypted_attribute, decrypt_method_name)
  define_method(name) do
    message = super()
    if self.class.not_encrypted?(message)
      ciphertext = send(encrypted_attribute)
      message =
        self.class.send(decrypt_method_name, ciphertext, context: self)
    end
    message
  end
end
define_decrypt_method_name(decrypt_method_name) click to toggle source

available column refresh_token, name


Credential < ActiveRecord

enrcypt_column :refresh_token

end

available method
 # decrypt_refresh_token_encrypted
 # refresh_token_chipertext?
 # refresh_token
 # refresh_token=(string)
 # refresh_token_encrypted

refresh_token_encrypted token_encrypted

# File lib/rahasia/model.rb, line 52
def define_decrypt_method_name(decrypt_method_name)
  define_singleton_method decrypt_method_name do |encrypted, **_opts|
    Rahasia.encryptor.decrypt(
      key: Rahasia.rahasia_key,
      value: encrypted
    )
  end
end
define_setter(name) click to toggle source

refresh_token=(string)

# refresh_token_encrypted
Calls superclass method
# File lib/rahasia/model.rb, line 77
def define_setter(name)
  define_method("#{name}=") do |val|
    begin
      val = val.presence || ''
      str = Rahasia.encryptor.encrypt(key: Rahasia.rahasia_key, value: val)
      send("#{name}_encrypted=", str)
      super(self.class.default_string)
    rescue ArgumentError => e
      puts e
    end
  end
end
enrcypt_column(*attributes, **options) click to toggle source
# File lib/rahasia/model.rb, line 7
def enrcypt_column(*attributes, **options)
  unless [nil, :string].include?(options[:type])
    raise ArgumentError, "UnknownType #{options[:type]}."
  end

  activerecord = defined?(ActiveRecord::Base) && self < ActiveRecord::Base
  if options[:type] && !activerecord
    raise ArgumentError, 'Type not supported yet with Mongoid'
  end

  define_attributes(attributes)
end
not_encrypted?(message) click to toggle source
# File lib/rahasia/model.rb, line 90
def not_encrypted?(message)
  return false if message.nil?

  message.match(/--encrypted:/) ? true : false
end