Commit 70672c4

mo khan <mo@mokhan.ca>
2014-07-25 17:35:49
add command to compute totp.
1 parent 7cd4063
Changed files (2)
lib/in.rb
@@ -3,8 +3,9 @@ require "pstore"
 
 module In
   class Console
-    def initialize(filename = "secrets")
+    def initialize(filename = "secrets", authenticator)
       @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
+      @authenticator = authenticator
     end
 
     def run(command)
@@ -16,10 +17,13 @@ module In
     private
 
     def command_for(command_name)
-      if command_name == "add"
+      case command_name
+      when "add"
         AddCommand.new(@storage)
-      else
+      when "show"
         ShowCommand.new(@storage)
+      when "totp"
+        TotpCommand.new(@storage, @authenticator)
       end
     end
   end
@@ -51,4 +55,19 @@ module In
       end
     end
   end
+
+  class TotpCommand
+    def initialize(storage, authenticator)
+      @storage = storage
+      @authenticator = authenticator
+    end
+
+    def run(arguments)
+      name = arguments.first
+      secret = @storage.transaction(true) do
+        @storage[name]
+      end
+      @authenticator.totp(secret)
+    end
+  end
 end
spec/lib/console_spec.rb
@@ -1,11 +1,20 @@
 module In
   describe Console do
-    subject { Console.new('testing') }
+    subject { Console.new('testing', authenticator) }
     let(:secret) { SecureRandom.uuid }
+    let(:authenticator) { Object.new }
 
     it "saves a new secret" do
       subject.run("add development #{secret}")
       expect(subject.run("show development")).to eql(secret)
     end
+
+    it "creates a totp for a certain key" do
+      totp = rand(100)
+      subject.run("add development #{secret}")
+
+      authenticator.stub(:totp).with(secret).and_return(totp)
+      expect(subject.run("totp development")).to eql(totp)
+    end
   end
 end