| 1 | package Catalyst::Plugin::RequireSSL; | ||
| 2 | |||
| 3 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 6 | ▶ | use base qw/Class::Accessor::Fast/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use MRO::Compat; | ||
| 6 | |||
| 7 | our $VERSION = '0.07'; | ||
| 8 | |||
| 9 | __PACKAGE__->mk_accessors( qw/_require_ssl _allow_ssl _ssl_strip_output/ ); | ||
| 10 | |||
| 11 | sub 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 { | ||
| 22 | $c->_ssl_strip_output(1); | ||
| 23 | $c->res->redirect( $redir ); | ||
| 24 | $c->detach if $c->config->{require_ssl}->{detach_on_redirect}; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | sub allow_ssl { | ||
| 30 | my $c = shift; | ||
| 31 | |||
| 32 | $c->_allow_ssl(1); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub finalize { | ||
| 36 | my $c = shift; | ||
| 37 | |||
| 38 | # Do not redirect static files (only works with Static::Simple) | ||
| 39 | if ( $c->isa( "Catalyst::Plugin::Static::Simple" ) ) { | ||
| 40 | return $c->next::method(@_) if $c->_static_file; | ||
| 41 | } | ||
| 42 | |||
| 43 | # redirect back to non-SSL mode | ||
| 44 | REDIRECT: | ||
| 45 | { | ||
| 46 | # No redirect if: | ||
| 47 | # we're not in SSL mode | ||
| 48 | last REDIRECT if !$c->req->secure; | ||
| 49 | # it's a POST request | ||
| 50 | last REDIRECT if $c->req->method eq "POST"; | ||
| 51 | # we're already required to be in SSL for this request | ||
| 52 | last REDIRECT if $c->_require_ssl; | ||
| 53 | # or the user doesn't want us to redirect | ||
| 54 | last REDIRECT if $c->config->{require_ssl}->{remain_in_ssl} || $c->_allow_ssl; | ||
| 55 | |||
| 56 | $c->res->redirect( $c->_redirect_uri('http') ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # do not allow any output to be displayed on the insecure page | ||
| 60 | if ( $c->_ssl_strip_output ) { | ||
| 61 | $c->res->body( '' ); | ||
| 62 | } | ||
| 63 | |||
| 64 | return $c->next::method(@_); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub setup { | ||
| 68 | my $c = shift; | ||
| 69 | |||
| 70 | $c->next::method(@_); | ||
| 71 | |||
| 72 | # disable the plugin when running under certain engines which don't | ||
| 73 | # support SSL | ||
| 74 | if ( $c->engine =~ /Catalyst::Engine::HTTP/ ) { | ||
| 75 | $c->config->{require_ssl}->{disabled} = 1; | ||
| 76 | $c->log->warn( "RequireSSL: Disabling SSL redirection while running " | ||
| 77 | . "under " . $c->engine ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _redirect_uri { | ||
| 82 | my ( $c, $type ) = @_; | ||
| 83 | |||
| 84 | if ( !$c->config->{require_ssl}->{$type} ) { | ||
| 85 | my $req_uri = $c->req->uri; | ||
| 86 | $c->config->{require_ssl}->{$type} = | ||
| 87 | join(':', $req_uri->host, $req_uri->_port); | ||
| 88 | } | ||
| 89 | |||
| 90 | $c->config->{require_ssl}->{$type} =~ s/\/+$//; | ||
| 91 | |||
| 92 | my $redir = $c->req->uri->clone; | ||
| 93 | $redir->scheme($type); | ||
| 94 | $redir->host_port($c->config->{require_ssl}->{$type}); | ||
| 95 | |||
| 96 | if ( $c->config->{require_ssl}->{no_cache} ) { | ||
| 97 | delete $c->config->{require_ssl}->{$type}; | ||
| 98 | } | ||
| 99 | |||
| 100 | return $redir; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | __END__ | ||