-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_crypt.rb
executable file
·130 lines (105 loc) · 3.02 KB
/
pass_crypt.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/env ruby
require "rubygems"
require "clipboard"
require File.join(File.dirname(__FILE__), "auth_model")
class PassCrypt
DEFAULT_PASSWORD_DISPLAY_TIME = 10 # seconds
USER_FOLDER = File.join(ENV['HOME'], ".pass_crypt")
def main(args)
init_config_folder
case args.join(" ")
when /^put(c)? (\w+)$/
insert($2, :clipboard => $1)
when /^get(p)? (\w+)(?: -t (\d+))?$/
retrieve($2, :password_time => $3.to_i, :only_password => $1)
when /^(list|ls)$/
list_ids
when /^del (\w+)$/
delete($1)
else
print_usage_and_quit
end
end
def insert(id, opts={})
passphrase = read_passphrase
username = read_input("Enter username: ")
password = if opts[:clipboard]
puts "Password taken from clipboard"
Clipboard.paste
else
read_input("Enter password: ", true)
end
if AuthModel.exists?(id)
overwrite = read_input("Already exists - overwrite? [Y/n]: ")
exit unless overwrite == "y" || overwrite == "Y" || overwrite == ""
AuthModel.delete(id)
end
auth = AuthModel.new(passphrase, id, username, password)
auth.save
puts "Stored!"
end
def retrieve(id, opts={})
user_delay = opts[:password_time].to_i
time = user_delay > 0 ? user_delay : DEFAULT_PASSWORD_DISPLAY_TIME
if not AuthModel.exists?(id)
puts "Invalid entry"
exit
end
passphrase = read_passphrase
auth = AuthModel.new(passphrase)
auth.retrieve_from_db(id)
puts "\n----------"
puts "Username: #{auth.username}" unless opts[:only_password]
prev_contents = Clipboard.paste
Clipboard.copy(auth.password)
puts "Password: * copied to the clipboard for #{time} seconds *"
puts "----------\n\nPress ENTER to continue"
t = Thread.new { sleep time }
Thread.new { STDIN.gets; t.kill }
t.join
ensure
Clipboard.copy(prev_contents)
end
def list_ids
puts AuthModel.get_ids.sort.join("\n")
end
def delete(id)
if AuthModel.delete(id)
puts "Deleted entry '#{id}'"
else
puts "Invalid entry"
end
end
def read_passphrase
read_input("Enter your secret passphrase: ", true)
end
def read_input(message, secret=false)
puts message
system "stty -echo" if secret
STDIN.gets.chomp
ensure
system "stty echo"
end
def print_usage_and_quit
puts "Usage: pass_crypt OPERATION [ID] [OPTION VALUE]"
puts "\nOperations:"
puts "\tget\tfetches the authentication details identified by ID"
puts "\tgetp\tfetches only the password identified by ID"
puts "\tput\tstores a username and password"
puts "\tputc\tsame as 'put', but takes password from the clipboard"
puts "\tdel\tdeletes an entry"
puts "\n\tThe following require no ID parameter:"
puts "\tlist\tdisplays the IDs of the stored authentication data"
puts "\thelp\tdisplays this usage message"
puts "\nOption:"
puts "\t-t\tholds the password in the clipboard for the given time in seconds. Only available for get and getp"
exit
end
def init_config_folder
Dir.mkdir(USER_FOLDER) unless Dir.exists?(USER_FOLDER)
rescue
puts "Fatal Error: cannot access or create #{USER_FOLDER}"
exit
end
end
PassCrypt.new.main(ARGV)