Module: Rahasia::Model

Defined in:
lib/rahasia/model.rb

Overview

Instance Method Summary collapse

Instance Method Details

#default_stringObject



20
21
22
# File 'lib/rahasia/model.rb', line 20

def default_string
  "--encrypted:#{SecureRandom.hex(16)}---"
end

#define_attributes(attributes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

refresh_token token



63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object


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



52
53
54
55
56
57
58
59
# 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) ⇒ Object

refresh_token=(string)

# refresh_token_encrypted


77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# 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) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/rahasia/model.rb', line 90

def not_encrypted?(message)
  return false if message.nil?

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