Module: Keystorage
- Included in:
- CLI, Manager
- Defined in:
- lib/keystorage.rb,
lib/keystorage/cli.rb,
lib/keystorage/manager.rb
Defined Under Namespace
Classes: CLI, FormatNotSupport, Manager, NoRootGroup, NoSecret, RejectGroupName, SecretMissMatch
Constant Summary
collapse
- DEFAULT_SECRET =
"3Qw9EtWE"
- DEFAULT_FILE =
File.join(ENV["HOME"],".keystorage")
Instance Method Summary
collapse
Instance Method Details
#decode(str, secret = secret) ⇒ Object
52
53
54
55
56
|
# File 'lib/keystorage.rb', line 52
def decode(str,secret=secret)
dec = OpenSSL::Cipher::Cipher.new('aes256')
dec.decrypt.pkcs5_keyivgen(secret)
(dec.update(Array.new([str]).pack("H*")) + dec.final)
end
|
#encode(str, secret = secret) ⇒ Object
46
47
48
49
50
|
# File 'lib/keystorage.rb', line 46
def encode(str,secret=secret)
enc = OpenSSL::Cipher::Cipher.new('aes256')
enc.encrypt.pkcs5_keyivgen(secret)
((enc.update(str) + enc.final).unpack("H*")).first.to_s
end
|
#file ⇒ Object
62
63
64
|
# File 'lib/keystorage.rb', line 62
def file
YAML.load(File.new(path)) || {}
end
|
#path ⇒ Object
58
59
60
|
# File 'lib/keystorage.rb', line 58
def path
options[:file] || ENV['KEYSTORAGE_FILE'] || DEFAULT_FILE
end
|
#render(out, format = :text) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/keystorage.rb', line 74
def render out,format =:text
case format
when :text then
render_text out
else
raise FormatNotSupport.new(format.to_s)
end
end
|
#render_text(out) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/keystorage.rb', line 83
def render_text out
if out.kind_of?(Array)
out.join("\n")
else
out.to_s
end
end
|
#root ⇒ Object
27
28
29
30
|
# File 'lib/keystorage.rb', line 27
def root
raise NoRootGroup unless file.has_key?("@")
file["@"] || {}
end
|
#root!(secret = secret, data = file) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/keystorage.rb', line 32
def root! secret=secret,data=file
data["@"] = {}
data["@"]["token"] = token
data["@"]["sig"] = sign(data["@"]["token"],secret)
data
end
|
#secret ⇒ Object
70
71
72
|
# File 'lib/keystorage.rb', line 70
def secret
options[:secret] || ENV['KEYSTORAGE_SECRET'] || DEFAULT_SECRET
end
|
#sign(message, secret = secret) ⇒ Object
18
19
20
21
|
# File 'lib/keystorage.rb', line 18
def sign message,secret=secret
raise NoSecret.new("set env KEYSTORAGE_SECRET") unless secret
OpenSSL::HMAC.hexdigest('sha512',secret, message)
end
|
#token ⇒ Object
23
24
25
|
# File 'lib/keystorage.rb', line 23
def token
SecureRandom.urlsafe_base64(nil, false)
end
|
#valid? ⇒ Boolean
40
41
42
43
44
|
# File 'lib/keystorage.rb', line 40
def valid?
sign(root["token"]) == root["sig"]
rescue NoRootGroup
write root!
end
|
#write(data) ⇒ Object
66
67
68
|
# File 'lib/keystorage.rb', line 66
def write data
File.open(path,'w',0600) { |f| YAML.dump(data,f) }
end
|