jcnetdev-ssl_requirement 1.0.200807043
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/README +43 -0
- data/init.rb +1 -0
- data/lib/ssl_requirement.rb +62 -0
- data/rails/init.rb +1 -0
- data/ssl_requirement.gemspec +27 -0
- data/test/ssl_requirement_test.rb +132 -0
- metadata +67 -0
data/README
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
SSL Requirement
|
2
|
+
===============
|
3
|
+
|
4
|
+
SSL requirement adds a declarative way of specifying that certain actions
|
5
|
+
should only be allowed to run under SSL, and if they're accessed without it,
|
6
|
+
they should be redirected.
|
7
|
+
|
8
|
+
Example:
|
9
|
+
|
10
|
+
class ApplicationController < ActiveRecord::Base
|
11
|
+
include SslRequirement
|
12
|
+
end
|
13
|
+
|
14
|
+
class AccountController < ApplicationController
|
15
|
+
ssl_required :signup, :payment
|
16
|
+
ssl_allowed :index
|
17
|
+
|
18
|
+
def signup
|
19
|
+
# Non-SSL access will be redirected to SSL
|
20
|
+
end
|
21
|
+
|
22
|
+
def payment
|
23
|
+
# Non-SSL access will be redirected to SSL
|
24
|
+
end
|
25
|
+
|
26
|
+
def index
|
27
|
+
# This action will work either with or without SSL
|
28
|
+
end
|
29
|
+
|
30
|
+
def other
|
31
|
+
# SSL access will be redirected to non-SSL
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
You can overwrite the protected method ssl_required? to rely on other things
|
36
|
+
than just the declarative specification. Say, only premium accounts get SSL.
|
37
|
+
|
38
|
+
P.S.: Beware when you include the SslRequirement module. At the time of
|
39
|
+
inclusion, it'll add the before_filter that validates the declarations. Some
|
40
|
+
times you'll want to run other before_filters before that. They should then be
|
41
|
+
declared ahead of including this module.
|
42
|
+
|
43
|
+
Copyright (c) 2005 David Heinemeier Hansson, released under the MIT license
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Copyright (c) 2005 David Heinemeier Hansson
|
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.
|
21
|
+
module SslRequirement
|
22
|
+
def self.included(controller)
|
23
|
+
controller.extend(ClassMethods)
|
24
|
+
controller.before_filter(:ensure_proper_protocol)
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
# Specifies that the named actions requires an SSL connection to be performed (which is enforced by ensure_proper_protocol).
|
29
|
+
def ssl_required(*actions)
|
30
|
+
write_inheritable_array(:ssl_required_actions, actions)
|
31
|
+
end
|
32
|
+
|
33
|
+
def ssl_allowed(*actions)
|
34
|
+
write_inheritable_array(:ssl_allowed_actions, actions)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
# Returns true if the current action is supposed to run as SSL
|
40
|
+
def ssl_required?
|
41
|
+
(self.class.read_inheritable_attribute(:ssl_required_actions) || []).include?(action_name.to_sym)
|
42
|
+
end
|
43
|
+
|
44
|
+
def ssl_allowed?
|
45
|
+
(self.class.read_inheritable_attribute(:ssl_allowed_actions) || []).include?(action_name.to_sym)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def ensure_proper_protocol
|
50
|
+
return true if ssl_allowed?
|
51
|
+
|
52
|
+
if ssl_required? && !request.ssl?
|
53
|
+
redirect_to "https://" + request.host + request.request_uri
|
54
|
+
flash.keep
|
55
|
+
return false
|
56
|
+
elsif request.ssl? && !ssl_required?
|
57
|
+
redirect_to "http://" + request.host + request.request_uri
|
58
|
+
flash.keep
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ssl_requirement'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'ssl_requirement'
|
3
|
+
s.version = '1.0.200807043'
|
4
|
+
s.date = '2008-07-04'
|
5
|
+
|
6
|
+
s.summary = "Allow controller actions to force SSL on specific parts of the site."
|
7
|
+
s.description = "SSL requirement adds a declarative way of specifying that certain actions should only be allowed to run under SSL, and if they're accessed without it, they should be redirected."
|
8
|
+
|
9
|
+
s.authors = ['RailsJedi', 'David Heinemeier Hansson']
|
10
|
+
s.email = 'railsjedi@gmail.com'
|
11
|
+
s.homepage = 'http://github.com/jcnetdev/ssl_requirement'
|
12
|
+
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.rdoc_options = ["--main", "README"]
|
15
|
+
s.extra_rdoc_files = ["README"]
|
16
|
+
|
17
|
+
s.add_dependency 'rails', ['>= 2.1']
|
18
|
+
|
19
|
+
s.files = ["README",
|
20
|
+
"init.rb",
|
21
|
+
"lib/ssl_requirement.rb",
|
22
|
+
"rails/init.rb",
|
23
|
+
"ssl_requirement.gemspec"]
|
24
|
+
|
25
|
+
s.test_files = ["test/ssl_requirement_test.rb"]
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
begin
|
2
|
+
require 'action_controller'
|
3
|
+
rescue LoadError
|
4
|
+
if ENV['ACTIONCONTROLLER_PATH'].nil?
|
5
|
+
abort <<MSG
|
6
|
+
Please set the ACTIONCONTROLLER_PATH environment variable to the directory
|
7
|
+
containing the action_controller.rb file.
|
8
|
+
MSG
|
9
|
+
else
|
10
|
+
$LOAD_PATH.unshift << ENV['ACTIONCONTROLLER_PATH']
|
11
|
+
begin
|
12
|
+
require 'action_controller'
|
13
|
+
rescue LoadError
|
14
|
+
abort "ActionController could not be found."
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'action_controller/test_process'
|
20
|
+
require 'test/unit'
|
21
|
+
require "#{File.dirname(__FILE__)}/../lib/ssl_requirement"
|
22
|
+
|
23
|
+
ActionController::Base.logger = nil
|
24
|
+
ActionController::Routing::Routes.reload rescue nil
|
25
|
+
|
26
|
+
class SslRequirementController < ActionController::Base
|
27
|
+
include SslRequirement
|
28
|
+
|
29
|
+
ssl_required :a, :b
|
30
|
+
ssl_allowed :c
|
31
|
+
|
32
|
+
def a
|
33
|
+
render :nothing => true
|
34
|
+
end
|
35
|
+
|
36
|
+
def b
|
37
|
+
render :nothing => true
|
38
|
+
end
|
39
|
+
|
40
|
+
def c
|
41
|
+
render :nothing => true
|
42
|
+
end
|
43
|
+
|
44
|
+
def d
|
45
|
+
render :nothing => true
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_flash
|
49
|
+
flash[:foo] = "bar"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class SslRequirementTest < Test::Unit::TestCase
|
54
|
+
def setup
|
55
|
+
@controller = SslRequirementController.new
|
56
|
+
@request = ActionController::TestRequest.new
|
57
|
+
@response = ActionController::TestResponse.new
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_redirect_to_https_preserves_flash
|
61
|
+
get :set_flash
|
62
|
+
get :b
|
63
|
+
assert_response :redirect
|
64
|
+
assert_equal "bar", flash[:foo]
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_not_redirecting_to_https_does_not_preserve_the_flash
|
68
|
+
get :set_flash
|
69
|
+
get :d
|
70
|
+
assert_response :success
|
71
|
+
assert_nil flash[:foo]
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_redirect_to_http_preserves_flash
|
75
|
+
get :set_flash
|
76
|
+
@request.env['HTTPS'] = "on"
|
77
|
+
get :d
|
78
|
+
assert_response :redirect
|
79
|
+
assert_equal "bar", flash[:foo]
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_not_redirecting_to_http_does_not_preserve_the_flash
|
83
|
+
get :set_flash
|
84
|
+
@request.env['HTTPS'] = "on"
|
85
|
+
get :a
|
86
|
+
assert_response :success
|
87
|
+
assert_nil flash[:foo]
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_required_without_ssl
|
91
|
+
assert_not_equal "on", @request.env["HTTPS"]
|
92
|
+
get :a
|
93
|
+
assert_response :redirect
|
94
|
+
assert_match %r{^https://}, @response.headers['Location']
|
95
|
+
get :b
|
96
|
+
assert_response :redirect
|
97
|
+
assert_match %r{^https://}, @response.headers['Location']
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_required_with_ssl
|
101
|
+
@request.env['HTTPS'] = "on"
|
102
|
+
get :a
|
103
|
+
assert_response :success
|
104
|
+
get :b
|
105
|
+
assert_response :success
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_disallowed_without_ssl
|
109
|
+
assert_not_equal "on", @request.env["HTTPS"]
|
110
|
+
get :d
|
111
|
+
assert_response :success
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_disallowed_with_ssl
|
115
|
+
@request.env['HTTPS'] = "on"
|
116
|
+
get :d
|
117
|
+
assert_response :redirect
|
118
|
+
assert_match %r{^http://}, @response.headers['Location']
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_allowed_without_ssl
|
122
|
+
assert_not_equal "on", @request.env["HTTPS"]
|
123
|
+
get :c
|
124
|
+
assert_response :success
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_allowed_with_ssl
|
128
|
+
@request.env['HTTPS'] = "on"
|
129
|
+
get :c
|
130
|
+
assert_response :success
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jcnetdev-ssl_requirement
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.200807043
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- RailsJedi
|
8
|
+
- David Heinemeier Hansson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-07-04 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rails
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "2.1"
|
24
|
+
version:
|
25
|
+
description: SSL requirement adds a declarative way of specifying that certain actions should only be allowed to run under SSL, and if they're accessed without it, they should be redirected.
|
26
|
+
email: railsjedi@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
files:
|
34
|
+
- README
|
35
|
+
- init.rb
|
36
|
+
- lib/ssl_requirement.rb
|
37
|
+
- rails/init.rb
|
38
|
+
- ssl_requirement.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/jcnetdev/ssl_requirement
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.2.0
|
63
|
+
signing_key:
|
64
|
+
specification_version: 2
|
65
|
+
summary: Allow controller actions to force SSL on specific parts of the site.
|
66
|
+
test_files:
|
67
|
+
- test/ssl_requirement_test.rb
|