Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "ignore_session" option to configuration #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/warden/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _perform_authentication(*args)

# Look for an existing user in the session for this scope.
# If there was no user in the session, see if we can get one from the request.
return user, opts if user = user(opts.merge(:scope => scope))
return user, opts if !opts[:ignore_session] && user = user(opts.merge(:scope => scope))
_run_strategies_for(scope, args)

if winning_strategy && winning_strategy.successful?
Expand Down
72 changes: 72 additions & 0 deletions spec/warden/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,38 @@
raise "Expected a cookie to not be sent or session id to match"
end
end

describe "`ignore_session` option provided" do
it "should not look for an active user in the session with authenticate" do
app = lambda do |env|
env['rack.session']["warden.user.default.key"] = "foo as a user"
env['warden'].authenticate(:pass, ignore_session: true)
valid_response
end
setup_rack(app).call(@env)
expect(@env['warden'].user).to eq("Valid User")
end

it "should not look for an active user in the session with authenticate?" do
app = lambda do |env|
env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
env['warden'].authenticate?(:pass, scope: :foo_scope, ignore_session: true)
valid_response
end
setup_rack(app).call(@env)
expect(@env['warden'].user(:foo_scope)).to eq("Valid User")
end

it "should not look for an active user in the session with authenticate!" do
app = lambda do |env|
env['rack.session']['warden.user.foo_scope.key'] = "a foo user"
env['warden'].authenticate!(:pass, scope: :foo_scope, ignore_session: true)
valid_response
end
setup_rack(app).call(@env)
expect(@env['warden'].user(:foo_scope)).to eq("Valid User")
end
end
end

describe "authentication cache" do
Expand Down Expand Up @@ -1052,6 +1084,46 @@ def wrap_app(app, &blk)
expect(session['warden.user.bar.key']).to be_nil
expect(session['warden.user.baz.key']).to eq("User")
end

it "should allow me to set `ignore_session` on a given scope" do
$captures = []
warden = []
builder = Rack::Builder.new do
use Warden::Manager do |config|
config.default_strategies :one
config.default_strategies :two, :one, scope: :foo
config.default_strategies :two, :one, scope: :bar

config.scope_defaults :bar, ignore_session: false
config.scope_defaults :baz, ignore_session: true
config.failure_app = Warden::Spec::Helpers::FAILURE_APP
end
run(lambda do |e|
w = e['warden']
w.authenticate
w.authenticate(scope: :foo)
w.authenticate(:one, scope: :bar, ignore_session: true)
w.authenticate(:one, scope: :baz)
warden << w
$captures << :complete
Rack::Response.new("OK").finish
end)
end
@env["rack.session"] = {
"warden.user.default.key" => "foo as a user",
"warden.user.foo.key" => "foo as a user",
"warden.user.bar.key" => "foo as a user",
"warden.user.baz.key" => "foo as a user"
}
builder.to_app.call(@env)

expect($captures).to include(:complete)
w = warden.first
expect(w.user).to eq("foo as a user")
expect(w.user(:foo)).to eq("foo as a user")
expect(w.user(:bar)).to eq("User")
expect(w.user(:baz)).to eq("User")
end
end

describe "#asset_request?" do
Expand Down