Class: PasskeyAuth::MagicLink::ShortCode

Inherits:
Object
  • Object
show all
Defined in:
app/models/passkey_auth/magic_link/short_code.rb

Constant Summary collapse

ALPHABET =

Based on the Crockford alphabet: https://www.crockford.com/base32.html

"0123456789ABCDEFGHJKMNPQRSTVWXYZ".freeze
CHARS =
ALPHABET.chars.freeze
BASE =
CHARS.size
DEFAULT_LENGTH =
8

Class Method Summary collapse

Class Method Details

.clean(code) ⇒ Object

Clean user input by removing any formatting and uppercase



24
25
26
# File 'app/models/passkey_auth/magic_link/short_code.rb', line 24

def self.clean(code)
  code.to_s.upcase.gsub(/[^A-Z0-9]/, "")
end

.format(code) ⇒ Object

Format code for display (e.g., "1234-5678")



17
18
19
20
21
# File 'app/models/passkey_auth/magic_link/short_code.rb', line 17

def self.format(code)
  return nil if code.blank?

  sprintf("%s%s%s%s-%s%s%s%s", *code.chars)
end

.generate(length = DEFAULT_LENGTH) ⇒ Object



12
13
14
# File 'app/models/passkey_auth/magic_link/short_code.rb', line 12

def self.generate(length = DEFAULT_LENGTH)
  Array.new(length) { CHARS[SecureRandom.random_number(BASE)] }.join
end