Class: OmniAuth::Strategies::Identity

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/identity.rb

Overview

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

Examples:

Basic Setup

use OmniAuth::Strategies::Identity,
    fields: [:email],
    model: User

With Registration

use OmniAuth::Strategies::Identity,
    fields: [:email, :name],
    model: User,
    enable_registration: true

See Also:

Constant Summary collapse

DEFAULT_REGISTRATION_FIELDS =

Default fields required for registration.

Returns:

  • (Array<Symbol>)
%i[password password_confirmation].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Text for the link to create a new identity.

Returns:

  • (String)


71
# File 'lib/omniauth/strategies/identity.rb', line 71

option :create_identity_link_text, "Create an Identity"

#DEFAULT_REGISTRATION_FIELDSArray<Symbol> (readonly)

Default fields required for registration.

Returns:

  • (Array<Symbol>)


25
# File 'lib/omniauth/strategies/identity.rb', line 25

DEFAULT_REGISTRATION_FIELDS = %i[password password_confirmation].freeze

#enable_logintrue, false

Whether to enable login functionality.

Returns:

  • (true, false)


41
# File 'lib/omniauth/strategies/identity.rb', line 41

option :enable_login, true

#enable_registrationtrue, false

Whether to enable user registration functionality.

Returns:

  • (true, false)


36
# File 'lib/omniauth/strategies/identity.rb', line 36

option :enable_registration, true

#fieldsArray<Symbol>

The fields to collect for user registration.

Returns:

  • (Array<Symbol>)


31
# File 'lib/omniauth/strategies/identity.rb', line 31

option :fields, %i[name email]

#locate_conditionsProc, Hash

Conditions for locating an identity during login.

Returns:

  • (Proc, Hash)


66
# File 'lib/omniauth/strategies/identity.rb', line 66

option :locate_conditions, ->(req) { {model.auth_key => req.params["auth_key"]} }

#on_failed_registrationProc?

Custom handler for failed registration.

Returns:

  • (Proc, nil)


61
# File 'lib/omniauth/strategies/identity.rb', line 61

option :on_failed_registration, nil

#on_loginProc?

Custom login handler. If provided, called instead of showing the default login form.

Returns:

  • (Proc, nil)


46
# File 'lib/omniauth/strategies/identity.rb', line 46

option :on_login, nil

#on_registrationProc?

Custom registration handler. If provided, called instead of showing the default registration form.

Returns:

  • (Proc, nil)


56
# File 'lib/omniauth/strategies/identity.rb', line 56

option :on_registration, nil

#on_validationProc?

Custom validation handler for registration.

Returns:

  • (Proc, nil)


51
# File 'lib/omniauth/strategies/identity.rb', line 51

option :on_validation, nil

#registration_failure_messageString

Message to display on registration failure.

Returns:

  • (String)


76
# File 'lib/omniauth/strategies/identity.rb', line 76

option :registration_failure_message, "One or more fields were invalid"

#registration_form_titleString

Title for the registration form.

Returns:

  • (String)


91
# File 'lib/omniauth/strategies/identity.rb', line 91

option :registration_form_title, "Register Identity"

#titleString

Title for the login form.

Returns:

  • (String)


86
# File 'lib/omniauth/strategies/identity.rb', line 86

option :title, "Identity Verification"

#validation_failure_messageString

Message to display on validation failure.

Returns:

  • (String)


81
# File 'lib/omniauth/strategies/identity.rb', line 81

option :validation_failure_message, "Validation failed"

Instance Method Details

#callback_phasevoid

This method returns an undefined value.

Handles the callback phase after login.

Authenticates the user and calls super if successful.



111
112
113
114
115
# File 'lib/omniauth/strategies/identity.rb', line 111

def callback_phase
  return fail!(:invalid_credentials) unless identity

  super
end

#identityObject?

Finds and authenticates the identity based on the request parameters.

Returns:

  • (Object, nil)

    the authenticated identity or nil



210
211
212
213
214
215
# File 'lib/omniauth/strategies/identity.rb', line 210

def identity
  conditions = options[:locate_conditions]
  conditions = conditions.is_a?(Proc) ? instance_exec(request, &conditions).to_hash : conditions.to_hash

  @identity ||= model.authenticate(conditions, request.params["password"])
end

#infoHash

Returns the info hash for the authenticated identity.

Returns:

  • (Hash)


191
# File 'lib/omniauth/strategies/identity.rb', line 191

info { identity.info }

#modelClass

Returns the model class to use for identities.

Returns:

  • (Class)

    the identity model class



220
221
222
# File 'lib/omniauth/strategies/identity.rb', line 220

def model
  options[:model] || ::Identity
end

#on_registration_path?true, false

Checks if the current request is for the registration path.

Returns:

  • (true, false)


203
204
205
# File 'lib/omniauth/strategies/identity.rb', line 203

def on_registration_path?
  on_path?(registration_path)
end

#other_phasevoid

This method returns an undefined value.

Handles other phases like registration.

Routes to registration or login based on the path and options.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/omniauth/strategies/identity.rb', line 122

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 globally would affect the security of all
    #   other OmniAuth strategies that are present, we do not ask users to modify that setting.
    # Instead, we use this hook 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) ⇒ Rack::Response

Shows the registration form or calls the custom on_registration handler.

Parameters:

  • validation_message (String, nil) (defaults to: nil)

    message to display if validation failed

Returns:

  • (Rack::Response)

    the response to send



147
148
149
150
151
152
153
# File 'lib/omniauth/strategies/identity.rb', line 147

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_pathString

Returns the path for registration.

Returns:

  • (String)

    the registration path



196
197
198
# File 'lib/omniauth/strategies/identity.rb', line 196

def registration_path
  options[:registration_path] || "#{script_name}#{path_prefix}/#{name}/register"
end

#registration_phasevoid

This method returns an undefined value.

Handles the registration phase.

Creates a new identity and saves it.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/omniauth/strategies/identity.rb', line 160

def registration_phase
  attributes = (options[:fields] + DEFAULT_REGISTRATION_FIELDS).each_with_object({}) do |k, h|
    h[k] = request.params[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_phaseRack::Response

Handles the initial request phase.

Shows the login form or calls the custom on_login handler.

Returns:

  • (Rack::Response)

    the response to send



98
99
100
101
102
103
104
# File 'lib/omniauth/strategies/identity.rb', line 98

def request_phase
  if options[:on_login]
    options[:on_login].call(env)
  else
    .to_response
  end
end

#uidString

Returns the unique identifier for the authenticated identity.

Returns:

  • (String)


186
# File 'lib/omniauth/strategies/identity.rb', line 186

uid { identity.uid }