RequireSSL 0.05, skipped tests will now run if Catalyst >= 5.5
[catagits/Catalyst-Plugin-RequireSSL.git] / lib / Catalyst / Plugin / RequireSSL.pm
CommitLineData
1763fe29 1package Catalyst::Plugin::RequireSSL;
2
3use strict;
4use base qw/Class::Accessor::Fast/;
5use NEXT;
6
cc4f2717 7our $VERSION = '0.05';
1763fe29 8
4585dfb1 9__PACKAGE__->mk_accessors( qw/_require_ssl _ssl_strip_output/ );
eeefd598 10
11sub require_ssl {
12 my $c = shift;
13
14 $c->_require_ssl(1);
15
16 if ( !$c->req->secure && $c->req->method ne "POST" ) {
17 my $redir = $c->_redirect_uri('https');
18 if ( $c->config->{require_ssl}->{disabled} ) {
19 $c->log->warn( "RequireSSL: Would have redirected to $redir" );
20 }
21 else {
4585dfb1 22 $c->_ssl_strip_output(1);
eeefd598 23 $c->res->redirect( $redir );
24 }
25 }
26}
27
28sub finalize {
29 my $c = shift;
30
31 # Do not redirect static files (only works with Static::Simple)
32 if ( $c->isa( "Catalyst::Plugin::Static::Simple" ) ) {
33 return $c->NEXT::finalize(@_) if $c->_static_file;
34 }
35
36 # redirect back to non-SSL mode
37 REDIRECT:
38 {
39 # No redirect if:
40 # we're not in SSL mode
41 last REDIRECT if !$c->req->secure;
42 # it's a POST request
43 last REDIRECT if $c->req->method eq "POST";
44 # we're already required to be in SSL for this request
45 last REDIRECT if $c->_require_ssl;
46 # or the user doesn't want us to redirect
47 last REDIRECT if $c->config->{require_ssl}->{remain_in_ssl};
48
49 $c->res->redirect( $c->_redirect_uri('http') );
50 }
51
4585dfb1 52 # do not allow any output to be displayed on the insecure page
53 if ( $c->_ssl_strip_output ) {
cc4f2717 54 $c->res->body( '' );
4585dfb1 55 }
56
eeefd598 57 return $c->NEXT::finalize(@_);
58}
59
60sub setup {
61 my $c = shift;
62
63 $c->NEXT::setup(@_);
64
65 # disable the plugin when running under certain engines which don't
66 # support SSL
67 # XXX: I didn't include Catalyst::Engine::Server here as it may be used as
68 # a backend in a proxy setup.
69 if ( $c->engine eq "Catalyst::Engine::HTTP" ) {
70 $c->config->{require_ssl}->{disabled} = 1;
71 $c->log->warn( "RequireSSL: Disabling SSL redirection while running "
72 . "under " . $c->engine );
73 }
74}
75
76sub _redirect_uri {
77 my ( $c, $type ) = @_;
78
79 # XXX: Cat needs a $c->req->host method...
80 # until then, strip off the leading protocol from base
81 if ( !$c->config->{require_ssl}->{$type} ) {
82 my $host = $c->req->base;
83 $host =~ s/^http(s?):\/\///;
84 $c->config->{require_ssl}->{$type} = $host;
85 }
86
87 if ( $c->config->{require_ssl}->{$type} !~ /\/$/xms ) {
88 $c->config->{require_ssl}->{$type} .= '/';
89 }
90
91 my $redir
92 = $type . '://' . $c->config->{require_ssl}->{$type} . $c->req->path;
4585dfb1 93
eeefd598 94 if ( scalar $c->req->param ) {
4585dfb1 95 my @params;
96 foreach my $arg ( sort keys %{ $c->req->params } ) {
97 if ( ref $c->req->params->{$arg} ) {
98 my $list = $c->req->params->{$arg};
99 push @params, map { "$arg=" . $_ } sort @{$list};
100 }
101 else {
102 push @params, "$arg=" . $c->req->params->{$arg};
103 }
104 }
105 $redir .= '?' . join( '&', @params );
106 }
eeefd598 107
108 return $redir;
109}
110
1111;
112__END__
1763fe29 113
114=head1 NAME
115
116Catalyst::Plugin::RequireSSL - Force SSL mode on select pages
117
118=head1 SYNOPSIS
119
eeefd598 120 # in MyApp.pm
121 use Catalyst;
122 MyApp->setup( qw/RequireSSL/ );
1763fe29 123
124 MyApp->config->{require_ssl} = {
125 https => 'secure.mydomain.com',
126 http => 'www.mydomain.com',
127 remain_in_ssl => 0,
128 };
129
eeefd598 130 # in any controller methods that should be secured
1763fe29 131 $c->require_ssl;
132
133=head1 DESCRIPTION
134
eeefd598 135Use this plugin if you wish to selectively force SSL mode on some of your web
136pages, for example a user login form or shopping cart.
1763fe29 137
eeefd598 138Simply place $c->require_ssl calls in any controller method you wish to be
139secured.
1763fe29 140
eeefd598 141This plugin will automatically disable itself if you are running under the
142standalone HTTP::Daemon Catalyst server. A warning message will be printed to
143the log file whenever an SSL redirect would have occurred.
1763fe29 144
145=head1 WARNINGS
146
eeefd598 147If you utilize different servers or hostnames for non-SSL and SSL requests,
148and you rely on a session cookie to determine redirection (i.e for a login
149page), your cookie must be visible to both servers. For more information, see
150the documentation for the Session plugin you are using.
1763fe29 151
152=head1 CONFIGURATION
153
154Configuration is optional. You may define the following configuration values:
155
156 https => $ssl_host
157
158If your SSL domain name is different from your non-SSL domain, set this value.
159
160 http => $non_ssl_host
161
eeefd598 162If you have set the https value above, you must also set the hostname of your
163non-SSL server.
1763fe29 164
165 remain_in_ssl
166
eeefd598 167If you'd like your users to remain in SSL mode after visiting an SSL-required
168page, you can set this option to 1. By default, this option is disabled and
169users will be redirected back to non-SSL mode as soon as possible.
1763fe29 170
eeefd598 171=head1 METHODS
1763fe29 172
eeefd598 173=head2 require_ssl
1763fe29 174
175Call require_ssl in any controller method you wish to be secured.
176
177 $c->require_ssl;
178
eeefd598 179The browser will be redirected to the same path on your SSL server. POST
180requests are never redirected.
1763fe29 181
182=head1 KNOWN ISSUES
183
eeefd598 184When viewing an SSL-required page that uses static files served from the
185Static plugin, the static files are redirected to the non-SSL path.
1763fe29 186
eeefd598 187In order to get the correct behaviour where static files are not redirected,
188you should use the Static::Simple plugin or always serve static files
189directly from your web server.
1763fe29 190
191=head1 SEE ALSO
192
eeefd598 193L<Catalyst>, L<Catalyst::Plugin::Static::Simple>
1763fe29 194
195=head1 AUTHOR
196
eeefd598 197Andy Grundman, <andy@hybridized.org>
1763fe29 198
199=head1 COPYRIGHT
200
201This program is free software, you can redistribute it and/or modify it under
202the same terms as Perl itself.
203
204=cut