# Exteding/Enhancing SSL Requirement plugin

**URL:** <https://www.ruby-forum.com/t/exteding-enhancing-ssl-requirement-plugin/181228>\
**Category:** Rails\
**Created:** [January 7, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/exteding-enhancing-ssl-requirement-plugin/181228 "2010-01-07T19:02:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [January 7, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/exteding-enhancing-ssl-requirement-plugin/181228/1 "2010-01-07T19:02:38Z")

</div>

I need to use SSL for a certain controller in my app. My app adapts  
style and some content from the domain being called via request.host.  
All of the domains are served up from apache-passenger vhost.

[domain1.com](http://domain1.com) is blue  
[domain2.com](http://domain2.com) is red etc etc

However, in order to keep to a single SSL certificate I only want to  
use one domain for port 443, again pointing to the same app  
directory. So I would like the following redirects to work correctly:

[http://domain1.com/pay/2](http://domain1.com/pay/2) redirect to [https://secure.domain1.com/pay/2](https://secure.domain1.com/pay/2)  
[http://domain2.com/pay/2](http://domain2.com/pay/2) redirect to [https://secure.domain1.com/pay/2](https://secure.domain1.com/pay/2)

- note it is still on [domain1.com](http://domain1.com)

# Copyright (c) 2005 David Heinemeier H.

module SslRequirement  
def self.included(controller)  
controller.extend(ClassMethods)  
controller.before\_filter(:ensure\_proper\_protocol)  
end

module ClassMethods  
# Specifies that the named actions requires an SSL connection to  
be performed (which is enforced by ensure\_proper\_protocol).  
def ssl\_required(\*actions)  
write\_inheritable\_array(:ssl\_required\_actions, actions)  
end

```
def ssl_allowed(*actions)
  write_inheritable_array(:ssl_allowed_actions, actions)
end

```

end

protected  
# Returns true if the current action is supposed to run as SSL  
def ssl\_required?  
(self.class.read\_inheritable\_attribute(:ssl\_required\_actions) ||  
[]).include?(action\_name.to\_sym)  
end

```
def ssl_allowed?
  (self.class.read_inheritable_attribute(:ssl_allowed_actions) ||

```

[]).include?(action\_name.to\_sym)  
end

private  
def ensure\_proper\_protocol  
return true if ssl\_allowed?

```
  if ssl_required? && !request.ssl?
    redirect_to "https://" + request.host + request.request_uri
    flash.keep
    return false
  elsif request.ssl? && !ssl_required?
    redirect_to "http://" + request.host + request.request_uri
    flash.keep
    return false
  end
end

```

end

The obvious thing would be to change the code in the plugin from  
redirect\_to “https://” + request.host + request.request\_uri  
to  
redirect\_to “https://” + “[secure.domain1.com](http://secure.domain1.com)” +  
request.request\_uri /\* use a app config parameter not hardcode

However, is there a better way of re-implimenting this without  
changing the plugin through over-riding the method. I hate to change  
the code of a plugin as in my past coding experience this always  
causes plugin upgrade issues.

Also, if I do such a redirect, would my session information be  
maintained? If so I can still use my skinning logic.

Thanks,

O.
