Class: Clandestine::Safe

Inherits:
Object
  • Object
show all
Defined in:
lib/clandestine/safe.rb

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ Safe

Returns a new instance of Safe.



12
13
14
15
16
17
18
19
20
21
# File 'lib/clandestine/safe.rb', line 12

def initialize(password)
  @password = password
  @location = ENV['CLANDESTINE_SAFE'] || SAFE_LOCATION
  @safe = PStore.new(location)
  if first_access(location)
    open do |s|
      safe[:safe_password] = Crypt.hash_password(IO.get_password(true))
    end
  end
end

Instance Method Details

#[](key) ⇒ Object Also known as: get



35
36
37
38
39
40
# File 'lib/clandestine/safe.rb', line 35

def [](key)
  return if !exists?(key)
  SafeAuthentication.authenticate(safe, password)
  value = safe[key]
  Crypt.decrypt(value, password)
end

#[]=(key, value) ⇒ Object Also known as: add



44
45
46
47
48
# File 'lib/clandestine/safe.rb', line 44

def []=(key, value)
  SafeAuthentication.authenticate(safe, password)
  safe[key] = Crypt.encrypt value, password
  true
end

#contentsObject



29
30
31
32
33
# File 'lib/clandestine/safe.rb', line 29

def contents
  contents = safe.roots
  contents.delete(:safe_password)
  contents
end

#delete(key) ⇒ Object



52
53
54
55
56
# File 'lib/clandestine/safe.rb', line 52

def delete(key)
  SafeAuthentication.authenticate(safe, password)
  safe.delete(key)
  true
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/clandestine/safe.rb', line 70

def exists?(key)
  SafeAuthentication.authenticate(safe, password)
  !safe[key].nil?
end

#openObject



23
24
25
26
27
# File 'lib/clandestine/safe.rb', line 23

def open
  safe.transaction do
    yield self
  end
end

#removeObject



64
65
66
67
68
# File 'lib/clandestine/safe.rb', line 64

def remove
  SafeAuthentication.authenticate(safe, password)
  File.delete location
  true
end

#update_safe_password(new_password) ⇒ Object



58
59
60
61
62
# File 'lib/clandestine/safe.rb', line 58

def update_safe_password(new_password)
  SafeAuthentication.authenticate(safe, password)
  safe[:safe_password] = Crypt.hash_password(new_password)
  true
end