Class: Keychain::Keychain

Inherits:
Sec::Base
  • Object
show all
Defined in:
lib/keychain/keychain.rb

Overview

Wrapper class for individual keychains. Corresponds to a SecKeychainRef

Instance Attribute Summary

Attributes inherited from Sec::Base

#attributes

Instance Method Summary collapse

Methods inherited from Sec::Base

define_attributes, #initialize, #keychain, #load_attributes, register_type, #update_self_from_dictionary

Constructor Details

This class inherits a constructor from Sec::Base

Instance Method Details

#add_to_search_listObject

Add the keychain to the default searchlist



49
50
51
52
53
54
55
56
57
58
# File 'lib/keychain/keychain.rb', line 49

def add_to_search_list
  list = FFI::MemoryPointer.new(:pointer)
  status = Sec.SecKeychainCopySearchList(list)
  Sec.check_osstatus(status)
  ruby_list = CF::Base.typecast(list.read_pointer).to_ruby
  ruby_list << self unless ruby_list.include?(self)
  status = Sec.SecKeychainSetSearchList(CF::Array.immutable(ruby_list))
  Sec.check_osstatus(status)
  self
end

#deleteObject

Removes the keychain from the search path and deletes the corresponding file (SecKeychainDelete)

See developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html#//apple_ref/c/func/SecKeychainDelete

Returns:

  • self



115
116
117
118
119
# File 'lib/keychain/keychain.rb', line 115

def delete
  status = Sec.SecKeychainDelete(self)
  Sec.check_osstatus(status)
  self
end

#exists?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
181
182
# File 'lib/keychain/keychain.rb', line 175

def exists?
  begin
    readable?
    true
  rescue NoSuchKeychainError
    false
  end
end

#generic_passwordsKeychain::Scope

Returns a scope for generic passwords contained in this keychain

Returns:



101
102
103
# File 'lib/keychain/keychain.rb', line 101

def generic_passwords
  Scope.new(Sec::Classes::GENERIC, self)
end

#inspectString

returns a description of the keychain

Returns:

  • (String)


107
108
109
# File 'lib/keychain/keychain.rb', line 107

def inspect
  "<SecKeychain 0x#{@ptr.address.to_s(16)}: #{path}>"
end

#internet_passwordsKeychain::Scope

Returns a scope for internet passwords contained in this keychain

Returns:



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

def internet_passwords
  Scope.new(Sec::Classes::INTERNET, self)
end

#lock!Object

Locks the keychain



139
140
141
142
# File 'lib/keychain/keychain.rb', line 139

def lock!
  status = Sec.SecKeychainLock(self)
  Sec.check_osstatus status
end

#lock_intervalBoolean

Returns the duration (in seconds) after which the keychain will be locked

Returns:

  • (Boolean)


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

def lock_interval
  get_settings[:lock_interval]
end

#lock_interval=(value) ⇒ Object

Sets the duration (in seconds) after which the keychain will be locked

Parameters:

  • value (Integer)

    dutarion in seconds



87
88
89
# File 'lib/keychain/keychain.rb', line 87

def lock_interval= value
  put_settings(get_settings.tap {|s| s[:lock_interval] = value})
end

#lock_on_sleep=(value) ⇒ Object

Set whether the keychain will be locked if the machine goes to sleep

Parameters:

  • value (Boolean)


79
80
81
# File 'lib/keychain/keychain.rb', line 79

def lock_on_sleep= value
  put_settings(get_settings.tap {|s| s[:lock_on_sleep] = value ? 1 : 0})
end

#lock_on_sleep?Boolean

Returns whether the keychain will be locked if the machine goes to sleep

Returns:

  • (Boolean)


63
64
65
# File 'lib/keychain/keychain.rb', line 63

def lock_on_sleep?
  get_settings[:lock_on_sleep] != 0
end

#locked?Boolean

Returns whether the keychain is locked

Returns:

  • (Boolean)


159
160
161
# File 'lib/keychain/keychain.rb', line 159

def locked?
  !status_flag?(:kSecUnlockStateStatus)
end

#pathString

Returns:

  • (String)

    path to the keychain file



126
127
128
129
130
131
132
133
134
135
# File 'lib/keychain/keychain.rb', line 126

def path
  out_buffer = FFI::MemoryPointer.new(:uchar, 2048)
  io_size = FFI::MemoryPointer.new(:uint32)
  io_size.put_uint32(0, out_buffer.size)

  status = Sec.SecKeychainGetPath(self,io_size, out_buffer)
  Sec.check_osstatus(status)

  out_buffer.read_string(io_size.get_uint32(0)).force_encoding(Encoding::UTF_8)
end

#readable?Boolean

Returns whether the keychain is readable

Returns:

  • (Boolean)


165
166
167
# File 'lib/keychain/keychain.rb', line 165

def readable?
  status_flag?(:kSecReadPermStatus)
end

#unlock!(password = nil) ⇒ Object

Unlocks the keychain

Parameters:

  • password (optional, String) (defaults to: nil)

    the password to unlock the keychain with. If no password is supplied the keychain will prompt the user for a password



147
148
149
150
151
152
153
154
155
# File 'lib/keychain/keychain.rb', line 147

def unlock! password=nil
  if password
    password = password.encode(Encoding::UTF_8)
    status = Sec.SecKeychainUnlock self, password.bytesize, password, 1
  else
    status = Sec.SecKeychainUnlock self, 0, nil, 0
  end
  Sec.check_osstatus status
end

#writeable?Boolean

Returns whether the keychain is writable

Returns:

  • (Boolean)


171
172
173
# File 'lib/keychain/keychain.rb', line 171

def writeable?
  status_flag?(:kSecWritePermStatus)
end