class FunWith::Passwords::Keychain

Constants

NAMESPACE_CHAR

Attributes

file_store[RW]

Public Class Methods

load( master_key, opts = {} ) click to toggle source

Load an existing

# File lib/fun_with/passwords/keychain.rb, line 9
def self.load( master_key, opts = {} )
  file = opts[:file] || FileStore.default_file
  master_key = Console.new.ask_for_master_key if master_key == :new
  self.new( :file => file, :master_key => master_key ).unlock
end
new( opts = {} ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 15
def initialize( opts = {} )
  @options = opts
  set_keys
  set_master_key
  set_file_store
end

Public Instance Methods

[]( key ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 75
def []( key )
  password = @keys[key]
          
  if !password && @ask_on_fail
    password = Console.new.ask_for_password( key )
    self[key] = password  
  end
  
  password
end
[]=( key, password ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 71
def []=( key, password )
  @keys[key] = password
end
delete( key ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 94
def delete( key )
  @keys.delete( key )
end
each( &block ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 86
def each( &block )
  if block_given?
    @keys.each(&block)
  else
    @keys.each
  end
end
printout() click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 98
def printout
  self.each do |key, password|
    puts "#{key}: #{password}"
  end
end
save( master_key = nil) click to toggle source

if no new master key is given, save with the old one.

# File lib/fun_with/passwords/keychain.rb, line 23
def save( master_key = nil)
  @master_key = master_key if master_key
  if @file_store
    @file_store.save( @keys.to_yaml, @master_key )
  else
    false
  end
end
set_file_store() click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 67
def set_file_store
  @file_store = FileStore.new( @options[:file] ) if @options[:file]
end
set_keys( hash = nil ) click to toggle source

After @keys is set, subsequent set_keys(nil) calls have no effect.

# File lib/fun_with/passwords/keychain.rb, line 49
def set_keys( hash = nil )
  if hash
    @keys = hash
  else
    @keys ||= @options[:keys] || {}
    @options.delete(:keys)
  end
end
set_master_key( key = nil ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 58
def set_master_key( key = nil )
  if key
    @master_key = key
  else
    @master_key ||= @options.delete(:master_key)
    @master_key ||= Console.new.ask_for_master_key if @ask_on_fail
  end
end
set_options() click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 42
def set_options
  if @opts[:interactive]
    @ask_on_fail = true
  end
end
unlock( master_key = nil ) click to toggle source
# File lib/fun_with/passwords/keychain.rb, line 32
def unlock( master_key = nil )
  @master_key = master_key if master_key
  
  if @file_store
    set_keys( @file_store.unlock( @master_key ) )
  end
  
  self
end