Class: Awsborn::Keychain
- Inherits:
-
Object
- Object
- Awsborn::Keychain
- Defined in:
- lib/awsborn/keychain.rb
Instance Method Summary collapse
- #decode_hex(hex_dump) ⇒ Object
- #find_generic_password(name) ⇒ Object
- #get(name) ⇒ Object
-
#initialize(path = nil) ⇒ Keychain
constructor
A new instance of Keychain.
- #lock ⇒ Object
- #master_password ⇒ Object
- #multi_encoded?(note) ⇒ Boolean
- #string_content(note) ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Keychain
Returns a new instance of Keychain.
3 4 5 |
# File 'lib/awsborn/keychain.rb', line 3 def initialize (path = nil) @keychain = path end |
Instance Method Details
#decode_hex(hex_dump) ⇒ Object
39 40 41 42 43 |
# File 'lib/awsborn/keychain.rb', line 39 def decode_hex (hex_dump) text = "" 0.step(hex_dump.size - 2, 2) { |i| text << hex_dump[i,2].hex.chr } text end |
#find_generic_password(name) ⇒ Object
34 35 36 37 |
# File 'lib/awsborn/keychain.rb', line 34 def find_generic_password (name) # -l for label, the editable field in a secure note (and 'name' for passwords) `security -q find-generic-password -l "#{name}" -g "#{@keychain}" 2>&1 1>/dev/null` end |
#get(name) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/awsborn/keychain.rb', line 19 def get (name) unlock password_line = find_generic_password(name) if password_line.match(/^password: 0x/) hex_dump = password_line[/password: 0x(\S+)/, 1] text = decode_hex(hex_dump) text = string_content(text) if multi_encoded?(text) elsif password_line.match(/^password: "/) text = password_line[/password: "(.+)"/, 1] else raise "Note '#{name}' not found in #{@keychain}" end text end |
#lock ⇒ Object
14 15 16 17 |
# File 'lib/awsborn/keychain.rb', line 14 def lock system 'security', 'lock-keychain', @keychain @unlocked = false end |
#master_password ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/awsborn/keychain.rb', line 57 def master_password unless @password dump = `security -q find-generic-password -s "#{File.basename(@keychain)}" -g 2>&1` @password = dump[/password: "(.*)"/, 1] end @password end |
#multi_encoded?(note) ⇒ Boolean
45 46 47 |
# File 'lib/awsborn/keychain.rb', line 45 def multi_encoded? (note) note.include?(%q(<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">)) end |
#string_content(note) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/awsborn/keychain.rb', line 49 def string_content (note) text = note[%r{<string>(.*)</string>}m,1] text.gsub!('<','<') text.gsub!('>','>') text.gsub!('&','&') text end |
#unlock ⇒ Object
7 8 9 10 11 12 |
# File 'lib/awsborn/keychain.rb', line 7 def unlock unless @unlocked system 'security', 'unlock-keychain', '-p', master_password, @keychain @unlocked = true end end |