module Negroni

Negroni extracts common authentication configuration to be used across the application.

Constants

ALL

@api private To hold all modules

VERSION

Version number

Attributes

omniauth_configs[RW]

Stores OmniAuth configurations @return [Hash<Symbol,OmniAuth::Config>]

paranoid[RW]

When true, enter in paranoid mode to avoid user enumeration. @return [Boolean]

secret_key[RW]

Stores the secret key @return [String]

token_generator[RW]

Stores the token generator @return [TokenGenerator]

Public Class Methods

configure() { |self| ... } click to toggle source

Yields the module for configuration. This is the standard way to configure Negroni.

@yield [Negroni] @return [Void]

# File lib/negroni.rb, line 82
def configure
  yield self
end
friendly_token(length = 20) click to toggle source

Generate a friendly string randomly to be used as a token.

@note: Taken from `Devise`.

@param length [Integer] the length of the token. Default: 20 @return [String] the generated token

# File lib/negroni.rb, line 113
def friendly_token(length = 20)
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
end
mailer() click to toggle source

Returns the mailer @return [Mailer]

# File lib/negroni.rb, line 66
def mailer
  @mailer_ref.resolve
end
mailer=(new_mailer) click to toggle source

Set the mailer class

# File lib/negroni.rb, line 71
def mailer=(new_mailer)
  @mailer_ref = ref(new_mailer)
end
omniauth(provider, *args) click to toggle source

Register an OmniAuth provider.

@param provider [Symbol] the OmniAuth provider to register @param args [Object*] any additional arguments needed to configure and

initialize the provider

@example

config.omniauth :github, APP_ID, APP_SECRET

@return [Void]

# File lib/negroni.rb, line 102
def omniauth(provider, *args)
  config = Negroni::OmniAuth::Config.new(provider, args)
  omniauth_configs[config.strategy_name.to_sym] = config
end
omniauth_providers() click to toggle source

List of OmniAuth provider that are registered @return [Array<Symbol>]

# File lib/negroni.rb, line 88
def omniauth_providers
  omniauth_configs.keys
end
register_module(module_name, options = {}) click to toggle source

Register available negroni modules. For the standard modules that Negroni provides, this method is called from lib/negroni/modules.rb. Third-party modules need to be added explicitly using this method.

@note that adding a module using this method does not cause it to be used

in the authentication process. That requires that the module be listed in
the arguments passed to the 'negroni' method in the model class
definition.

@param module_name [Symbol] the name of the module to register @param options [Hash] a hash of options @option options [String] :model the load path to a custom __model__ for

this module (to autoload.)

@option options [Symbol, Boolean] :controller the name of an existing or

custom __controller__ for this module.

@option options [Symbol, Boolean] :route the named __route__ helper for

this module.

All values which accept a boolean will have the same name as the given module name.

@return [Void]

@example

Negroni.register_module(:party_module)
Negroni.register_module(:party_module, model: 'party_module/model')
Negroni.register_module(:party_module, insert_at: 0)
# File lib/negroni.rb, line 180
def register_module(module_name, options = {})
  options.assert_valid_keys(:model, :controller, :insert_at)

  ALL.insert (options[:insert_at] || -1), module_name

  if (controller = options[:controller])
    register_controller(controller, module_name)
  end

  options[:model] && register_model(options[:model], module_name)
end
secure_compare(a, b) click to toggle source

Securely compare two passwords

@param a [String] the hashed password @param b [String] the password to compare

@return [Boolean] whether or not the passwords match

# File lib/negroni.rb, line 124
def secure_compare(a, b)
  return false if a.blank? || b.blank? || a.bytesize != b.bytesize

  l = a.unpack "C#{a.bytesize}"

  res = 0
  b.each_byte { |byte| res |= byte ^ l.shift }
  res.zero?
end

Private Class Methods

ref(arg) click to toggle source

Create a reference @return [Resolver]

# File lib/negroni.rb, line 138
def ref(arg)
  ActiveSupport::Dependencies.reference(arg)
  Resolver.new(arg)
end
register_controller(controller, module_name) click to toggle source
# File lib/negroni.rb, line 194
def register_controller(controller, module_name)
  controller = (controller == true ? module_name : controller)
  CONTROLLERS[module_name] = controller
end
register_model(model, module_name) click to toggle source
# File lib/negroni.rb, line 199
def register_model(model, module_name)
  path = (model == true ? "negroni/models/#{module_name}" : model)
  camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
  Negroni::Models.send(:autoload, camelized.to_sym, path)
end