Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Scoped failure app #33

Closed
wants to merge 2 commits into from

2 participants

Didier Lafforgue José Valim
Didier Lafforgue
did commented

Add tests for this functionality + fix tests suite for ruby 1.9.2 (replace lambda{} by proc {}).

José Valim
Collaborator

There is one issue in this implementation. Warden users do not have access to all the places we call throw(:warden), so they would be able to pass the failure app option in just some occasions. The best implementation would probably be to add a scoped failure_app configuration and check it before calling the failure app. But that would also require the user to always pass the scope when calling throw(:warden).

José Valim josevalim closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 6, 2011
  1. first implementation without tests

    did authored
Commits on Nov 7, 2011
This page is out of date. Refresh to see the latest.
2  Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- warden (1.0.7)
+ warden (1.1.0)
rack (>= 1.0)
GEM
2  lib/warden.rb
View
@@ -1,4 +1,6 @@
# encoding: utf-8
+puts "hello world from Warden"
+
require 'forwardable'
require 'warden/mixins/common'
7 lib/warden/manager.rb
View
@@ -21,6 +21,7 @@ def initialize(app, options={})
@app, @config = app, Warden::Config.new(options)
@config.default_strategies *default_strategies if default_strategies
yield @config if block_given?
+
self
end
@@ -112,13 +113,15 @@ def process_unauthenticated(env, options={})
# The before_failure hooks are run on each failure
# :api: private
def call_failure_app(env, options = {})
- if config.failure_app
+ failure_app = options[:failure_app] || config.failure_app
+
+ if failure_app
options.merge!(:attempted_path => ::Rack::Request.new(env).fullpath)
env["PATH_INFO"] = "/#{options[:action]}"
env["warden.options"] = options
_run_callbacks(:before_failure, env, options)
- config.failure_app.call(env).to_a
+ failure_app.call(env).to_a
else
raise "No Failure App provided"
end
2  lib/warden/version.rb
View
@@ -1,4 +1,4 @@
# encoding: utf-8
module Warden
- VERSION = "1.1.0".freeze
+ VERSION = "1.1.0"#.freeze
end
2  spec/helpers/request_helper.rb
View
@@ -15,7 +15,7 @@ def setup_rack(app = nil, opts = {}, &block)
opts[:failure_app] ||= failure_app
opts[:default_strategies] ||= [:password]
opts[:default_serializers] ||= [:session]
- blk = opts[:configurator] || lambda{}
+ blk = opts[:configurator] || proc{}
Rack::Builder.new do
use opts[:session] || Warden::Spec::Helpers::Session
27 spec/warden/manager_spec.rb
View
@@ -59,6 +59,31 @@
result.last.should == ["Failure App"]
end
+ it "should render the default failure app when there's a failure and no scoped failure app" do
+ app = lambda do |e|
+ throw(:warden, :action => :unauthenticated, :failure_app => nil) unless e['warden'].authenticated?(:failz)
+ end
+ fail_app = lambda do |e|
+ [401, {"Content-Type" => "text/plain"}, ["Failure App"]]
+ end
+ result = setup_rack(app, :failure_app => fail_app).call(env_with_params)
+ result.last.should == ["Failure App"]
+ end
+
+ it "should render the scoped failure app when there's a failure and a defined scoped failure app" do
+ fail_app = lambda do |e|
+ [401, {"Content-Type" => "text/plain"}, ["Failure App"]]
+ end
+ custom_fail_app = lambda do |e|
+ [401, {"Content-Type" => "text/plain"}, ["Custom Failure App"]]
+ end
+ app = lambda do |e|
+ throw(:warden, :action => :unauthenticated, :scope => :custom, :failure_app => custom_fail_app) unless e['warden'].authenticated?(:failz)
+ end
+ result = setup_rack(app, :failure_app => fail_app).call(env_with_params)
+ result.last.should == ["Custom Failure App"]
+ end
+
it "should call failure app if warden is thrown even after successful authentication" do
env = env_with_params("/", {})
app = lambda do |env|
@@ -132,7 +157,7 @@ def call(env)
action = nil
failure = lambda do |env|
- action = env['PATH_INFO']
+ action = env['PATH_INFO']
[401, {}, ['fail']]
end
21 spec/warden/proxy_spec.rb
View
@@ -864,6 +864,27 @@ def wrap_app(app, &blk)
request.path.should == "/some_bad_action"
end
+ it "should allow me to set a failure_app for a given scope" do
+ $captures = []
+ builder = Rack::Builder.new do
+ use Warden::Manager do |config|
+ config.scope_defaults :foo,
+ :strategies => [:two],
+ :action => "some_bad_action",
+ :failure_app => lambda{ |e| [401, { "Content-Type" => "text/plain"}, ["You Fail Smoothly!"]] }
+ config.failure_app = Warden::Spec::Helpers::FAILURE_APP
+ end
+
+ run(lambda do |e|
+ e['warden'].authenticate!(:scope => :foo)
+ end)
+ end
+
+ env = env_with_params("/foo")
+ env["rack.session"] = {}
+ builder.to_app.call(env).last.should == ["You Fail Smoothly!"]
+ end
+
it "should allow me to set store, false on a given scope" do
$captures = []
builder = Rack::Builder.new do
Something went wrong with that request. Please try again.