rack-ssl-enforcer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +31 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/rack-ssl-enforcer.rb +25 -0
- data/rack-ssl-enforcer.gemspec +53 -0
- data/test/helper.rb +11 -0
- data/test/test_rack-ssl-enforcer.rb +63 -0
- metadata +84 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Tobias Matthies
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
= rack-ssl-enforcer
|
|
2
|
+
|
|
3
|
+
Rack::SslEnforcer is a simple Rack middleware to enforce ssl connections
|
|
4
|
+
|
|
5
|
+
== Installation
|
|
6
|
+
|
|
7
|
+
sudo gem install rack-ssl-enforcer -s http://gemcutter.org
|
|
8
|
+
|
|
9
|
+
== Usage
|
|
10
|
+
|
|
11
|
+
require 'noie'
|
|
12
|
+
use Rack::SslEnforcer
|
|
13
|
+
|
|
14
|
+
This will redirect all plain-text requests to SSL. Rack::SslEnforcer accepts two params, :redirect_to and :message
|
|
15
|
+
You might need the :redirect_to option if the URL can't be determined (e.g. if using a proxy).
|
|
16
|
+
|
|
17
|
+
use Rack::SslEnforcer, :redirect_to => 'https://example.org', :message => 'R-R-R-Redirecting...'
|
|
18
|
+
|
|
19
|
+
== Note on Patches/Pull Requests
|
|
20
|
+
|
|
21
|
+
* Fork the project.
|
|
22
|
+
* Make your feature addition or bug fix.
|
|
23
|
+
* Add tests for it. This is important so I don't break it in a
|
|
24
|
+
future version unintentionally.
|
|
25
|
+
* Commit, do not mess with rakefile, version, or history.
|
|
26
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
|
27
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
28
|
+
|
|
29
|
+
== Copyright
|
|
30
|
+
|
|
31
|
+
Copyright (c) 2010 Tobias Matthies. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'jeweler'
|
|
6
|
+
Jeweler::Tasks.new do |gem|
|
|
7
|
+
gem.name = "rack-ssl-enforcer"
|
|
8
|
+
gem.summary = %Q{A simple Rack middleware to enforce SSL}
|
|
9
|
+
gem.email = "tm@mit2m.de"
|
|
10
|
+
gem.homepage = "http://github.com/tobmatth/rack-ssl-enforcer"
|
|
11
|
+
gem.authors = ["Tobias Matthies"]
|
|
12
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
14
|
+
end
|
|
15
|
+
Jeweler::GemcutterTasks.new
|
|
16
|
+
rescue LoadError
|
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
require 'rake/testtask'
|
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
|
22
|
+
test.libs << 'lib' << 'test'
|
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
|
24
|
+
test.verbose = true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
begin
|
|
28
|
+
require 'rcov/rcovtask'
|
|
29
|
+
Rcov::RcovTask.new do |test|
|
|
30
|
+
test.libs << 'test'
|
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
|
32
|
+
test.verbose = true
|
|
33
|
+
end
|
|
34
|
+
rescue LoadError
|
|
35
|
+
task :rcov do
|
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
task :test => :check_dependencies
|
|
41
|
+
|
|
42
|
+
task :default => :test
|
|
43
|
+
|
|
44
|
+
require 'rake/rdoctask'
|
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
47
|
+
|
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
49
|
+
rdoc.title = "rack-ssl-enforcer #{version}"
|
|
50
|
+
rdoc.rdoc_files.include('README*')
|
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
52
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Rack
|
|
2
|
+
|
|
3
|
+
class SslEnforcer
|
|
4
|
+
|
|
5
|
+
def initialize(app, options = {})
|
|
6
|
+
@app = app
|
|
7
|
+
@options = options
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call(env)
|
|
11
|
+
@options[:redirect_to] ||= Rack::Request.new(env).url
|
|
12
|
+
@options[:redirect_to].gsub!(/^http:/,'https:')
|
|
13
|
+
@options[:message] ||= "You are beeing redirected to #{@options[:redirect_to]}."
|
|
14
|
+
ssl_request?(env) ? @app.call(env) : [301, {'Location' => @options[:redirect_to]}, @options[:message]]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def ssl_request?(env)
|
|
21
|
+
env['rack.url_scheme'] == 'https'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = %q{rack-ssl-enforcer}
|
|
8
|
+
s.version = "0.1.0"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Tobias Matthies"]
|
|
12
|
+
s.date = %q{2010-03-17}
|
|
13
|
+
s.email = %q{tm@mit2m.de}
|
|
14
|
+
s.extra_rdoc_files = [
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.rdoc"
|
|
17
|
+
]
|
|
18
|
+
s.files = [
|
|
19
|
+
".document",
|
|
20
|
+
".gitignore",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.rdoc",
|
|
23
|
+
"Rakefile",
|
|
24
|
+
"VERSION",
|
|
25
|
+
"lib/rack-ssl-enforcer.rb",
|
|
26
|
+
"rack-ssl-enforcer.gemspec",
|
|
27
|
+
"test/helper.rb",
|
|
28
|
+
"test/test_rack-ssl-enforcer.rb"
|
|
29
|
+
]
|
|
30
|
+
s.homepage = %q{http://github.com/tobmatth/rack-ssl-enforcer}
|
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
32
|
+
s.require_paths = ["lib"]
|
|
33
|
+
s.rubygems_version = %q{1.3.6}
|
|
34
|
+
s.summary = %q{A simple Rack middleware to enforce SSL}
|
|
35
|
+
s.test_files = [
|
|
36
|
+
"test/helper.rb",
|
|
37
|
+
"test/test_rack-ssl-enforcer.rb"
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
if s.respond_to? :specification_version then
|
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
42
|
+
s.specification_version = 3
|
|
43
|
+
|
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
45
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
46
|
+
else
|
|
47
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
48
|
+
end
|
|
49
|
+
else
|
|
50
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
data/test/helper.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'test/unit'
|
|
3
|
+
require 'shoulda'
|
|
4
|
+
require 'rack/mock'
|
|
5
|
+
|
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
8
|
+
require 'rack-ssl-enforcer'
|
|
9
|
+
|
|
10
|
+
class Test::Unit::TestCase
|
|
11
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
class TestRackSslEnforcer < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def dummy_app(env)
|
|
6
|
+
[ 200, {'Content-Type' => 'text/plain'}, 'Hello world!' ]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
context 'Given an app' do
|
|
10
|
+
setup do
|
|
11
|
+
@app = method(:dummy_app)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context 'that has no :redirect_to set' do
|
|
15
|
+
setup do
|
|
16
|
+
@request = Rack::MockRequest.new(Rack::SslEnforcer.new(@app))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
should 'respond with a ssl redirect to plain-text requests' do
|
|
20
|
+
response = @request.get('http://www.example.org/', {})
|
|
21
|
+
assert_equal 301, response.status
|
|
22
|
+
assert_equal response.location, 'https://www.example.org/'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
should 'respond not redirect ssl requests' do
|
|
26
|
+
response = @request.get('https://www.example.org/', {})
|
|
27
|
+
assert_equal 200, response.status
|
|
28
|
+
assert_equal response.body, 'Hello world!'
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'that has :redirect_to set' do
|
|
33
|
+
setup do
|
|
34
|
+
@request = Rack::MockRequest.new(Rack::SslEnforcer.new(@app, :redirect_to => 'https://www.google.com/'))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
should 'respond with a ssl redirect to plain-text requests and redirect to :redirect_to' do
|
|
38
|
+
response = @request.get('http://www.example.org/', {})
|
|
39
|
+
assert_equal 301, response.status
|
|
40
|
+
assert_equal response.location, 'https://www.google.com/'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
should 'respond not redirect ssl requests' do
|
|
44
|
+
response = @request.get('https://www.example.org/', {})
|
|
45
|
+
assert_equal 200, response.status
|
|
46
|
+
assert_equal response.body, 'Hello world!'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context 'that has :message set' do
|
|
51
|
+
setup do
|
|
52
|
+
@message = 'R-R-R-Redirect!'
|
|
53
|
+
@request = Rack::MockRequest.new(Rack::SslEnforcer.new(@app, :message => @message))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
should 'output the given message when redirecting' do
|
|
57
|
+
response = @request.get('http://www.example.org/', {})
|
|
58
|
+
assert_equal 301, response.status
|
|
59
|
+
assert_equal response.body, @message
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rack-ssl-enforcer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 1
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.1.0
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Tobias Matthies
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2010-03-17 00:00:00 +01:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: thoughtbot-shoulda
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
segments:
|
|
28
|
+
- 0
|
|
29
|
+
version: "0"
|
|
30
|
+
type: :development
|
|
31
|
+
version_requirements: *id001
|
|
32
|
+
description:
|
|
33
|
+
email: tm@mit2m.de
|
|
34
|
+
executables: []
|
|
35
|
+
|
|
36
|
+
extensions: []
|
|
37
|
+
|
|
38
|
+
extra_rdoc_files:
|
|
39
|
+
- LICENSE
|
|
40
|
+
- README.rdoc
|
|
41
|
+
files:
|
|
42
|
+
- .document
|
|
43
|
+
- .gitignore
|
|
44
|
+
- LICENSE
|
|
45
|
+
- README.rdoc
|
|
46
|
+
- Rakefile
|
|
47
|
+
- VERSION
|
|
48
|
+
- lib/rack-ssl-enforcer.rb
|
|
49
|
+
- rack-ssl-enforcer.gemspec
|
|
50
|
+
- test/helper.rb
|
|
51
|
+
- test/test_rack-ssl-enforcer.rb
|
|
52
|
+
has_rdoc: true
|
|
53
|
+
homepage: http://github.com/tobmatth/rack-ssl-enforcer
|
|
54
|
+
licenses: []
|
|
55
|
+
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options:
|
|
58
|
+
- --charset=UTF-8
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
segments:
|
|
66
|
+
- 0
|
|
67
|
+
version: "0"
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
segments:
|
|
73
|
+
- 0
|
|
74
|
+
version: "0"
|
|
75
|
+
requirements: []
|
|
76
|
+
|
|
77
|
+
rubyforge_project:
|
|
78
|
+
rubygems_version: 1.3.6
|
|
79
|
+
signing_key:
|
|
80
|
+
specification_version: 3
|
|
81
|
+
summary: A simple Rack middleware to enforce SSL
|
|
82
|
+
test_files:
|
|
83
|
+
- test/helper.rb
|
|
84
|
+
- test/test_rack-ssl-enforcer.rb
|