Class: MrF::Keyring

Inherits:
Object
  • Object
show all
Defined in:
lib/mrf/keyring.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Keyring

Returns a new instance of Keyring.



12
13
14
15
# File 'lib/mrf/keyring.rb', line 12

def initialize (opts = {})
  @path = opts.fetch(:path)
  @gpg_passphrase = opts[:gpg_passphrase]
end

Instance Attribute Details

#gpg_passphraseObject (readonly)

Returns the value of attribute gpg_passphrase.



10
11
12
# File 'lib/mrf/keyring.rb', line 10

def gpg_passphrase
  @gpg_passphrase
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/mrf/keyring.rb', line 9

def path
  @path
end

#recipientsObject

Returns the value of attribute recipients.



9
10
11
# File 'lib/mrf/keyring.rb', line 9

def recipients
  @recipients
end

Instance Method Details

#cryptoObject



66
67
68
# File 'lib/mrf/keyring.rb', line 66

def crypto
  @crypto ||= GPGME::Crypto.new
end

#dataObject



17
18
19
20
21
22
23
24
25
# File 'lib/mrf/keyring.rb', line 17

def data
  return @data if @data
  if File.exists?(path)
    raw_text = crypto.decrypt(File.open(path), passphrase_callback: method(:passfunc))
    @data = YAML.load(raw_text.to_s)
  else
    @data = {}
  end
end

#passfunc(obj, uid_hint, passphrase_info, prev_was_bad, fd) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mrf/keyring.rb', line 27

def passfunc (obj, uid_hint, passphrase_info, prev_was_bad, fd)
  # Use known passphrase when given
  if @gpg_passphrase
    io = IO.for_fd(fd, 'w')
    io.puts(@gpg_passphrase)
    io.flush
    return
  end

  # Try from keychain
  key_id = passphrase_info.split(' ').first
  key_id = uid_hint[/<[^<>]*>$/].tr("<>", "")
  dump = `security -q find-generic-password -s "gpg-#{key_id}" -g 2>&1`
  password = dump[/password: "(.*)"/, 1]

  if password
    io = IO.for_fd(fd, 'w')
    io.puts(password)
    io.flush
  else
    # Prompt user
    begin
      io = IO.for_fd(fd, 'w')

      console = IO.console
      console.write("Passphrase for #{uid_hint}: ")
      console.noecho do |noecho|
        io.puts(noecho.gets)
        io.flush
      end
      console.puts
    ensure
      (0 ... $_.length).each do |i| $_[i] = ?0 end if $_
    end
  end

  $stderr.puts
end