From: Norbert Buchmuller Date: Mon, 6 Jul 2009 21:45:40 +0000 (+0000) Subject: Implemented $c->allow_ssl. X-Git-Tag: v0.07~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-RequireSSL.git;a=commitdiff_plain;h=c4744895212a6af6afeb79ad1d144be5a2e6c1ff Implemented $c->allow_ssl. --- diff --git a/lib/Catalyst/Plugin/RequireSSL.pm b/lib/Catalyst/Plugin/RequireSSL.pm index bf665b5..6aafbc4 100644 --- a/lib/Catalyst/Plugin/RequireSSL.pm +++ b/lib/Catalyst/Plugin/RequireSSL.pm @@ -6,7 +6,7 @@ use NEXT; our $VERSION = '0.07'; -__PACKAGE__->mk_accessors( qw/_require_ssl _ssl_strip_output/ ); +__PACKAGE__->mk_accessors( qw/_require_ssl _allow_ssl _ssl_strip_output/ ); sub require_ssl { my $c = shift; @@ -25,6 +25,12 @@ sub require_ssl { } } +sub allow_ssl { + my $c = shift; + + $c->_allow_ssl(1); +} + sub finalize { my $c = shift; @@ -44,7 +50,7 @@ sub finalize { # we're already required to be in SSL for this request last REDIRECT if $c->_require_ssl; # or the user doesn't want us to redirect - last REDIRECT if $c->config->{require_ssl}->{remain_in_ssl}; + last REDIRECT if $c->config->{require_ssl}->{remain_in_ssl} || $c->_allow_ssl; $c->res->redirect( $c->_redirect_uri('http') ); } @@ -187,6 +193,16 @@ Call require_ssl in any controller method you wish to be secured. The browser will be redirected to the same path on your SSL server. POST requests are never redirected. +=head2 allow_ssl + +Call allow_ssl in any controller method you wish to access both in SSL and +non-SSL mode. + + $c->allow_ssl; + +The browser will not be redirected, independently of whether the request was +made to the SSL or non-SSL server. + =head2 setup Disables this plugin if running under an engine which does not support SSL. diff --git a/t/04ssl.t b/t/04ssl.t index a120471..83fcfcc 100644 --- a/t/04ssl.t +++ b/t/04ssl.t @@ -6,7 +6,7 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 15; +use Test::More tests => 19; use Catalyst::Test 'TestApp'; use HTTP::Request::Common; @@ -20,6 +20,10 @@ isnt( $res->content, 'Secured', 'no content displayed on secure page, ok' ); ok( $res = request('http://localhost/ssl/secured?a=2&a=1&b=3&c=4'), 'request ok' ); is( $res->header('location'), 'https://localhost/ssl/secured?a=1&a=2&b=3&c=4', 'redirect with params ok' ); +# test that it does not redirect for actions where SSL mode is optional +ok( my $res = request('http://localhost/ssl/maybe_secured'), 'request ok' ); +is( $res->code, 200, 'no redirect for optional SSL action' ); + # test that it doesn't redirect on POST my $request = POST( 'http://localhost/ssl/secured', 'Content' => '', @@ -45,5 +49,9 @@ SKIP: # test redirection params ok( $res = request('https://localhost/ssl/unsecured?a=2&a=1&b=3&c=4'), 'request ok' ); is( $res->header('location'), 'http://localhost/ssl/unsecured?a=1&a=2&b=3&c=4', 'redirect with params ok' ); + + # test that it does not redirect for actions where SSL mode is optional + ok( $res = request('https://localhost/ssl/maybe_secured'), 'request ok' ); + is( $res->code, 200, 'no redirect for optional SSL action' ); } diff --git a/t/lib/TestApp/C/SSL.pm b/t/lib/TestApp/C/SSL.pm index 6faeddf..18f67e0 100644 --- a/t/lib/TestApp/C/SSL.pm +++ b/t/lib/TestApp/C/SSL.pm @@ -17,4 +17,12 @@ sub unsecured : Local { $c->res->output( 'Unsecured' ); } +sub maybe_secured : Local { + my ( $self, $c ) = @_; + + $c->allow_ssl; + + $c->res->output( 'Maybe secured' ); +} + 1;