summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mokhan@users.noreply.github.com>2015-08-09 17:27:24 -0600
committermo khan <mokhan@users.noreply.github.com>2015-08-09 17:27:24 -0600
commit22aaf0e09adfb955dfb93bc03b621dc269447d03 (patch)
treee34e1a5213336b8729c05510bfe4e3ad5f7175fa
parent5f859a714e715ae6a8f71cefd96c1f3487438a84 (diff)
parent5fca9b4cdcaaf20d28c4d18ff0a20673134b9c6a (diff)
Merge pull request #2 from gavingmiller/masterv0.0.11
Fix & Enhancement
-rw-r--r--lib/tfa/cli.rb11
-rw-r--r--lib/tfa/version.rb2
-rw-r--r--spec/lib/cli_spec.rb38
3 files changed, 49 insertions, 2 deletions
diff --git a/lib/tfa/cli.rb b/lib/tfa/cli.rb
index a73de1e..107f202 100644
--- a/lib/tfa/cli.rb
+++ b/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
@@ -18,7 +19,7 @@ module TFA
desc "totp NAME", "generate a Time based One Time Password"
def totp(name = nil)
- TotpCommand.new(storage).run([name])
+ TotpCommand.new(storage).run(name)
end
private
@@ -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
diff --git a/lib/tfa/version.rb b/lib/tfa/version.rb
index bc799e5..411ea83 100644
--- a/lib/tfa/version.rb
+++ b/lib/tfa/version.rb
@@ -1,3 +1,3 @@
module TFA
- VERSION = "0.0.10"
+ VERSION = "0.0.11"
end
diff --git a/spec/lib/cli_spec.rb b/spec/lib/cli_spec.rb
new file mode 100644
index 0000000..52f1a42
--- /dev/null
+++ b/spec/lib/cli_spec.rb
@@ -0,0 +1,38 @@
+module TFA
+ describe CLI do
+ subject { CLI.new }
+
+ def code_for(secret)
+ ::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
+ url = "otpauth://totp/email@email.com?secret=#{secret}&issuer="
+
+ subject.add("development", url)
+ expect(subject.show("development")).to eql(secret)
+ end
+ end
+ end
+
+ describe "#totp" do
+ context "when a single key is given" do
+ it "returns a time based one time password" do
+ subject.add("development", secret)
+ expect(subject.totp("development")).to eql(code_for(secret))
+ end
+ end
+ end
+ end
+end