Class: Xcode::Keychain
- Inherits:
-
Object
- Object
- Xcode::Keychain
- Includes:
- TerminalOutput
- Defined in:
- lib/xcode/keychain.rb
Constant Summary collapse
- TEMP_PASSWORD =
"build_keychain_password"
Constants included from TerminalOutput
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.create(path, password) {|kc| ... } ⇒ Xcode::Keychain
Create a new keychain with the given name and password.
-
.login {|kc| ... } ⇒ Xcode::Keychain
Opens the default login.keychain for current user.
-
.temp ⇒ Object
Creates a keychain with the given name that lasts for the duration of the provided block.
Instance Method Summary collapse
-
#delete ⇒ Object
Remove the keychain from the filesystem.
-
#identities ⇒ Array<String>
Returns a list of identities in the keychain.
-
#import(cert, password) ⇒ Object
Import the .p12 certificate file into the keychain using the provided password.
-
#in_search_path(&block) ⇒ Object
Installs this keychain in the head of teh search path and restores the original on completion of the block.
-
#initialize(path) {|_self| ... } ⇒ Keychain
constructor
Open the keychain with the specified name.
-
#lock ⇒ Object
Secure the keychain.
- #to_s ⇒ Object
-
#unlock(password) ⇒ Object
Unlock the keychain using the provided password.
Methods included from TerminalOutput
#color_output=, #color_output?, #format_lhs, included, #log_level, log_level=, #print, #print_input, #print_output, #print_system, #print_task, #puts, terminal_supports_colors?
Constructor Details
#initialize(path) {|_self| ... } ⇒ Keychain
Open the keychain with the specified name. It is assumed that keychains reside in the ~/Library/Keychains directory
54 55 56 57 58 59 |
# File 'lib/xcode/keychain.rb', line 54 def initialize(path) @path = File. path @name = File.basename path yield(self) if block_given? end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
44 45 46 |
# File 'lib/xcode/keychain.rb', line 44 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
44 45 46 |
# File 'lib/xcode/keychain.rb', line 44 def path @path end |
Class Method Details
.create(path, password) {|kc| ... } ⇒ Xcode::Keychain
Create a new keychain with the given name and password
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/xcode/keychain.rb', line 145 def self.create(path, password) cmd = Xcode::Shell::Command.new "security" cmd << "create-keychain" cmd << "-p #{password}" cmd << "\"#{path}\"" cmd.execute cmd = Xcode::Shell::Command.new "security" cmd << "set-keychain-settings" cmd << "-u" cmd << "\"#{path}\"" cmd.execute kc = Xcode::Keychain.new(path) yield(kc) if block_given? kc end |
.login {|kc| ... } ⇒ Xcode::Keychain
Opens the default login.keychain for current user
203 204 205 206 207 |
# File 'lib/xcode/keychain.rb', line 203 def self.login kc = Xcode::Keychain.new("~/Library/Keychains/login.keychain") yield(kc) if block_given? kc end |
.temp ⇒ Object
Creates a keychain with the given name that lasts for the duration of the provided block.
The keychain is deleted even if the block throws an exception.
If no block is provided, the temporary keychain is returned and it is deleted on system exit
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/xcode/keychain.rb', line 180 def self.temp kc = Xcode::Keychain.create("/tmp/xcoder#{Time.now.to_i}", TEMP_PASSWORD) kc.unlock(TEMP_PASSWORD) if !block_given? at_exit do kc.delete end kc else begin yield(kc) ensure kc.delete end end end |
Instance Method Details
#delete ⇒ Object
Remove the keychain from the filesystem
FIXME: dangerous
168 169 170 171 172 |
# File 'lib/xcode/keychain.rb', line 168 def delete cmd = Xcode::Shell::Command.new "security" cmd << "delete-keychain \"#{@path}\"" cmd.execute end |
#identities ⇒ Array<String>
Returns a list of identities in the keychain.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/xcode/keychain.rb', line 102 def identities names = [] cmd = Xcode::Shell::Command.new "security" cmd << "find-certificate" cmd << "-a" cmd << "\"#{@path}\"" cmd.show_output = false cmd.execute.join("").scan /\s+"labl"<blob>="([^"]+)"/ do |m| names << m[0] end names end |
#import(cert, password) ⇒ Object
Import the .p12 certificate file into the keychain using the provided password
88 89 90 91 92 93 94 95 |
# File 'lib/xcode/keychain.rb', line 88 def import(cert, password) cmd = Xcode::Shell::Command.new "security" cmd << "import '#{cert}'" cmd << "-k \"#{@path}\"" cmd << "-P #{password}" cmd << "-T /usr/bin/codesign" cmd.execute end |
#in_search_path(&block) ⇒ Object
Installs this keychain in the head of teh search path and restores the original on completion of the block
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/xcode/keychain.rb', line 71 def in_search_path(&block) keychains = Keychains.search_path begin Keychains.search_path = [self] + keychains yield ensure Keychains.search_path = keychains # print_task 'keychain', "Restored search path" end end |
#lock ⇒ Object
Secure the keychain
118 119 120 121 122 123 |
# File 'lib/xcode/keychain.rb', line 118 def lock cmd = Xcode::Shell::Command.new "security" cmd << "lock-keychain" cmd << "\"#{@path}\"" cmd.execute end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/xcode/keychain.rb', line 61 def to_s "Keychain(#{@name})" end |
#unlock(password) ⇒ Object
Unlock the keychain using the provided password
130 131 132 133 134 135 136 |
# File 'lib/xcode/keychain.rb', line 130 def unlock(password) cmd = Xcode::Shell::Command.new "security" cmd << "unlock-keychain" cmd << "-p #{password}" cmd << "\"#{@path}\"" cmd.execute end |