rack-ssl-enforcer 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -3
- data/lib/{rack-ssl-enforcer → rack/ssl-enforcer}/version.rb +1 -1
- data/lib/rack/ssl-enforcer.rb +47 -0
- data/lib/rack-ssl-enforcer.rb +1 -48
- metadata +25 -6
data/README.rdoc
CHANGED
@@ -4,17 +4,18 @@ Rack::SslEnforcer is a simple Rack middleware to enforce ssl connections
|
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
7
|
-
|
7
|
+
gem install rack-ssl-enforcer
|
8
8
|
|
9
9
|
== Usage
|
10
10
|
|
11
11
|
require 'rack-ssl-enforcer'
|
12
12
|
use Rack::SslEnforcer
|
13
13
|
|
14
|
-
This will redirect all
|
14
|
+
This will redirect all requests to SSL. Rack::SslEnforcer accepts params:
|
15
|
+
|
15
16
|
You might need the :redirect_to option if the requested URL can't be determined (e.g. if using a proxy).
|
16
17
|
|
17
|
-
use Rack::SslEnforcer, :redirect_to => 'https://example.org'
|
18
|
+
use Rack::SslEnforcer, :redirect_to => 'https://example.org'
|
18
19
|
|
19
20
|
You can also define specifics regex pattern or path to redirect.
|
20
21
|
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Rack
|
2
|
+
class SslEnforcer
|
3
|
+
|
4
|
+
def initialize(app, options = {})
|
5
|
+
@app, @options = app, options
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
@req = Rack::Request.new(env)
|
10
|
+
if enforce_ssl?(env)
|
11
|
+
scheme = 'https' unless ssl_request?(env)
|
12
|
+
elsif ssl_request?(env) && @options[:strict]
|
13
|
+
scheme = 'http'
|
14
|
+
end
|
15
|
+
|
16
|
+
if scheme
|
17
|
+
location = @options[:redirect_to] || @req.url.gsub(/^https?/, scheme)
|
18
|
+
body = "<html><body>You are being <a href=\"#{location}\">redirected</a>.</body></html>"
|
19
|
+
[301, { 'Content-Type' => 'text/html', 'Location' => location }, [body]]
|
20
|
+
else
|
21
|
+
@app.call(env)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def ssl_request?(env)
|
28
|
+
(env['HTTP_X_FORWARDED_PROTO'] || @req.scheme) == 'https'
|
29
|
+
end
|
30
|
+
|
31
|
+
def enforce_ssl?(env)
|
32
|
+
if @options[:only]
|
33
|
+
rules = [@options[:only]].flatten
|
34
|
+
rules.any? do |pattern|
|
35
|
+
if pattern.is_a?(Regexp)
|
36
|
+
@req.path =~ pattern
|
37
|
+
else
|
38
|
+
@req.path[0,pattern.length] == pattern
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
data/lib/rack-ssl-enforcer.rb
CHANGED
@@ -1,48 +1 @@
|
|
1
|
-
|
2
|
-
class SslEnforcer
|
3
|
-
|
4
|
-
def initialize(app, options = {})
|
5
|
-
@app, @options = app, options
|
6
|
-
end
|
7
|
-
|
8
|
-
def call(env)
|
9
|
-
if enforce_ssl?(env)
|
10
|
-
scheme = 'https' unless ssl_request?(env)
|
11
|
-
elsif ssl_request?(env) && @options[:strict]
|
12
|
-
scheme = 'http'
|
13
|
-
end
|
14
|
-
|
15
|
-
if scheme
|
16
|
-
@options[:redirect_to] ||= Rack::Request.new(env).url
|
17
|
-
@options[:redirect_to].gsub!(/^#{scheme == "https" ? 'http' : 'https'}:/, "#{scheme}:")
|
18
|
-
@options[:message] ||= "You are being redirected to #{@options[:redirect_to]}."
|
19
|
-
[301, { 'Location' => @options[:redirect_to] }, [@options[:message]].flatten]
|
20
|
-
else
|
21
|
-
@app.call(env)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def ssl_request?(env)
|
28
|
-
(env['HTTP_X_FORWARDED_PROTO'] || env['rack.url_scheme']) == 'https'
|
29
|
-
end
|
30
|
-
|
31
|
-
def enforce_ssl?(env)
|
32
|
-
path = env['PATH_INFO']
|
33
|
-
if @options[:only]
|
34
|
-
rules = [@options[:only]].flatten
|
35
|
-
rules.any? do |pattern|
|
36
|
-
if pattern.is_a?(Regexp)
|
37
|
-
path =~ pattern
|
38
|
-
else
|
39
|
-
path[0,pattern.length] == pattern
|
40
|
-
end
|
41
|
-
end
|
42
|
-
else
|
43
|
-
true
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
1
|
+
require 'rack/ssl-enforcer'
|
metadata
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-ssl-enforcer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 8
|
10
|
+
version: 0.1.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tobias Matthies
|
14
|
+
- Thibaud Guillaume-Gentil
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-09-
|
19
|
+
date: 2010-09-10 00:00:00 +02:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
@@ -24,7 +25,7 @@ dependencies:
|
|
24
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
26
|
none: false
|
26
27
|
requirements:
|
27
|
-
- -
|
28
|
+
- - ~>
|
28
29
|
- !ruby/object:Gem::Version
|
29
30
|
hash: 23
|
30
31
|
segments:
|
@@ -66,9 +67,26 @@ dependencies:
|
|
66
67
|
version: 1.2.0
|
67
68
|
type: :development
|
68
69
|
version_requirements: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rack-test
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
- 5
|
82
|
+
- 4
|
83
|
+
version: 0.5.4
|
84
|
+
type: :development
|
85
|
+
version_requirements: *id004
|
69
86
|
description: Rack::SslEnforcer is a simple Rack middleware to enforce ssl connections
|
70
87
|
email:
|
71
88
|
- tm@mit2m.de
|
89
|
+
- thibaud@thibaud.me
|
72
90
|
executables: []
|
73
91
|
|
74
92
|
extensions: []
|
@@ -76,7 +94,8 @@ extensions: []
|
|
76
94
|
extra_rdoc_files: []
|
77
95
|
|
78
96
|
files:
|
79
|
-
- lib/rack
|
97
|
+
- lib/rack/ssl-enforcer/version.rb
|
98
|
+
- lib/rack/ssl-enforcer.rb
|
80
99
|
- lib/rack-ssl-enforcer.rb
|
81
100
|
- LICENSE
|
82
101
|
- README.rdoc
|