Class: Lockie::FailureApp

Inherits:
ActionController::Metal
  • Object
show all
Includes:
ActionController::Redirecting
Defined in:
lib/lockie/failure_app.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(*args) ⇒ Object



8
9
10
11
# File 'lib/lockie/failure_app.rb', line 8

def self.call(*args)
  req = ActionDispatch::Request.new(*args)
  action(req.env['warden.options'][:action]).call(*args)
end

Instance Method Details

#api_response(format = :text) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/lockie/failure_app.rb', line 23

def api_response(format = :text)
  self.status = 401
  self.content_type = request.format.to_s
  if format.eql?(:text)
    self.response_body  = message
  else
    self.response_body  = { message: message }.to_json
  end
end

#html_responseObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lockie/failure_app.rb', line 33

def html_response
  flash[type] = message if message
  self.status = 302
  if Lockie.config.callback_url
    uri = URI(warden_options[:unauthenticated_path] || Lockie.config.unauthenticated_path)
    # only add callback_url if original path is not the same with login path
    unless request.original_fullpath == uri.path
      callback_url = request.base_url + request.original_fullpath
      uri.query = (uri.query.to_s.split("&") << "callback_url=#{ callback_url }").join("&")
    end
    redirect_to uri.to_s
  else
    redirect_to Lockie.config.unauthenticated_path
  end
end

#messageObject



49
50
51
# File 'lib/lockie/failure_app.rb', line 49

def message
  @message ||= request.env['warden.message'] 
end

#typeObject



57
58
59
# File 'lib/lockie/failure_app.rb', line 57

def type
  @type ||= warden_options[:type] || :error
end

#unauthenticatedObject



13
14
15
16
17
18
19
20
21
# File 'lib/lockie/failure_app.rb', line 13

def unauthenticated
  if request.xhr?
    api_response(:text)
  elsif request.format.to_sym == :json || request.content_type.to_s.split("/").last == 'json'
    api_response(:json)
  else
    html_response
  end
end

#wardenObject



61
62
63
# File 'lib/lockie/failure_app.rb', line 61

def warden
  request.env['warden']
end

#warden_optionsObject



53
54
55
# File 'lib/lockie/failure_app.rb', line 53

def warden_options
  request.env['warden.options']
end