Class: ApiKeys::Services::TokenGenerator
- Inherits:
-
Object
- Object
- ApiKeys::Services::TokenGenerator
- Defined in:
- lib/api_keys/services/token_generator.rb
Overview
Generates secure, random API tokens according to configured settings.
Class Method Summary collapse
-
.call(length: ApiKeys.configuration.token_length, prefix: ApiKeys.configuration.token_prefix.call, alphabet: ApiKeys.configuration.token_alphabet) ⇒ String
Generates a new token string.
Class Method Details
.call(length: ApiKeys.configuration.token_length, prefix: ApiKeys.configuration.token_prefix.call, alphabet: ApiKeys.configuration.token_alphabet) ⇒ String
Generates a new token string.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/api_keys/services/token_generator.rb', line 16 def self.call(length: ApiKeys.configuration.token_length, prefix: ApiKeys.configuration.token_prefix.call, alphabet: ApiKeys.configuration.token_alphabet) random_bytes = SecureRandom.bytes(length) random_part = case alphabet when :base58 Base58.binary_to_base58(random_bytes, :bitcoin) when :hex random_bytes.unpack1("H*") # Equivalent to SecureRandom.hex else raise ArgumentError, "Unsupported token alphabet: #{alphabet}. Use :base58 or :hex." end "#{prefix}#{random_part}" end |