Skip to content

Commit

Permalink
Use OTP::URI for HOTP#provisioning_uri generation
Browse files Browse the repository at this point in the history
  • Loading branch information
atcruice committed Jul 13, 2020
1 parent 236d66e commit c75d3e1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
7 changes: 1 addition & 6 deletions lib/rotp/hotp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ def verify(otp, counter, retries: 0)
# @param [Integer] initial_count starting counter value, defaults to 0
# @return [String] provisioning uri
def provisioning_uri(name, initial_count = 0)
params = {
secret: secret,
counter: initial_count,
digits: digits == DEFAULT_DIGITS ? nil : digits
}
encode_params("otpauth://hotp/#{Addressable::URI.escape(name)}", params)
OTP::URI.new(self, account_name: name, counter: initial_count).to_s
end
end
end
27 changes: 6 additions & 21 deletions spec/lib/rotp/hotp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,14 @@
end

describe '#provisioning_uri' do
let(:uri) { hotp.provisioning_uri('mark@percival') }
let(:params) { CGI.parse URI.parse(uri).query }

it 'has the correct format' do
expect(uri).to match %r{\Aotpauth:\/\/hotp.+}
end

it 'includes the secret as parameter' do
expect(params['secret'].first).to eq 'a' * 32
end

context 'with default digits' do
it 'does not include digits parameter with default digits' do
expect(params['digits'].first).to be_nil
end
it 'accepts the account name' do
expect(hotp.provisioning_uri('mark@percival'))
.to eq 'otpauth://hotp/mark%40percival?secret=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&counter=0'
end

context 'with non-default digits' do
let(:hotp) { ROTP::HOTP.new('a' * 32, digits: 8) }

it 'includes digits parameter' do
expect(params['digits'].first).to eq '8'
end
it 'also accepts a custom counter value' do
expect(hotp.provisioning_uri('mark@percival', 17))
.to eq 'otpauth://hotp/mark%40percival?secret=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&counter=17'
end
end
end

0 comments on commit c75d3e1

Please sign in to comment.