From 29bddf19f6c5607191e82b80f69725e8eebfe618 Mon Sep 17 00:00:00 2001 From: Johan Brissmyr Date: Fri, 25 Apr 2014 23:55:20 +0200 Subject: [PATCH] Pad OTP codes with less than 6 digits --- .../models/two_factor_authenticatable.rb | 2 +- .../models/two_factor_authenticatable_spec.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index d55a851..410055a 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -32,7 +32,7 @@ module Devise end def otp_code(time = Time.now) - ROTP::TOTP.new(self.otp_column).at(time) + ROTP::TOTP.new(self.otp_column).at(time, true) end def provisioning_uri(account = nil, options = {}) diff --git a/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb b/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb index efa8e37..014a432 100644 --- a/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb +++ b/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb @@ -25,7 +25,15 @@ describe Devise::Models::TwoFactorAuthenticatable, '#otp_code' do let(:time) { 1392852756 } it "should return a known result" do - expect(subject).to eq(562202) + expect(subject).to eq('562202') + end + end + + context "with a known time yielding a result with less than 6 digits" do + let(:time) { 1393065856 } + + it "should return a known result padded with zeroes" do + expect(subject).to eq('007672') end end end