Module: AnotherApi::TokenGeneration

Defined in:
lib/another_api/token_generation.rb

Class Method Summary collapse

Class Method Details

.digest(raw_token) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/another_api/token_generation.rb', line 16

def self.digest(raw_token)
  secret = AnotherApi.configuration.token_secret
  if secret.nil? || secret.to_s.empty?
    raise ConfigurationError, "AnotherApi.configuration.token_secret must be set"
  end
  OpenSSL::HMAC.hexdigest("SHA256", secret, raw_token)
end

.generate(prefix: AnotherApi.configuration.token_prefix) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/another_api/token_generation.rb', line 3

def self.generate(prefix: AnotherApi.configuration.token_prefix)
  body = SecureRandom.base58(24)
  token_prefix = SecureRandom.base58(4)
  token_suffix = SecureRandom.base58(4)
  raw_token = "#{prefix}_#{token_prefix}#{body}#{token_suffix}"
  {
    raw_token: raw_token,
    token_digest: digest(raw_token),
    token_prefix: token_prefix,
    token_suffix: token_suffix
  }
end