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:



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

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.



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

def name
  @name
end

#pathObject

Returns the value of attribute path.



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

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:



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/xcode/keychain.rb', line 133

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



187
188
189
190
191
# File 'lib/xcode/keychain.rb', line 187

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/xcode/keychain.rb', line 164

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



151
152
153
154
155
156
# File 'lib/xcode/keychain.rb', line 151

def delete
  cmd = []
  cmd << "security"
  cmd << "delete-keychain \"#{@path}\""
  Xcode::Shell.execute(cmd)
end

#identitiesArray<String>

Returns a list of identities in the keychain.

Returns:



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

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



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

def import(cert, password)
  cmd = []
  cmd << "security"
  cmd << "import '#{cert}'"
  cmd << "-k \"#{@path}\""
  cmd << "-P #{password}"
  cmd << "-T /usr/bin/codesign"
  Xcode::Shell.execute(cmd)
end

#lockObject

Secure the keychain



104
105
106
107
108
109
110
# File 'lib/xcode/keychain.rb', line 104

def lock
  cmd = []
  cmd << "security"
  cmd << "lock-keychain"
  cmd << "\"#{@path}\""
  Xcode::Shell.execute(cmd)
end

#unlock(password) ⇒ Object

Unlock the keychain using the provided password

Parameters:

  • the (String)

    password to open the keychain



117
118
119
120
121
122
123
124
# File 'lib/xcode/keychain.rb', line 117

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