system_keychain 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/system_keychain.rb +53 -0
- data/lib/system_keychain/input/color_console.rb +30 -0
- data/lib/system_keychain/store/mac.rb +26 -0
- metadata +77 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'system_keychain/store/mac'
|
2
|
+
require 'system_keychain/input/color_console'
|
3
|
+
|
4
|
+
module Keychain
|
5
|
+
@engine = SystemKeychain::Store::Mac
|
6
|
+
raise "No valid Keychain engines found (currently only Mac OSX is supported)" unless @engine.is_valid
|
7
|
+
|
8
|
+
@input = SystemKeychain::Input::ColorConsole
|
9
|
+
|
10
|
+
def self.get(url)
|
11
|
+
@engine.get(url)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.remove(url)
|
15
|
+
@engine.remove(url)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.save(url, user, pass, description = url)
|
19
|
+
@engine.save(url, user, pass, description)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.ask(url, description = nil)
|
23
|
+
@input.print_important("Enter credentials for #{description} #{url}")
|
24
|
+
@input.print("(These will be saved in your Mac OSX keychain)")
|
25
|
+
user = @input.ask("Username: ")
|
26
|
+
pass = @input.ask_secret("Password: ")
|
27
|
+
self.save(url, user, pass, description)
|
28
|
+
return [user, pass]
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.authorize(description, url, &block)
|
32
|
+
authorized = false
|
33
|
+
while !authorized
|
34
|
+
user, pass = self.get(url)
|
35
|
+
user, pass = self.ask(url, description) unless user
|
36
|
+
begin
|
37
|
+
value = yield user, pass
|
38
|
+
authorized = true
|
39
|
+
rescue StandardError => e
|
40
|
+
@input.print_error("Authorization failed: #{e.inspect}")
|
41
|
+
self.remove(url)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return value
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.authorize_url(description, url, &block)
|
48
|
+
self.authorize(description, url) do |user, pass|
|
49
|
+
auth_url = url.sub(/([^:]*:\/\/)/,"\\1#{user}:#{pass}@")
|
50
|
+
yield auth_url
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'highline'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module SystemKeychain
|
5
|
+
module Input
|
6
|
+
module ColorConsole
|
7
|
+
def self.print_important(s)
|
8
|
+
puts s.blue
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.print(s)
|
12
|
+
puts s
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.print_error(s)
|
16
|
+
puts s.red
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.ask(prompt)
|
20
|
+
@highline ||= HighLine.new
|
21
|
+
@highline.ask(prompt.blue)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.ask_secret(prompt)
|
25
|
+
@highline ||= HighLine.new
|
26
|
+
@highline.ask("Password: ".blue) { |q| q.echo = '*'*(1+rand(3)) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module SystemKeychain
|
4
|
+
module Store
|
5
|
+
module Mac
|
6
|
+
def self.is_valid
|
7
|
+
`which security` == "/usr/bin/security\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.get(url)
|
11
|
+
match = `security find-generic-password -s #{Shellwords.escape(url)} -g 2>&1`.match(/password: "([^"]*)".*acct"<blob>="([^"]*)"/m)
|
12
|
+
return nil unless match
|
13
|
+
pass, user = match.captures
|
14
|
+
return [user, pass]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.remove(url)
|
18
|
+
`security delete-generic-password -s #{Shellwords.escape(url)}`
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.save(url, user, pass, description = url)
|
22
|
+
`security add-generic-password -l #{Shellwords.escape(description)} -s #{Shellwords.escape(url)} -a #{Shellwords.escape(user)} -p #{Shellwords.escape(pass)}`
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: system_keychain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron VonderHaar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-08-28 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: colorize
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.5.8
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: highline
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.6.19
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Store account credentials in the OS keychain
|
38
|
+
email: gruen0aermel@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/system_keychain/input/color_console.rb
|
47
|
+
- lib/system_keychain/store/mac.rb
|
48
|
+
- lib/system_keychain.rb
|
49
|
+
homepage: http://rubygems.org/gems/system_keychain
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.11
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: System Keychain
|
76
|
+
test_files: []
|
77
|
+
|