Class: Xcode::Keychain

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

Constant Summary collapse

TEMP_PASSWORD =
"build_keychain_password"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) {|_self| ... } ⇒ Keychain

Open the keychain with the specified name. It is assumed that keychains reside in the ~/Library/Keychains directory

Parameters:

  • the (String)

    name of the keychain

Yields:

  • (_self)

Yield Parameters:



58
59
60
61
62
63
# File 'lib/xcode/keychain.rb', line 58

def initialize(path)
  @path = File.expand_path path
  @name = File.basename path
  
  yield(self) if block_given?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



48
49
50
# File 'lib/xcode/keychain.rb', line 48

def name
  @name
end

#pathObject

Returns the value of attribute path.



48
49
50
# File 'lib/xcode/keychain.rb', line 48

def path
  @path
end

Class Method Details

.create(path, password) {|kc| ... } ⇒ Xcode::Keychain

Create a new keychain with the given name and password

Parameters:

  • the (String)

    name for the new keychain

  • the (String)

    password for the new keychain

Yields:

  • (kc)

Returns:



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xcode/keychain.rb', line 128

def self.create(path, password)
  cmd = Xcode::Shell::Command.new "security"
  cmd << "create-keychain"
  cmd << "-p #{password}"
  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

Yields:

  • (kc)

Returns:



180
181
182
183
184
# File 'lib/xcode/keychain.rb', line 180

def self.
  kc = Xcode::Keychain.new("~/Library/Keychains/login.keychain")
  yield(kc) if block_given?
  kc
end

.tempObject

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/xcode/keychain.rb', line 157

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

#deleteObject

Remove the keychain from the filesystem

FIXME: dangerous



145
146
147
148
149
# File 'lib/xcode/keychain.rb', line 145

def delete
  cmd = Xcode::Shell::Command.new "security"
  cmd << "delete-keychain \"#{@path}\""
  cmd.execute
end

#identitiesArray<String>

Returns a list of identities in the keychain.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/xcode/keychain.rb', line 85

def identities
  names = []
  cmd = Xcode::Shell::Command.new "security"
  cmd << "find-certificate"
  cmd << "-a"
  cmd << "\"#{@path}\""
  data = cmd.execute(false).join("")
  data.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

Parameters:

  • the (String)

    path to the .p12 certificate file

  • the (String)

    password to open the certificate file



71
72
73
74
75
76
77
78
# File 'lib/xcode/keychain.rb', line 71

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

#lockObject

Secure the keychain



101
102
103
104
105
106
# File 'lib/xcode/keychain.rb', line 101

def lock
  cmd = Xcode::Shell::Command.new "security"
  cmd << "lock-keychain"
  cmd << "\"#{@path}\""
  cmd.execute
end

#unlock(password) ⇒ Object

Unlock the keychain using the provided password

Parameters:

  • the (String)

    password to open the keychain



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

def unlock(password)
  cmd = Xcode::Shell::Command.new "security"
  cmd << "unlock-keychain"
  cmd << "-p #{password}"
  cmd << "\"#{@path}\""
  cmd.execute
end