Module: Kingsman

Defined in:
lib/kingsman.rb,
lib/kingsman/orm.rb,
lib/kingsman/jets.rb,
lib/kingsman/hooks.rb,
lib/kingsman/models.rb,
lib/kingsman/mapping.rb,
lib/kingsman/version.rb,
lib/kingsman/delegator.rb,
lib/kingsman/encryptor.rb,
lib/kingsman/autoloader.rb,
lib/kingsman/failure_app.rb,
lib/kingsman/hooks/proxy.rb,
lib/kingsman/jets/routes.rb,
lib/kingsman/mailers/helpers.rb,
lib/kingsman/models/lockable.rb,
lib/kingsman/omniauth/config.rb,
lib/kingsman/strategies/base.rb,
lib/kingsman/token_generator.rb,
lib/kingsman/models/trackable.rb,
lib/kingsman/parameter_filter.rb,
lib/kingsman/secret_key_finder.rb,
lib/kingsman/models/confirmable.rb,
lib/kingsman/models/recoverable.rb,
lib/kingsman/models/timeoutable.rb,
lib/kingsman/models/validatable.rb,
lib/kingsman/controllers/helpers.rb,
lib/kingsman/models/omniauthable.rb,
lib/kingsman/models/registerable.rb,
lib/kingsman/models/rememberable.rb,
lib/kingsman/parameter_sanitizer.rb,
lib/kingsman/omniauth/url_helpers.rb,
lib/kingsman/controllers/responder.rb,
lib/generators/kingsman/orm_helpers.rb,
lib/kingsman/models/authenticatable.rb,
lib/kingsman/controllers/sign_in_out.rb,
lib/kingsman/controllers/url_helpers.rb,
lib/kingsman/strategies/rememberable.rb,
lib/kingsman/controllers/rememberable.rb,
lib/kingsman/controllers/scoped_views.rb,
lib/generators/kingsman/views_generator.rb,
lib/kingsman/controllers/store_location.rb,
lib/kingsman/strategies/authenticatable.rb,
lib/generators/kingsman/install_generator.rb,
lib/generators/kingsman/kingsman_generator.rb,
lib/kingsman/models/database_authenticatable.rb,
lib/generators/kingsman/controllers_generator.rb,
lib/kingsman/strategies/database_authenticatable.rb

Defined Under Namespace

Modules: Controllers, Encryptor, Generators, Hooks, Mailers, Models, OmniAuth, Orm, Router, Strategies Classes: Autoloader, ConfirmationsController, Delegator, Engine, Error, FailureApp, Mailer, Mapping, MissingWarden, OmniauthCallbacksController, ParameterFilter, ParameterSanitizer, PasswordsController, RegistrationsController, SecretKeyFinder, SessionsController, TokenGenerator, UnlocksController

Constant Summary collapse

ALL =
[]
CONTROLLERS =
{}
ROUTES =
{}
STRATEGIES =
{}
URL_HELPERS =
{}
NO_INPUT =

Strategies that do not require user input.

[]
TRUE_VALUES =

True values used to check params

[true, 1, '1', 't', 'T', 'true', 'TRUE']
VERSION =
"0.1.1"
@@secret_key =
nil
@@rememberable_options =
{}
@@stretches =
12
@@http_authentication_key =
nil
@@authentication_keys =
[:email]
@@request_keys =
[]
@@case_insensitive_keys =
[:email]
@@strip_whitespace_keys =
[:email]
@@http_authenticatable =
false
@@http_authenticatable_on_xhr =
true
@@params_authenticatable =
true
@@http_authentication_realm =
"Application"
@@email_regexp =
/\A[^@\s]+@[^@\s]+\z/
@@password_length =
6..128
@@remember_for =
2.weeks
@@extend_remember_period =
false
@@expire_all_remember_me_on_sign_out =
true
@@allow_unconfirmed_access_for =
0.days
@@confirm_within =
nil
@@confirmation_keys =
[:email]
@@reconfirmable =
true
@@timeout_in =
30.minutes
@@pepper =
nil
@@send_email_changed_notification =
false
@@send_password_change_notification =
false
@@scoped_views =
false
@@lock_strategy =
:failed_attempts
@@unlock_keys =
[:email]
@@unlock_strategy =
:both
@@maximum_attempts =
20
@@unlock_in =
1.hour
@@reset_password_keys =
[:email]
@@reset_password_within =
6.hours
@@sign_in_after_reset_password =
true
@@default_scope =
nil
@@mailer_sender =
nil
@@skip_session_storage =
[:http_auth]
["*/*", :html, :turbo_stream]
@@responder =
Kingsman::Controllers::Responder
@@sign_out_all_scopes =
true
@@sign_out_via =
:delete
@@parent_controller =
"ApplicationController"
@@parent_mailer =
"ActionMailer::Base"
@@router_name =
nil
@@omniauth_path_prefix =
nil
@@clean_up_csrf_token_on_authentication =
true
@@reload_routes =
true
@@mappings =
{}
@@omniauth_configs =
{}
@@warden_config =
nil
@@warden_config_blocks =
[]
@@paranoid =
false
@@last_attempt_warning =
true
@@token_generator =
nil
@@sign_in_after_change_password =
true

Class Method Summary collapse

Class Method Details

.add_mapping(resource, options) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/kingsman.rb', line 284

def self.add_mapping(resource, options)
  mapping = Mapping.new(resource, options)
  @@mappings[mapping.name] = mapping
  @@default_scope ||= mapping.name
  Kingsman::Controllers::Helpers.define_helpers(mapping)
  mapping # important to return the mapping
end

.add_module(module_name, options = {}) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/kingsman.rb', line 292

def self.add_module(module_name, options = {})
  ALL.insert (options[:insert_at] || -1), module_name

  if strategy = options[:strategy]
    strategy = (strategy == true ? module_name : strategy)
    STRATEGIES[module_name] = strategy
  end

  if controller = options[:controller]
    controller = (controller == true ? module_name : controller)
    CONTROLLERS[module_name] = controller
  end

  NO_INPUT << strategy if options[:no_input]

  if route = options[:route]
    case route
    when TrueClass
      key, value = module_name, []
    when Symbol
      key, value = route, []
    when Hash
      key, value = route.keys.first, route.values.flatten
    else
      raise ArgumentError, ":route should be true, a Symbol or a Hash"
    end

    URL_HELPERS[key] ||= []
    URL_HELPERS[key].concat(value)
    URL_HELPERS[key].uniq!

    ROUTES[module_name] = key
  end

  if options[:model]
    path = (options[:model] == true ? "kingsman/models/#{module_name}" : options[:model])
    camelized = ActiveSupport::Inflector.camelize(module_name.to_s)
    Kingsman::Models.send(:autoload, camelized.to_sym, path)
  end

  Kingsman::Mapping.add_module module_name
end

.available_router_nameObject



265
266
267
# File 'lib/kingsman.rb', line 265

def self.available_router_name
  router_name || :main_app
end

.configure_warden!Object

A method used internally to complete the setup of warden manager after routes are loaded. See lib/kingsman/rails/routes.rb - ActionDispatch::Routing::RouteSet#finalize_with_kingsman!



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/kingsman.rb', line 379

def self.configure_warden! #:nodoc:
  @@warden_configured ||= begin
    # warden_config.failure_app   = Kingsman::SessionsController.action(:unauthenticated)
    warden_config.failure_app   = Kingsman::Delegator.new
    warden_config.default_scope = Kingsman.default_scope
    warden_config.intercept_401 = false

    Kingsman.mappings.each_value do |mapping|
      warden_config.scope_defaults mapping.name, strategies: mapping.strategies

      warden_config.serialize_into_session(mapping.name) do |record|
        mapping.to.serialize_into_session(record)
      end

      warden_config.serialize_from_session(mapping.name) do |args|
        mapping.to.serialize_from_session(*args)
      end
    end

    @@warden_config_blocks.map { |block| block.call Kingsman.warden_config }
    true
  end
end

.friendly_token(length = 20) ⇒ Object

Generate a friendly string randomly to be used as token. By default, length is 20 characters.



405
406
407
408
409
410
# File 'lib/kingsman.rb', line 405

def self.friendly_token(length = 20)
  # To calculate real characters, we must perform this operation.
  # See SecureRandom.urlsafe_base64
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
end

.include_helpers(scope) ⇒ Object

Include helpers in the given scope to AC and AV.



360
361
362
363
364
365
366
367
368
369
# File 'lib/kingsman.rb', line 360

def self.include_helpers(scope)
  ActiveSupport.on_load(:jets_controller) do
    include scope::Helpers if defined?(scope::Helpers)
    include scope::UrlHelpers
  end

  ActiveSupport.on_load(:action_view) do
    include scope::UrlHelpers
  end
end

.mailerObject

Get the mailer class from the mailer reference object.



274
275
276
# File 'lib/kingsman.rb', line 274

def self.mailer
  @@mailer_ref.constantize
end

.mailer=(class_name) ⇒ Object

Set the mailer reference object to access the mailer.



279
280
281
# File 'lib/kingsman.rb', line 279

def self.mailer=(class_name)
  @@mailer_ref = class_name
end

.omniauth(provider, *args) ⇒ Object

Specify an OmniAuth provider.

config.omniauth :github, APP_ID, APP_SECRET


354
355
356
357
# File 'lib/kingsman.rb', line 354

def self.omniauth(provider, *args)
  config = Kingsman::OmniAuth::Config.new(provider, args)
  @@omniauth_configs[config.strategy_name.to_sym] = config
end

.omniauth_providersObject



269
270
271
# File 'lib/kingsman.rb', line 269

def self.omniauth_providers
  omniauth_configs.keys
end

.regenerate_helpers!Object

Regenerates url helpers considering Kingsman.mapping



372
373
374
375
# File 'lib/kingsman.rb', line 372

def self.regenerate_helpers!
  Kingsman::Controllers::UrlHelpers.remove_helpers!
  Kingsman::Controllers::UrlHelpers.generate_helpers!
end

.secure_compare(a, b) ⇒ Object

constant-time comparison algorithm to prevent timing attacks



413
414
415
416
417
418
419
420
# File 'lib/kingsman.rb', line 413

def self.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 == 0
end

.setup {|_self| ... } ⇒ Object

Default way to set up Kingsman. Run jets generate kingsman:install to create a fresh initializer with all configuration values.

Yields:

  • (_self)

Yield Parameters:

  • _self (Kingsman)

    the object that the method was called on



424
425
426
# File 'lib/kingsman.rb', line 424

def self.setup
  yield self
end

.warden(&block) ⇒ Object

Sets warden configuration using a block that will be invoked on warden initialization.

Kingsman.setup do |config|
  config.allow_unconfirmed_access_for = 2.days

  config.warden do |manager|
    # Configure warden to use other strategies, like oauth.
    manager.oauth(:twitter)
  end
end


346
347
348
# File 'lib/kingsman.rb', line 346

def self.warden(&block)
  @@warden_config_blocks << block
end