Blob of app/models/ssh_key_file.rb (raw blob data)

1 # FIXME: this class acts too much like a singleton: read the file contents on
2 # initialize and mark it as dirty and reload only when needed.
3 class SshKeyFile
4 def initialize(path=nil)
5 @path = path || default_authorized_keys_path
6 end
7 attr_accessor :path
8
9 def contents
10 File.read(@path)
11 end
12
13 def add_key(key)
14 File.open(@path, "a") do |file|
15 file << key
16 end
17 end
18
19 def delete_key(key)
20 data = File.read(@path)
21 return true unless data.include?(key)
22 new_data = data.gsub(key, "")
23 File.open(@path, "w") do |file|
24 file.puts new_data
25 end
26 end
27
28 protected
29 def default_authorized_keys_path
30 File.join(File.expand_path("~"), ".ssh", "authorized_keys")
31 end
32 end