ssl_enforcer 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile CHANGED
@@ -1,8 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- group :development do
4
- gem "rspec", "~> 2.3.0"
5
- gem "bundler", "~> 1.0.0"
6
- gem "jeweler", "~> 1.6.4"
7
- gem "simplecov", ">= 0"
8
- end
3
+ # Specify your gem's dependencies in ssl_enforcer.gemspec
4
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2012 Digital Opera
1
+ Copyright (c) 2013 Digital Opera, LLC (www.digitalopera.com)
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -17,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
21
  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.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -15,17 +15,17 @@ And then run `bundle install` to install it.
15
15
  To use SSL Enforcer after installing it open `config/environments/production.rb` and add the following line:
16
16
 
17
17
  ```rb
18
- # Force SSL Connections
19
- config.middleware.use SSLEnforcer
18
+ ## Force SSL Connections for all domains/subdomains
19
+ config.middleware.use SSLEnforcer::Enforcer
20
20
  ```
21
21
 
22
22
  This will force SSL connections for the entire application.
23
23
 
24
- If you would like to force SSL connections for a specific subdomain only, you can use the `:subdomain` option:
24
+ If you would like to force SSL connections for a specific subdomain only, you can use the `:only` option:
25
25
 
26
26
  ```rb
27
- # Force SSL Connections
28
- config.middleware.use SSLEnforcer, :subdomain => %w( secure )
27
+ ## Force SSL Connections for specific subdomains
28
+ config.middleware.use SSLEnforcer::Enforcer, :only => [:secure]
29
29
  ```
30
30
 
31
31
  By specifying the *secure* subdomain as the one to force SSL on, all connections to "https://secure.mydomain.com" will be forced redirected using a 301 redirect to "https://secure.mydomain.com". Connections to "http://www.mydomain.com/" would be left untouched.
@@ -33,13 +33,25 @@ By specifying the *secure* subdomain as the one to force SSL on, all connections
33
33
  If you have several different subdomains in your application that you would like to force SSL connections on, you can do so by providing an array of the subdomains to be enforced.
34
34
 
35
35
  ```rb
36
- # Force SSL Connections
37
- config.middleware.use SSLEnforcer, :subdomain => %w( secure checkout protected)
36
+ # Force SSL Connections on multiple subdomains
37
+ config.middleware.use SSLEnforcer::Enforcer, :only => [:secure, :checkout, :protected]
38
38
  ```
39
39
 
40
40
  This will enforce SSL connections for "secure.mydomain.com", as well as "checkout.mydomain.com" and "protected.mydomain.com".
41
41
 
42
- *NOTE* SSL Enforcer will not provide SSL certificates. You should first configure your server environment to properly support SSL connections before using SSL Enforcer.
42
+ ## Subdomain Exceptions
43
+
44
+ You can specify subdomain exceptions that should not be forced to HTTPS. This can be handy in the event that you application supports "wildcard" subdomains. You may want to force SSL
45
+ on all of the application subdomains, except say your public website "www". You can do this using the `:except` option:
46
+
47
+ ```rb
48
+ ## Force SSL connections for all requests except the website
49
+ config.middleware.use SSLEnforcer::Enforcer, :except => [:www]
50
+ ```
51
+
52
+ SSL Enforcer will not provide SSL certificates. You should first configure your server environment to properly support SSL connections before using SSL Enforcer.
53
+
54
+ **NOTE** The `only` and `except` options must be passed an array.
43
55
 
44
56
  ## Bug Reports
45
57
 
data/Rakefile CHANGED
@@ -1,24 +1 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- gem.name = "ssl_enforcer"
17
- gem.homepage = "http://github.com/noiseunion/ssl_enforcer"
18
- gem.license = "MIT"
19
- gem.summary = "Force SSL for specific subdomains of your application"
20
- gem.description = "Simple Rack middleware for forcing SSL on specific subdomains of an application."
21
- gem.email = "jd@digitalopera.com"
22
- gem.authors = ["JD Hendrickson"]
23
- end
24
- Jeweler::RubygemsDotOrgTasks.new
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,76 @@
1
+ require "rack"
2
+ require "rack/request"
3
+
4
+ class SSLEnforcer::Enforcer
5
+ def initialize(app, options = {})
6
+ @options = { :only => [] }.merge(options)
7
+ @app = app
8
+ @only = @options[:only] || []
9
+ @exceptions = @options[:except] || []
10
+
11
+ ## Convert @only and @exception values to symbols
12
+ @exceptions.map!{ |sd| sd.downcase.to_sym }
13
+ @only.map!{ |sd| sd.downcase.to_sym }
14
+ end
15
+
16
+ def call(env)
17
+ # if the domain is already using SSL don't do anything
18
+ if url_is_ok?(env)
19
+ @app.call(env)
20
+ else
21
+ # if the domain is NOT currently using SSL then we need to redirect the request
22
+ req = Rack::Request.new(env)
23
+ headers = { "Location" => req.url.gsub(/^http:/, "https:"), "Content-Type"=>"text/plain" }
24
+
25
+ [301, headers, []]
26
+ end
27
+ end
28
+
29
+ private # -----------------------------------------------
30
+
31
+ def url_is_ok?(env)
32
+ tld = get_top_level_domain(env["SERVER_NAME"])
33
+ subdomain = get_subdomain(env["SERVER_NAME"], tld).downcase.to_sym
34
+
35
+ # Hack to deal with heroku redirect issues.
36
+ # http://rack.lighthouseapp.com/projects/22435/tickets/101
37
+ scheme = (env["SERVER_PORT"] == "443") ? :https : :http
38
+ #scheme = env["HTTP_X_FORWARDED_PROTO"] if env["HTTP_X_FORWARDED_PROTO"]
39
+
40
+ # If the "only" and or "exceptions" options have not been passed, then
41
+ # we want to force SSL on ALL subdomains
42
+ return false if @only.empty? && @exceptions.empty?
43
+
44
+ ## Return true if the subdomain is in in the "except" list
45
+ return true if @exceptions.include?(subdomain)
46
+
47
+ ## Return the current scheme test restuls if the
48
+ ## subdomain is in the "only" list
49
+ return scheme == :https if @only.include?(subdomain)
50
+
51
+ ## If the subdomain is not found in either @exceptions || @only
52
+ ## we must first check to see if the "only" option was passed.
53
+ ## If so, then we will return true and not change the request.
54
+ ## If the option was not passed, then we must assume to change all
55
+ ## subdomains NOT responded to by @exceptions
56
+ if @only.empty?
57
+ return scheme == :https
58
+ else
59
+ return true
60
+ end
61
+ end
62
+
63
+ # return the subdomain regardless of how many levels deep it is
64
+ def get_subdomain(server_name, tld)
65
+ return server_name.gsub /\.?#{tld}$/, ""
66
+ end
67
+
68
+ # We will break the server URL into separate parts, reverse them and then
69
+ # reconstruct the TLD (Top Level Domain) from there. This should allow
70
+ # for support of domains that extend beyond a third level
71
+ # (i.e. - secure.my.domain.com)
72
+ def get_top_level_domain(server_name)
73
+ domain_parts = server_name.split(".").reverse
74
+ return [domain_parts.second, domain_parts.first].join(".")
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module SSLEnforcer
2
+ VERSION = "0.2.0"
3
+ end
data/lib/ssl_enforcer.rb CHANGED
@@ -1,56 +1,5 @@
1
- require "rack"
2
- require "rack/request"
1
+ require "ssl_enforcer/version"
2
+ require "ssl_enforcer/enforcer"
3
3
 
4
- class SSLEnforcer
5
- def initialize(app, options = {})
6
- @options = { :subdomain => [] }.merge(options)
7
- @app = app
8
- @subdomains = @options[:subdomain]
9
- end
10
-
11
- def call(env)
12
- # if the domain is already using SSL don't do anything
13
- if url_is_ok?(env)
14
- @app.call(env)
15
- else # if the domain is NOT currently using SSL then we need to redirect the request
16
- req = Rack::Request.new(env)
17
- headers = { "Location" => req.url.gsub(/^http:/, "https:") }
18
-
19
- [301, headers, []]
20
- end
21
- end
22
-
23
- private # -----------------------------------------------------------------
24
-
25
- def url_is_ok?(env)
26
- tld = get_top_level_domain(env["SERVER_NAME"])
27
- subdomain = get_subdomain(env["SERVER_NAME"], tld)
28
-
29
- # Hack to deal with heroku redirect issues.
30
- # http://rack.lighthouseapp.com/projects/22435/tickets/101
31
- scheme = "https" if env["SERVER_PORT"] == "443"
32
- scheme = env["HTTP_X_FORWARDED_PROTO"] if env["HTTP_X_FORWARDED_PROTO"]
33
-
34
- # If the subdomain is in the list of HTTPS enforced subs, check for HTTPS
35
- # otherwise, return true
36
- if @subdomains.index(subdomain).nil?
37
- return true
38
- else
39
- return scheme == "https"
40
- end
41
- end
42
-
43
- # return the subdomain regardless of how many levels deep it is
44
- def get_subdomain(server_name, tld)
45
- return server_name.gsub /\.?#{tld}$/, ""
46
- end
47
-
48
- # We will break the server URL into separate parts, reverse them and then
49
- # reconstruct the TLD (Top Level Domain) from there. This should allow
50
- # for support of domains that extend beyond a third level
51
- # (i.e. - secure.my.domain.com)
52
- def get_top_level_domain(server_name)
53
- domain_parts = server_name.split(".").reverse
54
- return [domain_parts.second, domain_parts.first].join(".")
55
- end
4
+ module SSLEnforcer
56
5
  end
data/ssl_enforcer.gemspec CHANGED
@@ -1,60 +1,19 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ssl_enforcer/version'
5
5
 
6
- Gem::Specification.new do |s|
7
- s.name = "ssl_enforcer"
8
- s.version = "0.1.4"
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ssl_enforcer"
8
+ gem.version = SSLEnforcer::VERSION
9
+ gem.authors = ["JD Hendrickson"]
10
+ gem.email = ["jd@digitalopera.com"]
11
+ gem.description = "Simple Rack middleware for forcing SSL on specific subdomains of an application."
12
+ gem.summary = "Simple Rack middleware for forcing SSL on specific subdomains of an application."
13
+ gem.homepage = "https://github.com/digitalopera/ssl_enforcer/"
9
14
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["JD Hendrickson"]
12
- s.date = "2012-04-09"
13
- s.description = "Simple Rack middleware for forcing SSL on specific subdomains of an application."
14
- s.email = "jd@digitalopera.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.md"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "Gemfile",
23
- "Gemfile.lock",
24
- "LICENSE.txt",
25
- "README.md",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/ssl_enforcer.rb",
29
- "spec/spec_helper.rb",
30
- "spec/ssl_enforcer_spec.rb",
31
- "ssl_enforcer.gemspec"
32
- ]
33
- s.homepage = "http://github.com/noiseunion/ssl_enforcer"
34
- s.licenses = ["MIT"]
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = "1.8.21"
37
- s.summary = "Force SSL for specific subdomains of your application"
38
-
39
- if s.respond_to? :specification_version then
40
- s.specification_version = 3
41
-
42
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
44
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
46
- s.add_development_dependency(%q<simplecov>, [">= 0"])
47
- else
48
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
49
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
51
- s.add_dependency(%q<simplecov>, [">= 0"])
52
- end
53
- else
54
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
55
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
57
- s.add_dependency(%q<simplecov>, [">= 0"])
58
- end
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
59
19
  end
60
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssl_enforcer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,95 +9,26 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-09 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 2.3.0
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 2.3.0
30
- - !ruby/object:Gem::Dependency
31
- name: bundler
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 1.0.0
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 1.0.0
46
- - !ruby/object:Gem::Dependency
47
- name: jeweler
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 1.6.4
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 1.6.4
62
- - !ruby/object:Gem::Dependency
63
- name: simplecov
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
+ dependencies: []
78
14
  description: Simple Rack middleware for forcing SSL on specific subdomains of an application.
79
- email: jd@digitalopera.com
15
+ email:
16
+ - jd@digitalopera.com
80
17
  executables: []
81
18
  extensions: []
82
- extra_rdoc_files:
83
- - LICENSE.txt
84
- - README.md
19
+ extra_rdoc_files: []
85
20
  files:
86
- - .document
87
- - .rspec
21
+ - .gitignore
88
22
  - Gemfile
89
- - Gemfile.lock
90
23
  - LICENSE.txt
91
24
  - README.md
92
25
  - Rakefile
93
- - VERSION
94
26
  - lib/ssl_enforcer.rb
95
- - spec/spec_helper.rb
96
- - spec/ssl_enforcer_spec.rb
27
+ - lib/ssl_enforcer/enforcer.rb
28
+ - lib/ssl_enforcer/version.rb
97
29
  - ssl_enforcer.gemspec
98
- homepage: http://github.com/noiseunion/ssl_enforcer
99
- licenses:
100
- - MIT
30
+ homepage: https://github.com/digitalopera/ssl_enforcer/
31
+ licenses: []
101
32
  post_install_message:
102
33
  rdoc_options: []
103
34
  require_paths:
@@ -108,9 +39,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
39
  - - ! '>='
109
40
  - !ruby/object:Gem::Version
110
41
  version: '0'
111
- segments:
112
- - 0
113
- hash: -3686267842636745665
114
42
  required_rubygems_version: !ruby/object:Gem::Requirement
115
43
  none: false
116
44
  requirements:
@@ -119,8 +47,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
47
  version: '0'
120
48
  requirements: []
121
49
  rubyforge_project:
122
- rubygems_version: 1.8.21
50
+ rubygems_version: 1.8.24
123
51
  signing_key:
124
52
  specification_version: 3
125
- summary: Force SSL for specific subdomains of your application
53
+ summary: Simple Rack middleware for forcing SSL on specific subdomains of an application.
126
54
  test_files: []
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
data/Gemfile.lock DELETED
@@ -1,32 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- diff-lcs (1.1.3)
5
- git (1.2.5)
6
- jeweler (1.6.4)
7
- bundler (~> 1.0)
8
- git (>= 1.2.5)
9
- rake
10
- multi_json (1.2.0)
11
- rake (0.9.2.2)
12
- rspec (2.3.0)
13
- rspec-core (~> 2.3.0)
14
- rspec-expectations (~> 2.3.0)
15
- rspec-mocks (~> 2.3.0)
16
- rspec-core (2.3.1)
17
- rspec-expectations (2.3.0)
18
- diff-lcs (~> 1.1.2)
19
- rspec-mocks (2.3.0)
20
- simplecov (0.6.1)
21
- multi_json (~> 1.0)
22
- simplecov-html (~> 0.5.3)
23
- simplecov-html (0.5.3)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- bundler (~> 1.0.0)
30
- jeweler (~> 1.6.4)
31
- rspec (~> 2.3.0)
32
- simplecov
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.4
data/spec/spec_helper.rb DELETED
@@ -1,12 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'rspec'
4
- require 'ssl_enforcer'
5
-
6
- # Requires supporting files with custom matchers and macros, etc,
7
- # in ./support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
-
10
- RSpec.configure do |config|
11
-
12
- end
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "SslEnforcer" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end