merb-ssl-requirement 0.0.1 → 0.0.2

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 CHANGED
@@ -40,5 +40,18 @@ inclusion, it'll add the before filter that validates the declarations. Some
40
40
  times you'll want to run other before filters before that. They should then be
41
41
  declared ahead of including this module.
42
42
 
43
+ Update Sat Feb 20, 2010: Added the ability to set a configuration parameter where you can specify which environments you want to exclude merb-ssl-requirement from enforcing ssl. This might be useful for testing and development environments where you don't have ssl certs in place. If you don't provide the configuration parameter or its value, merb-ssl-requirement still continues to function as expected.
44
+
45
+ Example:
46
+
47
+ If you want to work in the development environment and you don't want ssl enforced then you can do the following in your-merb-app-base-dir/config/init.rb:
48
+
49
+ Merb::Config.use do |c|
50
+ c[:ssl_requirement_excluded_environments] = ["development"]
51
+ end
52
+
53
+ Other environments, will continue to enforce ssl without being effected.
54
+
43
55
  Copyright (c) 2005 David Heinemeier Hansson, released under the MIT license
44
- Copyright (c) 2008 Steve Tooke
56
+ Copyright (c) 2008 Steve Tooke
57
+ Copyright (c) 2010 Lang Riley
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'merb-core'
5
5
  require 'merb-core/tasks/merb'
6
6
 
7
7
  GEM_NAME = "merb-ssl-requirement"
8
- GEM_VERSION = "0.0.1"
8
+ GEM_VERSION = "0.0.2"
9
9
  AUTHOR = "Steve Tooke"
10
10
  EMAIL = "steve.tooke@gmail.com"
11
11
  SUMMARY = "Merb plugin that provides ssl_requirement from rails"
@@ -48,4 +48,4 @@ task :gemspec do
48
48
  File.open("#{GEM_NAME}.gemspec", "w") do |file|
49
49
  file.puts spec.to_ruby
50
50
  end
51
- end
51
+ end
@@ -1,5 +1,6 @@
1
1
  # Copyright (c) 2005 David Heinemeier Hansson
2
2
  # Copyright (c) 2008 Steve Tooke
3
+ # Copyright (c) 2010 Lang Riley
3
4
  #
4
5
  # Permission is hereby granted, free of charge, to any person obtaining
5
6
  # a copy of this software and associated documentation files (the
@@ -28,12 +29,10 @@ module SslRequirement
28
29
  module ClassMethods
29
30
  # Specifies that the named actions requires an SSL connection to be performed (which is enforced by ensure_proper_protocol).
30
31
  def ssl_required(*actions)
31
- # write_inheritable_array(:ssl_required_actions, actions)
32
32
  self.ssl_required_actions.push(*actions)
33
33
  end
34
34
 
35
35
  def ssl_allowed(*actions)
36
- # write_inheritable_array(:ssl_allowed_actions, actions)
37
36
  self.ssl_allowed_actions.push(*actions)
38
37
  end
39
38
 
@@ -47,25 +46,43 @@ module SslRequirement
47
46
  end
48
47
 
49
48
  protected
50
- # Returns true if the current action is supposed to run as SSL
49
+ # Returns true if the current action is supposed to run as SSL and
50
+ # the application configuration (see README) has not specified the
51
+ # current environment to be exempt from ssl-requirement
52
+ # enforcement
51
53
  def ssl_required?
52
- # (self.class.read_inheritable_attribute(:ssl_required_actions) || []).include?(action_name.to_sym)
53
- self.class.ssl_required_actions.include?(action_name.to_sym)
54
+
55
+ if exclude_ssl_requirement?
56
+ false
57
+ else
58
+ self.class.ssl_required_actions.include?(action_name.to_sym)
59
+ end
60
+
54
61
  end
55
62
 
56
63
  def ssl_allowed?
57
64
  self.class.ssl_allowed_actions.include?(action_name.to_sym)
58
- # (self.class.read_inheritable_attribute(:ssl_allowed_actions) || []).include?(action_name.to_sym)
59
65
  end
60
66
 
61
67
  private
62
68
  def ensure_proper_protocol
63
69
  return true if ssl_allowed?
64
-
70
+
65
71
  if ssl_required? && !request.ssl?
66
72
  throw :halt, redirect("https://" + request.host + request.uri)
67
73
  elsif request.ssl? && !ssl_required?
68
74
  throw :halt, redirect("http://" + request.host + request.uri)
69
75
  end
70
76
  end
71
- end
77
+
78
+ def exclude_ssl_requirement?
79
+
80
+ if Merb::Config.key?(:ssl_requirement_excluded_environments) and Merb::Config[:ssl_requirement_excluded_environments]
81
+ Merb::Config[:ssl_requirement_excluded_environments].include?(Merb.env)
82
+ else
83
+ false
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -23,4 +23,5 @@ class Secure < Merb::Controller
23
23
  # def set_flash
24
24
  # flash[:foo] = "bar"
25
25
  # end
26
- end
26
+ end
27
+
@@ -25,6 +25,7 @@ describe "ssl_required" do
25
25
  controller.headers['Location'].should match(%r{^https://})
26
26
  end
27
27
 
28
+
28
29
  it "should allow https connection to required actions" do
29
30
  dispatch_to(Secure, :a, {}, 'HTTPS' => 'on').body.should == "a"
30
31
  end
@@ -40,4 +41,50 @@ describe "non-ssl actions" do
40
41
  controller.should redirect
41
42
  controller.headers['Location'].should match(%r{^http://})
42
43
  end
43
- end
44
+ end
45
+
46
+
47
+ describe "ssl_required behavior taking into account configuration" do
48
+ before(:each) do
49
+ Merb::Config.use do |c|
50
+ c[:ssl_requirement_excluded_environments] = ["test"]
51
+ end
52
+ end
53
+
54
+ it "should verify ability to set configuration parameter :ssl_requirement_excluded_environments in test environment" do
55
+ Merb::Config.key?(:ssl_requirement_excluded_environments).should be_true
56
+ Merb::Config[:ssl_requirement_excluded_environments].should == ["test"]
57
+ end
58
+
59
+
60
+ it "should not require ssl if the application configuration specifies the test environment as an environment excluded from enforcement" do
61
+ controller = dispatch_to(Secure, :a, {}, 'HTTPS' => nil)
62
+ controller.should_not redirect
63
+ end
64
+
65
+ it "should require ssl if the configuration does not specify :ssl_requirement_excluded_environments" do
66
+ Merb::Config.delete(:ssl_requirement_excluded_environments)
67
+ Merb::Config.key?(:ssl_requirement_excluded_environments).should be_false
68
+ controller = dispatch_to(Secure, :a, {}, 'HTTPS' => nil)
69
+ controller.should redirect
70
+ controller.headers['Location'].should match(%r{^https://})
71
+ end
72
+
73
+ it "should require ssl if the configuration does specify :ssl_requirement_excluded_environments, but provides not initialized value" do
74
+ Merb::Config[:ssl_requirement_excluded_environments] = nil
75
+ controller = dispatch_to(Secure, :a, {}, 'HTTPS' => nil)
76
+ controller.should redirect
77
+ controller.headers['Location'].should match(%r{^https://})
78
+ end
79
+
80
+ it "should require ssl if the configuration specifies an an environment, in :ssl_requirement_excluded_environments, that does not include 'test'" do
81
+ Merb::Config[:ssl_requirement_excluded_environments] = ["staging", "development"]
82
+ controller = dispatch_to(Secure, :a, {}, 'HTTPS' => nil)
83
+ controller.should redirect
84
+ controller.headers['Location'].should match(%r{^https://})
85
+ end
86
+
87
+
88
+
89
+ end
90
+
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb-ssl-requirement
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Steve Tooke
7
+ - Steve Tooke m7d
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
@@ -69,6 +69,6 @@ rubyforge_project: merb
69
69
  rubygems_version: 1.3.5
70
70
  signing_key:
71
71
  specification_version: 2
72
- summary: Merb plugin that provides ssl_requirement from rails
72
+ summary: Merb plugin that provides ssl_requirement from rails but ensures http protocol when in test or development mode to make it easier to develop and test
73
73
  test_files: []
74
74