Skip to content

Commit

Permalink
Use ROTP::Base32 for backup codes
Browse files Browse the repository at this point in the history
This uses SecureRandom under the hood and provides more randomness per
character
  • Loading branch information
Tho85 committed Apr 3, 2020
1 parent 07acb0a commit 6ca2afd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/devise_two_factor/models/two_factor_backupable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rotp'

module Devise
module Models
# TwoFactorBackupable allows a user to generate backup codes which
Expand All @@ -20,7 +22,7 @@ def generate_otp_backup_codes!
code_length = self.class.otp_backup_code_length

number_of_codes.times do
codes << SecureRandom.hex(code_length / 2) # Hexstring has length 2*n
codes << ROTP::Base32.random_base32(code_length)
end

hashed_codes = codes.map { |code| Devise::Encryptor.digest(self.class, code) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
it 'generates recovery codes of the correct length' do
@plaintext_codes.each do |code|
expect(code.length).to eq(subject.class.otp_backup_code_length)
expect(code).to match(/\A[2-7a-z]+\z/)
end
end

Expand Down

0 comments on commit 6ca2afd

Please sign in to comment.