Class: FunWith::Passwords::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_with/passwords/file_store.rb

Overview

The FileStore knows only where to save itself, and how to encrypt and decrypt the file. The contents of a file are a mystery for Keychain to deal with

Constant Summary collapse

DEFAULT_DIR =
XDG['CONFIG'].fwf_filepath.join("fun_with_passwords")
DEFAULT_EXT =
"aes256.dat"
DEFAULT_FILENAME =
"password_store"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ FileStore

if the key is nil, waits until a password is requested or added to decrypt password file



19
20
21
22
# File 'lib/fun_with/passwords/file_store.rb', line 19

def initialize( filename = nil )
  @password_file = expand_filepath( filename )
  @password_file.touch
end

Instance Attribute Details

#key_chainObject

Returns the value of attribute key_chain.



12
13
14
# File 'lib/fun_with/passwords/file_store.rb', line 12

def key_chain
  @key_chain
end

#password_fileObject

Returns the value of attribute password_file.



12
13
14
# File 'lib/fun_with/passwords/file_store.rb', line 12

def password_file
  @password_file
end

Class Method Details

.default_fileObject



14
15
16
# File 'lib/fun_with/passwords/file_store.rb', line 14

def self.default_file
  DEFAULT_DIR.join( "#{DEFAULT_FILENAME}.#{DEFAULT_EXT}" )
end

Instance Method Details

#save(yaml, master_key) ⇒ Object

def initialize_key_chain_if_needed( keychain_keys, master_key )

unless @key_chain
  @key_chain = Keychain.new( :keys => keychain_keys, :master_key => master_key )
  @key_chain.file_store = self
end

end



40
41
42
43
44
45
46
# File 'lib/fun_with/passwords/file_store.rb', line 40

def save( yaml, master_key )
  encrypted_message = Crypt.encrypt( yaml, master_key )
  @password_file.write( encrypted_message )
  true
rescue Exception => e
  false
end

#unlock(master_key) ⇒ Object

sends back a Keychain that knows where its store is



25
26
27
28
29
30
31
# File 'lib/fun_with/passwords/file_store.rb', line 25

def unlock( master_key )
  if @password_file.file? && !@password_file.empty?
    YAML.load( Crypt.decrypt( @password_file.read, master_key ) )
  else
    {}
  end
end