Class: OSX::Keychain

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keychain_path, key_password) ⇒ Keychain

Returns a new instance of Keychain.



10
11
12
13
14
15
# File 'lib/keychain_osx/keychain.rb', line 10

def initialize(keychain_path, key_password)
    @keychain_path = keychain_path
    create(key_password)
    unlock(key_password)
    add_to_search_path
end

Instance Attribute Details

#keychain_pathObject

Returns the value of attribute keychain_path.



8
9
10
# File 'lib/keychain_osx/keychain.rb', line 8

def keychain_path
  @keychain_path
end

Class Method Details

.temp(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/keychain_osx/keychain.rb', line 50

def self.temp(&block)
    kc = OSX::Keychain.new(KEYCHAIN_LOCATION, TEMP_PASSWORD)
    kc.unlock(TEMP_PASSWORD)

    if block_given? # block is given
        begin
            yield(kc)
        ensure
            kc.delete
        end
    else 
        kc.delete
    end
end

Instance Method Details

#add_to_search_pathObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/keychain_osx/keychain.rb', line 37

def add_to_search_path
    keychains = []

    `security list-keychain`.split.map do |keychain| 
        keychains << keychain.strip.gsub(/\"/,'')
    end

    keychains << @keychain_path

    command = "security list-keychain -s #{keychains.join(' ')}"
    OSX::Command::run(command)
end

#deleteObject



27
28
29
30
# File 'lib/keychain_osx/keychain.rb', line 27

def delete
    command  = "security delete-keychain #{@keychain_path}"
    OSX::Command::run(command)
end

#import(cert, password) ⇒ Object



22
23
24
25
# File 'lib/keychain_osx/keychain.rb', line 22

def import(cert, password)
    command = "security import '#{cert}' -k \"#{@keychain_path}\" -P #{password} -T /usr/bin/codesign"
    OSX::Command::run(command)
end

#set_defaultObject



32
33
34
35
# File 'lib/keychain_osx/keychain.rb', line 32

def set_default
    command = "security default-keychain -s #{@keychain_path}"
    OSX::Command::run(command)
end

#unlock(password) ⇒ Object



17
18
19
20
# File 'lib/keychain_osx/keychain.rb', line 17

def unlock(password)
    command = "security unlock-keychain -p #{password} \"#{@keychain_path}\""
    OSX::Command::run(command)
end