Commit d5dc55d
Changed files (4)
lib/tfa/storage.rb
@@ -3,8 +3,13 @@ module TFA
include Enumerable
def initialize(options)
- partial_path = File.join(Dir.home, ".#{options[:filename]}")
- @storage = PStore.new("#{partial_path}.pstore")
+ pstore_path = File.join(Dir.home, ".#{options[:filename]}.pstore")
+ if File.exist?(pstore_path)
+ @storage = PStore.new(pstore_path)
+ else
+ path = File.join(Dir.home, ".#{options[:filename]}.yml")
+ @storage = YAML::Store.new(path)
+ end
end
def each
lib/tfa.rb
@@ -1,4 +1,5 @@
require "pstore"
+require "yaml/store"
require "rotp"
require "openssl"
require "tfa/version"
spec/lib/cli_spec.rb
@@ -1,6 +1,6 @@
module TFA
describe CLI do
- subject { CLI.new }
+ subject { CLI.new([], filename: SecureRandom.uuid) }
def code_for(secret)
::ROTP::TOTP.new(secret).now
@@ -10,10 +10,12 @@ module TFA
let(:prod_secret) { ::ROTP::Base32.random_base32 }
describe "#add" do
+ let(:key) { SecureRandom.uuid }
+
context "when a secret is added" do
it "adds the secret" do
- subject.add("development", dev_secret)
- expect(subject.show("development")).to eql(dev_secret)
+ subject.add(key, dev_secret)
+ expect(subject.show(key)).to eql(dev_secret)
end
end
@@ -21,23 +23,26 @@ module TFA
it "strips out the url for just the secret" do
url = "otpauth://totp/email@email.com?secret=#{dev_secret}&issuer="
- subject.add("development", url)
- expect(subject.show("development")).to eql(dev_secret)
+ subject.add(key, url)
+ expect(subject.show(key)).to eql(dev_secret)
end
end
end
describe "#show" do
context "when a single key is given" do
+ let(:key) { SecureRandom.uuid }
+
it "returns the secret" do
- subject.add("development", dev_secret)
- expect(subject.show("development")).to eql(dev_secret)
+ subject.add(key, dev_secret)
+ expect(subject.show(key)).to eql(dev_secret)
end
end
context "when no key is given" do
it "returns the secret for all keys" do
- subject.add("development", dev_secret)
+ key = SecureRandom.uuid
+ subject.add(key, dev_secret)
subject.add("production", prod_secret)
result = subject.show.to_s
@@ -50,15 +55,16 @@ module TFA
describe "#totp" do
context "when a single key is given" do
it "returns a time based one time password" do
- subject.add("development", dev_secret)
- expect(subject.totp("development")).to eql(code_for(dev_secret))
+ key = SecureRandom.uuid
+ subject.add(key, dev_secret)
+ expect(subject.totp(key)).to eql(code_for(dev_secret))
end
end
context "when no key is given" do
it "returns a time based one time password for all keys" do
- subject.add("development", dev_secret)
- subject.add("production", prod_secret)
+ subject.add(SecureRandom.uuid, dev_secret)
+ subject.add(SecureRandom.uuid, prod_secret)
result = subject.totp.to_s
expect(result).to include(code_for(dev_secret))
tfa.gemspec
@@ -17,10 +17,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
+ spec.required_ruby_version = "~> 2.0"
- spec.add_dependency "rotp"
- spec.add_dependency "thor"
+ spec.add_dependency "rotp", "~> 3.3"
+ spec.add_dependency "thor", "~> 0.20"
spec.add_development_dependency "bundler", "~> 1.6"
- spec.add_development_dependency "rake"
- spec.add_development_dependency "rspec"
+ spec.add_development_dependency "rake", "~> 12.3"
+ spec.add_development_dependency "rspec", "~> 3.7"
end