mac-keychain 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +19 -0
- data/Manifest +7 -0
- data/README.rdoc +33 -0
- data/Rakefile +22 -0
- data/ext/bridgesupport/rakefile +5 -0
- data/lib/mac_keychain.rb +81 -0
- data/mac-keychain.gemspec +38 -0
- data/test/test_mac_keychain.rb +37 -0
- metadata +89 -0
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Li Xiao, iam@li-xiao.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= Mac-Keychain -- Ruby interface to Mac OSX Keychain
|
2
|
+
|
3
|
+
== SYNOPSIS
|
4
|
+
|
5
|
+
@keychain = MacKeychain.new('AppName')
|
6
|
+
@keychain.save('account name', 'password')
|
7
|
+
....
|
8
|
+
@keychain.delete('account name')
|
9
|
+
|
10
|
+
== Install
|
11
|
+
|
12
|
+
gem install mac-keychain
|
13
|
+
|
14
|
+
== Limitation
|
15
|
+
|
16
|
+
It only works on Mac, and need RubyCocoa.
|
17
|
+
|
18
|
+
= Other stuff
|
19
|
+
|
20
|
+
Author: Li Xiao <iam@li-xiao.com>
|
21
|
+
|
22
|
+
Requires: Ruby 1.8.6 or later
|
23
|
+
|
24
|
+
License: Copyright 2010 by Li Xiao.
|
25
|
+
Released under an MIT-LICENSE. See the MIT-LICENSE.txt file
|
26
|
+
included in the distribution.
|
27
|
+
|
28
|
+
== Warranty
|
29
|
+
|
30
|
+
This software is provided "as is" and without any express or
|
31
|
+
implied warranties, including, without limitation, the implied
|
32
|
+
warranties of merchantibility and fitness for a particular
|
33
|
+
purpose.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('mac-keychain', '0.0.1') do |p|
|
6
|
+
p.description = "Mac-Keychain -- Ruby interface to Mac OSX Keychain"
|
7
|
+
p.url = "https://github.com/xli/mac-keychain"
|
8
|
+
p.author = "Li Xiao"
|
9
|
+
p.email = "iam@li-xiao.com"
|
10
|
+
p.ignore_pattern = ["*.gemspec", 'lib/Security.bridgesupport']
|
11
|
+
p.development_dependencies = ['rake', 'echoe']
|
12
|
+
p.test_pattern = "test/test_*.rb"
|
13
|
+
p.rdoc_options = %w(--main README.rdoc --inline-source --line-numbers --charset UTF-8)
|
14
|
+
p.extension_pattern = "ext/**/rakefile"
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :generate do
|
18
|
+
task :bridge do
|
19
|
+
puts "generating lib/Security.bridgesupport"
|
20
|
+
puts %x[gen_bridge_metadata -f Security -o lib/Security.bridgesupport]
|
21
|
+
end
|
22
|
+
end
|
data/lib/mac_keychain.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'osx/cocoa'
|
2
|
+
OSX.require_framework 'Security'
|
3
|
+
OSX.load_bridge_support_file(File.join(File.dirname(__FILE__), 'Security.bridgesupport'))
|
4
|
+
|
5
|
+
class MacKeychain
|
6
|
+
include OSX
|
7
|
+
|
8
|
+
# A Key is an existing item in Keychain, contains item reference and password
|
9
|
+
class Key
|
10
|
+
def self.create(data)
|
11
|
+
password_length = data.shift
|
12
|
+
password_data = data.shift #password data
|
13
|
+
item = data.shift #SecKeychainItemRef
|
14
|
+
password = if password_data
|
15
|
+
password_data.bytestr(password_length)
|
16
|
+
end
|
17
|
+
self.new(item, password)
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :item, :password
|
21
|
+
|
22
|
+
# :item => Keychain item
|
23
|
+
# :password => password of item
|
24
|
+
def initialize(item, password)
|
25
|
+
@item = item
|
26
|
+
@password = password
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# :service => Name of keychain item, Application name or service name
|
31
|
+
def initialize(service)
|
32
|
+
@service = service
|
33
|
+
end
|
34
|
+
|
35
|
+
# Find Key by username (Account of Keychain item)
|
36
|
+
def find(username)
|
37
|
+
status, *data = SecKeychainFindGenericPassword(nil, @service.length, @service, username.length, username)
|
38
|
+
case status
|
39
|
+
when ErrSecItemNotFound
|
40
|
+
nil
|
41
|
+
when 0
|
42
|
+
Key.create(data)
|
43
|
+
else
|
44
|
+
raise "Error when accessing Keychain looking for key: #{username} of #{@service}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Create Keychain item with username (Account of Keychain item) and password
|
49
|
+
# Returns creation status code, please see Keychain Services Result Codes.
|
50
|
+
def create(username, password)
|
51
|
+
SecKeychainAddGenericPassword(nil, @service.length, @service, username.length, username, password.length, password, nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Delete Keychain item by username (Account of Keychain item)
|
55
|
+
# Raises error when delete failed
|
56
|
+
def delete(username)
|
57
|
+
if key = find(username)
|
58
|
+
unless SecKeychainItemDelete(key.item) == 0
|
59
|
+
raise "Could not delete Keychain item by #{username} of #{@service}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Update Keychain item by username with new password
|
65
|
+
# Returns update status code, please see Keychain Services Result Codes.
|
66
|
+
def update(username, password)
|
67
|
+
key = self.find(username)
|
68
|
+
SecKeychainItemModifyContent(key.item, nil, password.length, password)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Create Keychain item if it does not exist, otherwise update it.
|
72
|
+
# Returns creation/update status code, please see Keychain Services Result Codes.
|
73
|
+
def save(username, password)
|
74
|
+
code = create(username, password)
|
75
|
+
if ErrSecDuplicateItem == code
|
76
|
+
update(username, password)
|
77
|
+
else
|
78
|
+
code
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{mac-keychain}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Li Xiao"]
|
9
|
+
s.date = %q{2010-06-15}
|
10
|
+
s.description = %q{Mac-Keychain -- Ruby interface to Mac OSX Keychain}
|
11
|
+
s.email = %q{iam@li-xiao.com}
|
12
|
+
s.extensions = ["ext/bridgesupport/rakefile"]
|
13
|
+
s.extra_rdoc_files = ["README.rdoc", "ext/bridgesupport/rakefile", "lib/mac_keychain.rb"]
|
14
|
+
s.files = ["MIT-LICENSE.txt", "Manifest", "README.rdoc", "Rakefile", "ext/bridgesupport/rakefile", "lib/mac_keychain.rb", "test/test_mac_keychain.rb", "mac-keychain.gemspec"]
|
15
|
+
s.homepage = %q{https://github.com/xli/mac-keychain}
|
16
|
+
s.rdoc_options = ["--main", "README.rdoc", "--inline-source", "--line-numbers", "--charset", "UTF-8"]
|
17
|
+
s.require_paths = ["lib", "ext"]
|
18
|
+
s.rubyforge_project = %q{mac-keychain}
|
19
|
+
s.rubygems_version = %q{1.3.5}
|
20
|
+
s.summary = %q{Mac-Keychain -- Ruby interface to Mac OSX Keychain}
|
21
|
+
s.test_files = ["test/test_mac_keychain.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 3
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
29
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
32
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
36
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require "mac_keychain"
|
4
|
+
|
5
|
+
class TestMacKeychain < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@keychain = MacKeychain.new('TestKeychain')
|
8
|
+
@username = 'username'
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
@keychain.delete(@username)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_delete
|
16
|
+
@keychain.create(@username, 'password')
|
17
|
+
@keychain.delete(@username)
|
18
|
+
assert_nil @keychain.find(@username)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_store
|
22
|
+
@keychain.create(@username, 'password')
|
23
|
+
assert_equal 'password', @keychain.find(@username).password
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_find
|
27
|
+
assert_nil @keychain.find(@username)
|
28
|
+
@keychain.create(@username, 'password')
|
29
|
+
assert @keychain.find(@username)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_update_password_when_save_new_password
|
33
|
+
@keychain.save(@username, 'password')
|
34
|
+
@keychain.save(@username, 'new password')
|
35
|
+
assert_equal 'new password', @keychain.find(@username).password
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mac-keychain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Li Xiao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-06-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: echoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Mac-Keychain -- Ruby interface to Mac OSX Keychain
|
36
|
+
email: iam@li-xiao.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions:
|
40
|
+
- ext/bridgesupport/rakefile
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.rdoc
|
43
|
+
- ext/bridgesupport/rakefile
|
44
|
+
- lib/mac_keychain.rb
|
45
|
+
files:
|
46
|
+
- MIT-LICENSE.txt
|
47
|
+
- Manifest
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- ext/bridgesupport/rakefile
|
51
|
+
- lib/mac_keychain.rb
|
52
|
+
- test/test_mac_keychain.rb
|
53
|
+
- mac-keychain.gemspec
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: https://github.com/xli/mac-keychain
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.rdoc
|
62
|
+
- --inline-source
|
63
|
+
- --line-numbers
|
64
|
+
- --charset
|
65
|
+
- UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
- ext
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "1.2"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: mac-keychain
|
84
|
+
rubygems_version: 1.3.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Mac-Keychain -- Ruby interface to Mac OSX Keychain
|
88
|
+
test_files:
|
89
|
+
- test/test_mac_keychain.rb
|