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

allow proc to be called before redirecting fixes #56 #57

Merged
merged 3 commits into from
Sep 6, 2013
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ config.middleware.use Rack::SslEnforcer, :only => "/login", :force_secure_cookie
But be aware that if you do so, you have to make sure that the content of you cookie is encoded.
This can be done using a coder with [Rack::Session::Cookie](https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L28-42).

### Running code before redirect

You may want to run some code before rack-ssl-enforcer forces a redirect. This code could do something like log that a redirect was done, or if this is used in a Rails app, keeping the flash when redirecting:


```ruby
config.middleware.use Rack::SslEnforcer, :only => '/login', :before_redirect => Proc.new {
#keep flash on redirect
@request.session[:flash].keep if [email protected]? && @request.session.key?('flash') && [email protected]['flash'].empty?
}
```


## Deployment

If you run your application behind a proxy (e.g. Nginx) you may need to do some configuration on that side. If you don't you may experience an infinite redirect loop.
Expand Down
10 changes: 8 additions & 2 deletions lib/rack/ssl-enforcer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def initialize(app, options={})
:hsts => nil,
:http_port => nil,
:https_port => nil,
:force_secure_cookies => true
:force_secure_cookies => true,
:before_redirect => nil
}
CONSTRAINTS_BY_TYPE.values.each do |constraints|
constraints.each { |constraint| default_options[constraint] = nil }
Expand All @@ -43,7 +44,8 @@ def call(env)
end

if redirect_required?
modify_location_and_redirect
call_before_redirect
modify_location_and_redirect
elsif ssl_request?
status, headers, body = @app.call(env)
flag_cookies_as_secure!(headers) if @options[:force_secure_cookies]
Expand Down Expand Up @@ -79,6 +81,10 @@ def host_mismatch?
destination_host && destination_host != @request.host
end

def call_before_redirect
@options[:before_redirect].call unless @options[:before_redirect].nil?
end

def modify_location_and_redirect
location = "#{current_scheme}://#{@request.host}#{@request.fullpath}"
location = replace_scheme(location, @scheme)
Expand Down
17 changes: 17 additions & 0 deletions test/rack-ssl-enforcer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ class TestRackSslEnforcer < Test::Unit::TestCase
end
end

context ":before_redirect" do
def self.startup
@before_redirect_called = false
end
setup { mock_app :redirect_to => 'https://www.google.com', :before_redirect => Proc.new {
@before_redirect_called = true
}}
should "call before_direct when redirecting" do
get 'http://www.google.com/'
assert @before_redirect_called, "before_redirect was not called"
end
should "not call before_direct when not redirecting" do
get 'https://www.google.com/'
refute @before_redirect_called, "before_redirect was called"
end
end

context ':redirect_code' do
setup { mock_app :redirect_code => 302 }

Expand Down