class OmniAuth::Strategies::Identity

The identity strategy allows you to provide simple internal user authentication using the same process flow that you use for external OmniAuth providers.

Constants

DEFAULT_REGISTRATION_FIELDS

Public Instance Methods

callback_phase() click to toggle source
Calls superclass method
# File lib/omniauth/strategies/identity.rb, line 37
def callback_phase
  return fail!(:invalid_credentials) unless identity

  super
end
identity() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 106
def identity
  if options[:locate_conditions].is_a? Proc
    conditions = instance_exec(request, &options[:locate_conditions])
    conditions.to_hash
  else
    conditions = options[:locate_conditions].to_hash
  end
  @identity ||= model.authenticate(conditions, request['password'])
end
model() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 116
def model
  options[:model] || ::Identity
end
on_registration_path?() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 102
def on_registration_path?
  on_path?(registration_path)
end
other_phase() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 43
def other_phase
  if options[:enable_registration] && on_registration_path?
    if request.get?
      registration_form
    elsif request.post?
      registration_phase
    else
      call_app!
    end
  elsif options[:enable_login] && on_request_path?
    # OmniAuth, by default, disables "GET" requests for security reasons.
    # This effectively disables omniauth-identity tool's login form feature.
    # Because it is disabled by default, and because enabling it would desecuritize all the other
    #   OmniAuth strategies that may be implemented, we do not ask users to modify that setting.
    # Instead we hook in here in the "other_phase", with a config setting of our own: `enable_login`
    request_phase
  else
    call_app!
  end
end
registration_form(validation_message = nil) click to toggle source
# File lib/omniauth/strategies/identity.rb, line 64
def registration_form(validation_message = nil)
  if options[:on_registration]
    options[:on_registration].call(env)
  else
    build_omniauth_registration_form(validation_message).to_response
  end
end
registration_path() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 98
def registration_path
  options[:registration_path] || "#{path_prefix}/#{name}/register"
end
registration_phase() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 72
def registration_phase
  attributes = (options[:fields] + DEFAULT_REGISTRATION_FIELDS).each_with_object({}) do |k, h|
    h[k] = request[k.to_s]
  end
  if model.respond_to?(:column_names) && model.column_names.include?('provider')
    attributes.reverse_merge!(provider: 'identity')
  end
  if validating?
    @identity = model.new(attributes)
    env['omniauth.identity'] = @identity
    if valid?
      @identity.save
      registration_result
    else
      registration_failure(options[:validation_failure_message])
    end
  else
    @identity = model.create(attributes)
    env['omniauth.identity'] = @identity
    registration_result
  end
end
request_phase() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 29
def request_phase
  if options[:on_login]
    options[:on_login].call(env)
  else
    build_omniauth_login_form.to_response
  end
end

Private Instance Methods

build_omniauth_login_form() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 122
def build_omniauth_login_form
  OmniAuth::Form.build(
    title: options[:title],
    url: callback_path
  ) do |f|
    f.text_field 'Login', 'auth_key'
    f.password_field 'Password', 'password'
    if options[:enable_registration]
      f.html "<p align='center'><a href='#{registration_path}'>#{options[:create_identity_link_text]}</a></p>"
    end
  end
end
build_omniauth_registration_form(validation_message) click to toggle source
# File lib/omniauth/strategies/identity.rb, line 135
def build_omniauth_registration_form(validation_message)
  OmniAuth::Form.build(title: options[:registration_form_title]) do |f|
    f.html "<p style='color:red'>#{validation_message}</p>" if validation_message
    options[:fields].each do |field|
      f.text_field field.to_s.capitalize, field.to_s
    end
    f.password_field 'Password', 'password'
    f.password_field 'Confirm Password', 'password_confirmation'
  end
end
registration_failure(message) click to toggle source
# File lib/omniauth/strategies/identity.rb, line 162
def registration_failure(message)
  if options[:on_failed_registration]
    options[:on_failed_registration].call(env)
  else
    registration_form(message)
  end
end
registration_result() click to toggle source
# File lib/omniauth/strategies/identity.rb, line 170
def registration_result
  if @identity.persisted?
    env['PATH_INFO'] = callback_path
    callback_phase
  else
    registration_failure(options[:registration_failure_message])
  end
end
valid?() click to toggle source

Validates the model before it is persisted

@return [true or false] result of :on_validation call

# File lib/omniauth/strategies/identity.rb, line 156
def valid?
  # on_validation may run a Captcha or other validation mechanism
  # Must return true when validation passes, false otherwise
  !!options[:on_validation].call(env: env)
end
validating?() click to toggle source

Validates the model before it is persisted

@return [truthy or falsey] :on_validation option is truthy or falsey

# File lib/omniauth/strategies/identity.rb, line 149
def validating?
  !!options[:on_validation]
end