Module: Negroni

Extended by:
ActiveSupport::Autoload, Configuration::Delegation
Defined in:
lib/negroni.rb,
lib/negroni/engine.rb,
lib/negroni/models.rb,
lib/negroni/version.rb,
lib/negroni/omniauth.rb,
lib/negroni/resolver.rb,
lib/negroni/encryptor.rb,
lib/negroni/models/base.rb,
lib/negroni/param_filter.rb,
lib/negroni/configuration.rb,
app/mailers/negroni/mailer.rb,
lib/negroni/mailers/helpers.rb,
lib/negroni/models/lockable.rb,
lib/negroni/omniauth/config.rb,
lib/negroni/token_generator.rb,
lib/negroni/token_not_found.rb,
lib/negroni/models/recoverable.rb,
lib/negroni/models/validatable.rb,
lib/negroni/controllers/helpers.rb,
lib/negroni/models/omniauthable.rb,
lib/negroni/models/registerable.rb,
lib/negroni/models/authenticable.rb,
lib/negroni/controllers/token_authenticable.rb

Overview

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

Defined Under Namespace

Modules: Controllers, Encryptor, Mailers, Models, OmniAuth Classes: Configuration, Engine, Mailer, ParamFilter, Resolver, TokenGenerator, TokenNotFound

Constant Summary collapse

ALL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

To hold all modules

[]
VERSION =

Version number

'0.1.0'

Private Configuration collapse

Module Registration collapse

Class Method Summary collapse

Methods included from Configuration::Delegation

authentication_keys, case_insensitive_keys, config_delegator, configuration, email_regexp, lock_strategy, mailer_sender, maximum_attempts, not_found_exception, parent_controller, parent_mailer, password_length, pepper, reset_password_keys, reset_password_within, send_password_change_notification, stretches, strip_whitespace_keys, token_algorithm, token_audience, token_lifetime, token_public_key, token_secret, unlock_in, unlock_keys, unlock_strategy

Class Attribute Details

.mailerMailer

Returns the mailer

Returns:



# File 'lib/negroni.rb', line 61

.omniauth_configsHash<Symbol,OmniAuth::Config>

Stores OmniAuth configurations

Returns:



47
48
49
# File 'lib/negroni.rb', line 47

def omniauth_configs
  @omniauth_configs
end

.paranoidBoolean

When true, enter in paranoid mode to avoid user enumeration.

Returns:

  • (Boolean)


51
52
53
# File 'lib/negroni.rb', line 51

def paranoid
  @paranoid
end

.secret_keyString

Stores the secret key

Returns:

  • (String)


59
60
61
# File 'lib/negroni.rb', line 59

def secret_key
  @secret_key
end

.token_generatorTokenGenerator

Stores the token generator

Returns:



55
56
57
# File 'lib/negroni.rb', line 55

def token_generator
  @token_generator
end

Class Method Details

.configure {|Negroni| ... } ⇒ Void

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

Yields:

Returns:

  • (Void)


82
83
84
# File 'lib/negroni.rb', line 82

def configure
  yield self
end

.friendly_token(length = 20) ⇒ String

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

@note: Taken from ‘Devise`.

Parameters:

  • length (Integer) (defaults to: 20)

    the length of the token. Default: 20

Returns:

  • (String)

    the generated token



113
114
115
116
# File 'lib/negroni.rb', line 113

def friendly_token(length = 20)
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
end

.omniauth(provider, *args) ⇒ Void

Register an OmniAuth provider.

Examples:

config.omniauth :github, APP_ID, APP_SECRET

Parameters:

  • provider (Symbol)

    the OmniAuth provider to register

  • args (Object*)

    any additional arguments needed to configure and initialize the provider

Returns:

  • (Void)


102
103
104
105
# 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_providersArray<Symbol>

List of OmniAuth provider that are registered

Returns:

  • (Array<Symbol>)


88
89
90
# File 'lib/negroni.rb', line 88

def omniauth_providers
  omniauth_configs.keys
end

.register_module(module_name, options = {}) ⇒ Void

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.

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.

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

Examples:

Negroni.register_module(:party_module)
Negroni.register_module(:party_module, model: 'party_module/model')
Negroni.register_module(:party_module, insert_at: 0)

Parameters:

  • module_name (Symbol)

    the name of the module to register

  • options (Hash) (defaults to: {})

    a hash of options

Options Hash (options):

  • :model (String)

    the load path to a custom __model__ for this module (to autoload.)

  • :controller (Symbol, Boolean)

    the name of an existing or custom __controller__ for this module.

  • :route (Symbol, Boolean)

    the named __route__ helper for this module.

Returns:

  • (Void)


180
181
182
183
184
185
186
187
188
189
190
# 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) ⇒ Boolean

Securely compare two passwords

Parameters:

  • a (String)

    the hashed password

  • b (String)

    the password to compare

Returns:

  • (Boolean)

    whether or not the passwords match



124
125
126
127
128
129
130
131
132
# 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