Class: FunWith::Passwords::Keychain

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

Constant Summary collapse

NAMESPACE_CHAR =
":"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Keychain

Returns a new instance of Keychain.



15
16
17
18
19
20
# File 'lib/fun_with/passwords/keychain.rb', line 15

def initialize( opts = {} )
  @options = opts
  set_keys
  set_master_key
  set_file_store
end

Instance Attribute Details

#file_storeObject

Returns the value of attribute file_store.



6
7
8
# File 'lib/fun_with/passwords/keychain.rb', line 6

def file_store
  @file_store
end

Class Method Details

.load(master_key, opts = {}) ⇒ Object

Load an existing



9
10
11
12
13
# 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

Instance Method Details

#[](key) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



71
72
73
# File 'lib/fun_with/passwords/keychain.rb', line 71

def []=( key, password )
  @keys[key] = password
end

#delete(key) ⇒ Object



94
95
96
# File 'lib/fun_with/passwords/keychain.rb', line 94

def delete( key )
  @keys.delete( key )
end

#each(&block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/fun_with/passwords/keychain.rb', line 86

def each( &block )
  if block_given?
    @keys.each(&block)
  else
    @keys.each
  end
end

#printoutObject



98
99
100
101
102
# 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) ⇒ Object

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



23
24
25
26
27
28
29
30
# 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_storeObject



67
68
69
# 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) ⇒ Object

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



49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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_optionsObject



42
43
44
45
46
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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