Class: OmniAuth::Strategies::Identity
- Inherits:
-
Object
- Object
- OmniAuth::Strategies::Identity
- 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.
Constant Summary collapse
- DEFAULT_REGISTRATION_FIELDS =
Default fields required for registration.
%i[password password_confirmation].freeze
Instance Attribute Summary collapse
-
#create_identity_link_text ⇒ String
Text for the link to create a new identity.
-
#DEFAULT_REGISTRATION_FIELDS ⇒ Array<Symbol>
readonly
Default fields required for registration.
-
#enable_login ⇒ true, false
Whether to enable login functionality.
-
#enable_registration ⇒ true, false
Whether to enable user registration functionality.
-
#fields ⇒ Array<Symbol>
The fields to collect for user registration.
-
#locate_conditions ⇒ Proc, Hash
Conditions for locating an identity during login.
-
#on_failed_registration ⇒ Proc?
Custom handler for failed registration.
-
#on_login ⇒ Proc?
Custom login handler.
-
#on_registration ⇒ Proc?
Custom registration handler.
-
#on_validation ⇒ Proc?
Custom validation handler for registration.
-
#registration_failure_message ⇒ String
Message to display on registration failure.
-
#registration_form_title ⇒ String
Title for the registration form.
-
#title ⇒ String
Title for the login form.
-
#validation_failure_message ⇒ String
Message to display on validation failure.
Instance Method Summary collapse
-
#callback_phase ⇒ void
Handles the callback phase after login.
-
#identity ⇒ Object?
Finds and authenticates the identity based on the request parameters.
-
#info ⇒ Hash
Returns the info hash for the authenticated identity.
-
#model ⇒ Class
Returns the model class to use for identities.
-
#on_registration_path? ⇒ true, false
Checks if the current request is for the registration path.
-
#other_phase ⇒ void
Handles other phases like registration.
-
#registration_form(validation_message = nil) ⇒ Rack::Response
Shows the registration form or calls the custom on_registration handler.
-
#registration_path ⇒ String
Returns the path for registration.
-
#registration_phase ⇒ void
Handles the registration phase.
-
#request_phase ⇒ Rack::Response
Handles the initial request phase.
-
#uid ⇒ String
Returns the unique identifier for the authenticated identity.
Instance Attribute Details
#create_identity_link_text ⇒ String
Text for the link to create a new identity.
71 |
# File 'lib/omniauth/strategies/identity.rb', line 71 option :create_identity_link_text, "Create an Identity" |
#DEFAULT_REGISTRATION_FIELDS ⇒ Array<Symbol> (readonly)
Default fields required for registration.
25 |
# File 'lib/omniauth/strategies/identity.rb', line 25 DEFAULT_REGISTRATION_FIELDS = %i[password password_confirmation].freeze |
#enable_login ⇒ true, false
Whether to enable login functionality.
41 |
# File 'lib/omniauth/strategies/identity.rb', line 41 option :enable_login, true |
#enable_registration ⇒ true, false
Whether to enable user registration functionality.
36 |
# File 'lib/omniauth/strategies/identity.rb', line 36 option :enable_registration, true |
#fields ⇒ Array<Symbol>
The fields to collect for user registration.
31 |
# File 'lib/omniauth/strategies/identity.rb', line 31 option :fields, %i[name email] |
#locate_conditions ⇒ Proc, Hash
Conditions for locating an identity during login.
66 |
# File 'lib/omniauth/strategies/identity.rb', line 66 option :locate_conditions, ->(req) { {model.auth_key => req.params["auth_key"]} } |
#on_failed_registration ⇒ Proc?
Custom handler for failed registration.
61 |
# File 'lib/omniauth/strategies/identity.rb', line 61 option :on_failed_registration, nil |
#on_login ⇒ Proc?
Custom login handler. If provided, called instead of showing the default login form.
46 |
# File 'lib/omniauth/strategies/identity.rb', line 46 option :on_login, nil |
#on_registration ⇒ Proc?
Custom registration handler. If provided, called instead of showing the default registration form.
56 |
# File 'lib/omniauth/strategies/identity.rb', line 56 option :on_registration, nil |
#on_validation ⇒ Proc?
Custom validation handler for registration.
51 |
# File 'lib/omniauth/strategies/identity.rb', line 51 option :on_validation, nil |
#registration_failure_message ⇒ String
Message to display on registration failure.
76 |
# File 'lib/omniauth/strategies/identity.rb', line 76 option :registration_failure_message, "One or more fields were invalid" |
#registration_form_title ⇒ String
Title for the registration form.
91 |
# File 'lib/omniauth/strategies/identity.rb', line 91 option :registration_form_title, "Register Identity" |
#title ⇒ String
Title for the login form.
86 |
# File 'lib/omniauth/strategies/identity.rb', line 86 option :title, "Identity Verification" |
#validation_failure_message ⇒ String
Message to display on validation failure.
81 |
# File 'lib/omniauth/strategies/identity.rb', line 81 option :validation_failure_message, "Validation failed" |
Instance Method Details
#callback_phase ⇒ void
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 |
#identity ⇒ Object?
Finds and authenticates the identity based on the request parameters.
210 211 212 213 214 215 |
# File 'lib/omniauth/strategies/identity.rb', line 210 def identity conditions = [:locate_conditions] conditions = conditions.is_a?(Proc) ? instance_exec(request, &conditions).to_hash : conditions.to_hash @identity ||= model.authenticate(conditions, request.params["password"]) end |
#info ⇒ Hash
Returns the info hash for the authenticated identity.
191 |
# File 'lib/omniauth/strategies/identity.rb', line 191 info { identity.info } |
#model ⇒ Class
Returns the model class to use for identities.
220 221 222 |
# File 'lib/omniauth/strategies/identity.rb', line 220 def model [:model] || ::Identity end |
#on_registration_path? ⇒ true, false
Checks if the current request is for the registration path.
203 204 205 |
# File 'lib/omniauth/strategies/identity.rb', line 203 def on_registration_path? on_path?(registration_path) end |
#other_phase ⇒ void
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 [:enable_registration] && on_registration_path? if request.get? registration_form elsif request.post? registration_phase else call_app! end elsif [: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.
147 148 149 150 151 152 153 |
# File 'lib/omniauth/strategies/identity.rb', line 147 def registration_form( = nil) if [:on_registration] [:on_registration].call(env) else build_omniauth_registration_form().to_response end end |
#registration_path ⇒ String
Returns the path for registration.
196 197 198 |
# File 'lib/omniauth/strategies/identity.rb', line 196 def registration_path [:registration_path] || "#{script_name}#{path_prefix}/#{name}/register" end |
#registration_phase ⇒ void
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 = ([: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([:validation_failure_message]) end else @identity = model.create(attributes) env["omniauth.identity"] = @identity registration_result end end |
#request_phase ⇒ Rack::Response
Handles the initial request phase.
Shows the login form or calls the custom on_login handler.
98 99 100 101 102 103 104 |
# File 'lib/omniauth/strategies/identity.rb', line 98 def request_phase if [:on_login] [:on_login].call(env) else build_omniauth_login_form.to_response end end |
#uid ⇒ String
Returns the unique identifier for the authenticated identity.
186 |
# File 'lib/omniauth/strategies/identity.rb', line 186 uid { identity.uid } |