-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support validating OTP without a generated_at time
- Loading branch information
Showing
4 changed files
with
108 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,36 @@ | ||
module OneTimePassword | ||
class Validator < Base | ||
def initialize(code, generated_at) | ||
def initialize(code, generated_at = nil) | ||
@code = code | ||
@generated_at = generated_at | ||
end | ||
|
||
def valid? | ||
code.present? && !wrong_length? && !expired? && !incorrect? | ||
code.present? && !wrong_length? && (generated_at && !expired?) && !incorrect? | ||
end | ||
|
||
def warning | ||
return "Enter a passcode" if code.blank? | ||
return "Enter a valid passcode containing #{LENGTH} digits" if wrong_length? | ||
return "Your passcode has expired, request a new one" if expired? | ||
"Enter a valid passcode" if incorrect? | ||
return "Your passcode has expired, request a new one" if generated_at && expired? | ||
return "Your passcode is not valid or has expired" if !generated_at | ||
"Enter a valid passcode" | ||
end | ||
|
||
private | ||
|
||
attr_reader :code, :generated_at | ||
|
||
def wrong_length? | ||
return @wrong_length if defined?(@wrong_length) | ||
|
||
@wrong_length = code.gsub(/\D/, "").length != LENGTH | ||
@wrong_length ||= code.length != LENGTH | ||
end | ||
|
||
def expired? | ||
return @expired if defined?(@expired) | ||
|
||
@expired = generated_at < DRIFT.seconds.ago | ||
@expired ||= generated_at < DRIFT.seconds.ago | ||
end | ||
|
||
def incorrect? | ||
return @incorrect if defined? @incorrect | ||
|
||
@incorrect = rotp.new(SECRET, issuer: ISSUER).verify(code, drift_behind: DRIFT).nil? | ||
@incorrect ||= rotp.new(SECRET, issuer: ISSUER).verify(code, drift_behind: DRIFT).nil? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe OneTimePassword::Generator do | ||
describe "#code" do | ||
subject { described_class.new.code } | ||
|
||
let(:totp) { instance_double ROTP::TOTP, now: one_time_passcode } | ||
let(:one_time_passcode) { 123456 } | ||
|
||
before do | ||
allow(ROTP::TOTP).to receive(:new).and_return(totp) | ||
end | ||
|
||
it "generates a new code" do | ||
expect(subject).to eq one_time_passcode | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe OneTimePassword::Validator do | ||
subject { described_class.new(one_time_passcode, generated_at) } | ||
|
||
let!(:one_time_passcode) { OneTimePassword::Generator.new.code } | ||
let!(:generated_at) { Time.now } | ||
|
||
context "with a valid code" do | ||
it { is_expected.to be_valid } | ||
end | ||
|
||
context "with an empty code" do | ||
let(:one_time_passcode) { "" } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Enter a passcode" | ||
end | ||
end | ||
|
||
context "with a nil code" do | ||
let(:one_time_passcode) { nil } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Enter a passcode" | ||
end | ||
end | ||
|
||
context "with a code that is too short" do | ||
let(:one_time_passcode) { "12345" } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Enter a valid passcode containing 6 digits" | ||
end | ||
end | ||
|
||
context "with a code that is the correct length, but wrong" do | ||
let(:one_time_passcode) { "000000" } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Enter a valid passcode" | ||
end | ||
end | ||
|
||
context "with a code that has expired" do | ||
before { travel 20.minutes } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Your passcode has expired, request a new one" | ||
end | ||
|
||
context "when generated_at is not specified" do | ||
subject { described_class.new(one_time_passcode) } | ||
|
||
it { is_expected.to_not be_valid } | ||
|
||
it "has the correct warning" do | ||
expect(subject.warning).to eq "Your passcode is not valid or has expired" | ||
end | ||
end | ||
end | ||
end |