Commit b4332ff

Gavin Miller <gavmille@cisco.com>
2015-08-06 19:49:40
Strip out otpauth if it is added as part of the secret
1 parent 1c1770a
Changed files (2)
lib
spec
lib/tfa/cli.rb
@@ -7,6 +7,7 @@ module TFA
 
     desc "add NAME SECRET", "add a new secret to the database"
     def add(name, secret)
+      secret = clean(secret)
       storage.save(name, secret)
       "Added #{name}"
     end
@@ -26,5 +27,13 @@ module TFA
     def storage
       @storage ||= Storage.new(filename: options[:filename] || 'tfa')
     end
+
+    def clean(secret)
+      if secret.include?('=')
+        /secret=([^&]*)/.match(secret).captures.first
+      else
+        secret
+      end
+    end
   end
 end
spec/lib/cli_spec.rb
@@ -6,10 +6,26 @@ module TFA
       ::ROTP::TOTP.new(secret).now
     end
 
+    let(:secret) { ::ROTP::Base32.random_base32 }
+
+    describe "#add" do
+      context "when a secret is added" do
+        it "adds the secret" do
+          subject.add('development', secret)
+          expect(subject.show('development')).to eql(secret)
+        end
+      end
+
+      context "when a full otpauth string is added" do
+        it "strips out the url for just the secret" do
+          subject.add('development', "otpauth://totp/email@email.com?secret=#{secret}&issuer=")
+          expect(subject.show('development')).to eql(secret)
+        end
+      end
+    end
+
     describe "#totp" do
       context "when a single key is given" do
-        let(:secret) { ::ROTP::Base32.random_base32 }
-
         it "returns a time based one time password for the authentication secret given" do
           subject.add('development', secret)
           expect(subject.totp("development")).to eql(code_for(secret))