diff options
| author | mo khan <mo@mokhan.ca> | 2014-07-25 17:06:44 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-07-25 17:06:44 -0600 |
| commit | 38cb80f8a96a3899a884959eff5dfdf7ffc60b3a (patch) | |
| tree | 2946e0c738477876ef5d836805f86c672663f822 | |
| parent | d12953c65d628d502e1576548d57357487ca02eb (diff) | |
dump all totp passwords not no key is given.
| -rw-r--r-- | lib/tfa/totp_command.rb | 21 | ||||
| -rw-r--r-- | spec/lib/totp_command_spec.rb | 26 |
2 files changed, 41 insertions, 6 deletions
diff --git a/lib/tfa/totp_command.rb b/lib/tfa/totp_command.rb index 9d6e54a..b49afde 100644 --- a/lib/tfa/totp_command.rb +++ b/lib/tfa/totp_command.rb @@ -5,7 +5,26 @@ module TFA end def run(arguments) - ::ROTP::TOTP.new(@storage.secret_for(arguments.first)).now + return password_for(secret_for(arguments.first)) if arguments.any? + all_passwords + end + + private + + def password_for(secret) + ::ROTP::TOTP.new(secret).now + end + + def all_passwords + secrets = @storage.all_secrets + secrets.each do |hash| + hash[hash.keys.first] = password_for(hash[hash.keys.first]) + end + secrets + end + + def secret_for(key) + @storage.secret_for(key) end end end diff --git a/spec/lib/totp_command_spec.rb b/spec/lib/totp_command_spec.rb index 354e38a..8b5bae1 100644 --- a/spec/lib/totp_command_spec.rb +++ b/spec/lib/totp_command_spec.rb @@ -4,14 +4,30 @@ module TFA let(:secret) { ::ROTP::Base32.random_base32 } let(:storage) { Storage.new(Tempfile.new('test').path) } - before :each do - storage.save('development', secret) + def code_for(secret) + ::ROTP::TOTP.new(secret).now end describe "#run" do - it "returns a time based one time password for the authentication secret given" do - correct_code = ::ROTP::TOTP.new(secret).now - expect(subject.run(["development"])).to eql(correct_code) + context "when a single key is given" do + it "returns a time based one time password for the authentication secret given" do + storage.save('development', secret) + expect(subject.run(["development"])).to eql(code_for(secret)) + end + end + + context "when no arguments are given" do + let(:development_secret) { ::ROTP::Base32.random_base32 } + let(:staging_secret) { ::ROTP::Base32.random_base32 } + + it "returns the one time password for all keys" do + storage.save('development', development_secret) + storage.save('staging', staging_secret) + expect(subject.run([])).to eql([ + { 'development' => code_for(development_secret) }, + { 'staging' => code_for(staging_secret) } + ]) + end end end end |
