Class: Devise::FailureApp
- Inherits:
-
ActionController::Metal
- Object
- ActionController::Metal
- Devise::FailureApp
show all
- Includes:
- AbstractController::Callbacks, ActionController::Redirecting, ActionController::UrlFor, Controllers::StoreLocation
- Defined in:
- lib/devise/failure_app.rb
Overview
Failure application that will be called every time :warden is thrown from any strategy or hook. It is responsible for redirecting the user to the sign in page based on current scope and mapping. If no scope is given, it redirects to the default_url.
Class Method Summary
collapse
Instance Method Summary
collapse
#store_location_for, #stored_location_for
Class Method Details
permalink
.call(env) ⇒ Object
[View source] [
View on GitHub]
26
27
28
29
|
# File 'lib/devise/failure_app.rb', line 26
def self.call(env)
@respond ||= action(:respond)
@respond.call(env)
end
|
permalink
.default_url_options(*args) ⇒ Object
Try retrieving the URL options from the parent controller (usually ApplicationController). Instance methods are not supported at the moment, so only the class-level attribute is used.
[View source] [
View on GitHub]
34
35
36
37
38
39
40
|
# File 'lib/devise/failure_app.rb', line 34
def self.default_url_options(*args)
if defined?(Devise.parent_controller.constantize)
Devise.parent_controller.constantize.try(:default_url_options) || {}
else
{}
end
end
|
Instance Method Details
permalink
#attempted_path ⇒ Object
[View source] [
View on GitHub]
240
241
242
|
# File 'lib/devise/failure_app.rb', line 240
def attempted_path
warden_options[:attempted_path]
end
|
permalink
#http_auth ⇒ Object
[View source] [
View on GitHub]
52
53
54
55
56
57
|
# File 'lib/devise/failure_app.rb', line 52
def http_auth
self.status = 401
self.["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect}) if
self.content_type = request.format.to_s
self.response_body = http_auth_body
end
|
permalink
#http_auth? ⇒ Boolean
Choose whether we should respond in an HTTP authentication fashion, including 401 and optional headers.
This method allows the user to explicitly disable HTTP authentication on AJAX requests in case they want to redirect on failures instead of handling the errors on their own. This is useful in case your AJAX API is the same as your public API and uses a format like JSON (so you cannot mark JSON as a navigational format).
[View source] [
View on GitHub]
187
188
189
190
191
192
193
|
# File 'lib/devise/failure_app.rb', line 187
def http_auth?
if request.xhr?
Devise.http_authenticatable_on_xhr
else
!(request_format && is_navigational_format?)
end
end
|
permalink
#http_auth_body ⇒ Object
[View source] [
View on GitHub]
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/devise/failure_app.rb', line 201
def http_auth_body
return i18n_message unless request_format
method = "to_#{request_format}"
if method == "to_xml"
{ error: i18n_message }.to_xml(root: "errors")
elsif {}.respond_to?(method)
{ error: i18n_message }.send(method)
else
i18n_message
end
end
|
It doesn’t make sense to send authenticate headers in AJAX requests or if the user disabled them.
[View source] [
View on GitHub]
197
198
199
|
# File 'lib/devise/failure_app.rb', line 197
def
scope_class.http_authenticatable && !request.xhr?
end
|
permalink
#i18n_message(default = nil) ⇒ Object
[View source] [
View on GitHub]
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/devise/failure_app.rb', line 105
def i18n_message(default = nil)
message = warden_message || default || :unauthenticated
if message.is_a?(Symbol)
options = {}
options[:resource_name] = scope
options[:scope] = "devise.failure"
options[:default] = [message]
auth_keys = scope_class.authentication_keys
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key) }
options[:authentication_keys] = keys.join(I18n.t(:"support.array.words_connector"))
options = i18n_options(options)
I18n.t(:"#{scope}.#{message}", **options)
else
message.to_s
end
end
|
Check if flash messages should be emitted. Default is to do it on navigational formats
[View source] [
View on GitHub]
258
259
260
|
# File 'lib/devise/failure_app.rb', line 258
def is_flashing_format?
request.respond_to?(:flash) && is_navigational_format?
end
|
[View source] [
View on GitHub]
252
253
254
|
# File 'lib/devise/failure_app.rb', line 252
def is_navigational_format?
Devise.navigational_formats.include?(request_format)
end
|
[View source] [
View on GitHub]
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/devise/failure_app.rb', line 59
def recall
= if relative_url_root?
base_path = Pathname.new(relative_url_root)
full_path = Pathname.new(attempted_path)
{ "SCRIPT_NAME" => relative_url_root,
"PATH_INFO" => '/' + full_path.relative_path_from(base_path).to_s }
else
{ "PATH_INFO" => attempted_path }
end
.each do | var, value|
if request.respond_to?(:set_header)
request.(var, value)
else
request.env[var] = value
end
end
flash.now[:alert] = i18n_message(:invalid) if is_flashing_format?
self.response = recall_app(warden_options[:recall]).call(request.env).tap { |response|
response[0] = Rack::Utils.status_code(
response[0].in?(300..399) ? Devise.responder.redirect_status : Devise.responder.error_status
)
}
end
|
permalink
#recall_app(app) ⇒ Object
[View source] [
View on GitHub]
213
214
215
216
217
218
|
# File 'lib/devise/failure_app.rb', line 213
def recall_app(app)
controller, action = app.split("#")
controller_name = ActiveSupport::Inflector.camelize(controller)
controller_klass = ActiveSupport::Inflector.constantize("#{controller_name}Controller")
controller_klass.action(action)
end
|
permalink
#redirect ⇒ Object
[View source] [
View on GitHub]
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/devise/failure_app.rb', line 86
def redirect
store_location!
if is_flashing_format?
if flash[:timedout] && flash[:alert]
flash.keep(:timedout)
flash.keep(:alert)
else
flash[:alert] = i18n_message
end
end
redirect_to redirect_url
end
|
permalink
#redirect_url ⇒ Object
[View source] [
View on GitHub]
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/devise/failure_app.rb', line 128
def redirect_url
if warden_message == :timeout
flash[:timedout] = true if is_flashing_format?
path = if request.get?
attempted_path
else
request.referrer
end
path || scope_url
else
scope_url
end
end
|
permalink
#relative_url_root ⇒ Object
[View source] [
View on GitHub]
266
267
268
269
270
271
272
|
# File 'lib/devise/failure_app.rb', line 266
def relative_url_root
@relative_url_root ||= begin
config = Rails.application.config
config.try(:relative_url_root) || config.action_controller.try(:relative_url_root)
end
end
|
permalink
#relative_url_root? ⇒ Boolean
[View source] [
View on GitHub]
274
275
276
|
# File 'lib/devise/failure_app.rb', line 274
def relative_url_root?
relative_url_root.present?
end
|
[View source] [
View on GitHub]
262
263
264
|
# File 'lib/devise/failure_app.rb', line 262
def request_format
@request_format ||= request.format.try(:ref)
end
|
[View source] [
View on GitHub]
42
43
44
45
46
47
48
49
50
|
# File 'lib/devise/failure_app.rb', line 42
def respond
if http_auth?
http_auth
elsif warden_options[:recall]
recall
else
redirect
end
end
|
[View source] [
View on GitHub]
232
233
234
|
# File 'lib/devise/failure_app.rb', line 232
def scope
@scope ||= warden_options[:scope] || Devise.default_scope
end
|
permalink
#scope_url ⇒ Object
[View source] [
View on GitHub]
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/devise/failure_app.rb', line 148
def scope_url
opts = {}
opts[:script_name] = nil
route = route(scope)
opts[:format] = request_format unless skip_format?
router_name = Devise.mappings[scope].router_name || Devise.available_router_name
context = send(router_name)
if relative_url_root?
opts[:script_name] = relative_url_root
end
if context.respond_to?(route)
context.send(route, opts)
elsif respond_to?(:root_url)
root_url(opts)
else
"/"
end
end
|
[View source] [
View on GitHub]
175
176
177
|
# File 'lib/devise/failure_app.rb', line 175
def skip_format?
%w(html */* turbo_stream).include? request_format.to_s
end
|
permalink
#store_location! ⇒ Object
Stores requested URI to redirect the user after signing in. We can’t use the scoped session provided by warden here, since the user is not authenticated yet, but we still need to store the URI based on scope, so different scopes would never use the same URI to redirect.
[View source] [
View on GitHub]
248
249
250
|
# File 'lib/devise/failure_app.rb', line 248
def store_location!
store_location_for(scope, attempted_path) if request.get? && !http_auth?
end
|
[View source] [
View on GitHub]
220
221
222
|
# File 'lib/devise/failure_app.rb', line 220
def warden
request.respond_to?(:get_header) ? request.("warden") : request.env["warden"]
end
|
permalink
#warden_message ⇒ Object
[View source] [
View on GitHub]
228
229
230
|
# File 'lib/devise/failure_app.rb', line 228
def warden_message
@message ||= warden.message || warden_options[:message]
end
|
permalink
#warden_options ⇒ Object
[View source] [
View on GitHub]
224
225
226
|
# File 'lib/devise/failure_app.rb', line 224
def warden_options
request.respond_to?(:get_header) ? request.("warden.options") : request.env["warden.options"]
end
|