Class: Exportation::Keychain

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Keychain

Returns a new instance of Keychain.



122
123
124
125
# File 'lib/exportation.rb', line 122

def initialize(options)
  @path = options[:path]
  @password = options[:password]
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



120
121
122
# File 'lib/exportation.rb', line 120

def password
  @password
end

#pathObject

Returns the value of attribute path.



120
121
122
# File 'lib/exportation.rb', line 120

def path
  @path
end

Class Method Details

.find_or_create_keychain(name, password, output_directory = './') ⇒ Object



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

def self.find_or_create_keychain(name, password, output_directory='./')
  path = chain_path(name, output_directory)

  unless File.exists? path
    `security create-keychain -p '#{password}' #{path}`
  end

  Keychain.new(path: path, password: password)
end

.list_keychainsObject



142
143
144
145
146
# File 'lib/exportation.rb', line 142

def self.list_keychains
  # Gets a list of all the user's keychains in an array
  # The keychain are paths wrapped in double quotes
  (`security list-keychains -d user`).scan(/(?:\w|"[^"]*")+/)
end

.login_keychain(password) ⇒ Object



137
138
139
140
# File 'lib/exportation.rb', line 137

def self.(password)
  path = `security login-keychain`.strip
  Keychain.new(path: path, password: password)
end

Instance Method Details

#add_to_keychain_list!Object



164
165
166
167
168
# File 'lib/exportation.rb', line 164

def add_to_keychain_list!
  # Adds the keychain to the search list
  keychains = (Keychain.list_keychains - ["\"#{@path}\""]).join(' ')
  `security list-keychains -d user -s #{keychains} \"#{@path}\"`
end

#import_certificate(cer_path) ⇒ Object



148
149
150
151
# File 'lib/exportation.rb', line 148

def import_certificate(cer_path)
  # Imports a certificate into the keychain
  `security import #{cer_path} -k #{@path} -T /usr/bin/codesign`
end

#import_private_key(key_path, password) ⇒ Object



153
154
155
156
# File 'lib/exportation.rb', line 153

def import_private_key(key_path, password)
  # Imports a private key into the keychain
  `security import #{key_path} -k #{@path} -P '#{password}' -T /usr/bin/codesign`
end

#remove_keychain_from_list!Object



170
171
172
173
174
# File 'lib/exportation.rb', line 170

def remove_keychain_from_list!
  # Removes the keychain from the search list
  keychains = (Keychain.list_keychains - ["\"#{@path}\""]).join(' ')
  `security list-keychains -d user -s #{keychains}`
end

#unlock!(seconds = 3600) ⇒ Object



158
159
160
161
162
# File 'lib/exportation.rb', line 158

def unlock!(seconds=3600)
  # Unlocks the keychain
  `security unlock-keychain -p '#{@password}' #{@path}`
  `security -v set-keychain-settings -t #{seconds} -l #{@path}`
end