class FatFreeCRM::SecretTokenGenerator

Public Class Methods

setup!() click to toggle source

If there is no secret token defined, we generate one and save it as a setting If a token has been already been saved, we tell Rails to use it and move on.

# File lib/fat_free_crm/secret_token_generator.rb, line 17
def setup!
  unless token_exists?
    Rails.logger.info("No secret key defined yet... generating and saving to Setting.secret_token")
    new_token!
  end
  # If db isn't setup yet, token will return nil, provide a randomly generated one for now.
  FatFreeCRM::Application.config.secret_key_base = (token || generate_token)
end

Private Class Methods

generate_token() click to toggle source
# File lib/fat_free_crm/secret_token_generator.rb, line 46
def generate_token
  SecureRandom.hex(64)
end
new_token!() click to toggle source

Create a new secret token and save it as a setting.

# File lib/fat_free_crm/secret_token_generator.rb, line 40
def new_token!
  quietly do
    Setting.secret_token = generate_token
  end
end
quietly() { || ... } click to toggle source

Yields to a block that executes with the logging turned off This stops the secret token from being appended to the log

# File lib/fat_free_crm/secret_token_generator.rb, line 53
def quietly(&_block)
  temp_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
  yield
  ActiveRecord::Base.logger = temp_logger
end
token() click to toggle source

Read the current token from settings

# File lib/fat_free_crm/secret_token_generator.rb, line 34
def token
  Setting.secret_token
end
token_exists?() click to toggle source
# File lib/fat_free_crm/secret_token_generator.rb, line 28
def token_exists?
  Setting.secret_token.present?
end