Skip to content

Commit

Permalink
Add "force" option to configuration
Browse files Browse the repository at this point in the history
It skips the look up for an existing user and run strategies
  • Loading branch information
shatalov-boris committed Nov 14, 2021
1 parent 8b44aa4 commit ff3001a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
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[:force] && 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 "`force` 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, force: 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, force: 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, force: 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 `force` 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, force: false
config.scope_defaults :baz, force: 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, force: 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

0 comments on commit ff3001a

Please sign in to comment.