| 1 | package Catalyst::Plugin::Authentication::Basic::Remote; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use base qw/Class::Accessor::Fast/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use NEXT; | ||
| 6 | |||
| 7 | use LWP::UserAgent; | ||
| 8 | use MIME::Base64; | ||
| 9 | |||
| 10 | our $VERSION = '0.03'; | ||
| 11 | |||
| 12 | __PACKAGE__->mk_accessors(qw/_login/); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Catalyst::Plugin::Authentication::Basic::Remote - (DEPRECATED) Basic authentication via remote host. | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Catalyst; | ||
| 21 | MyApp->setup(qw/Authentication::Basic::Remote Session::FastMmap/); | ||
| 22 | |||
| 23 | MyApp->config( | ||
| 24 | authentication => { | ||
| 25 | auth_url => 'http://example.com/', | ||
| 26 | |||
| 27 | # Use Template when unauthorized. (option) | ||
| 28 | view_tt => 'MyApp::V::TT', | ||
| 29 | template => '401.tt', | ||
| 30 | |||
| 31 | # text in Authentication dialog (default="Require Authorization") | ||
| 32 | auth_name => 'Require Authorization', | ||
| 33 | }, | ||
| 34 | ); | ||
| 35 | |||
| 36 | =head1 DEPRECATION NOTICE | ||
| 37 | |||
| 38 | This module has been deprecated. The use of a new Authentication style is recommended. | ||
| 39 | |||
| 40 | See L<Catalyst::Plugin::Authetnication> for detail. | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | Catalyst authentication plugin that use remote host's Basic authentication. | ||
| 45 | |||
| 46 | It is only first time that plugin request to remote host for authentication. | ||
| 47 | After that, user infomation keeps in sessions. | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item prepare | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub prepare { | ||
| 58 | my $c = shift; | ||
| 59 | |||
| 60 | $c = $c->NEXT::prepare(@_); | ||
| 61 | |||
| 62 | if ( $c->session->{user} and $c->session->{password} ) { | ||
| 63 | $c->log->debug("Auth info found in Session:"); | ||
| 64 | $c->log->debug("user: ".$c->session->{user}); | ||
| 65 | $c->log->debug("pass: ".$c->session->{password}); | ||
| 66 | |||
| 67 | $c->req->{user} = $c->session->{user}; | ||
| 68 | $c->req->{password} = $c->session->{password}; | ||
| 69 | return $c; | ||
| 70 | } | ||
| 71 | |||
| 72 | if ( $c->config->{authentication}->{auth_url} ) { | ||
| 73 | if ( $c->req->header('Authorization') and my ($tokens) = ( $c->req->header('Authorization') =~ /^Basic (.+)$/) ) { | ||
| 74 | my ( $username, $password ) = split /:/, decode_base64($tokens); | ||
| 75 | |||
| 76 | $c->log->debug("Authentication via ". $c->config->{authentication}->{auth_url} ); | ||
| 77 | $c->log->debug("user: $username"); | ||
| 78 | $c->log->debug("pass: $password"); | ||
| 79 | |||
| 80 | my $ua = LWP::UserAgent->new; | ||
| 81 | my $req = HTTP::Request->new( HEAD => $c->config->{authentication}->{auth_url} ); | ||
| 82 | $req->header( 'Authorization' => $c->req->header('Authorization') ); | ||
| 83 | |||
| 84 | my $res = $ua->request($req); | ||
| 85 | |||
| 86 | if ( $res->code ne '401' ) { | ||
| 87 | $c->log->debug("Authorization successful."); | ||
| 88 | $c->req->{user} = $username; | ||
| 89 | $c->session->{user} = $username; | ||
| 90 | $c->req->{password} = $password; | ||
| 91 | $c->session->{password} = $password; | ||
| 92 | $c->_login(1); | ||
| 93 | } else { | ||
| 94 | $c->log->debug("Authorization failed."); | ||
| 95 | $c->log->debug("Remote status line: " . $res->status_line); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | unless ( $c->req->{user} ) { | ||
| 100 | $c->log->debug("return 401 Unauthorized."); | ||
| 101 | $c->res->status(401); | ||
| 102 | $c->res->header( 'WWW-Authenticate' => | ||
| 103 | qq!Basic realm="@{[ $c->config->{authentication}->{auth_name} || 'Require Authorization' ]}"! | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | return $c; | ||
| 109 | } | ||
| 110 | |||
| 111 | =item dispatch | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub dispatch { | ||
| 116 | my $c = shift; | ||
| 117 | |||
| 118 | if ( $c->config->{authentication}->{template} ) { | ||
| 119 | my $view = $c->config->{authentication}->{view_tt} || $c->config->{name}; | ||
| 120 | |||
| 121 | if ($view and $c->res->status eq '401') { | ||
| 122 | $c->stash->{template} = $c->config->{authentication}->{template}; | ||
| 123 | $c->forward($view); | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | return $c->NEXT::dispatch(@_); | ||
| 129 | } | ||
| 130 | |||
| 131 | =item login | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub login { | ||
| 136 | my $c = shift; | ||
| 137 | |||
| 138 | return unless $c->session->{user}; | ||
| 139 | return if ($c->_login); | ||
| 140 | |||
| 141 | if ($c->config->{authentication}->{auth_url}) { | ||
| 142 | $c->log->debug("Login method called"); | ||
| 143 | |||
| 144 | delete $c->session->{user} if $c->session->{user}; | ||
| 145 | delete $c->session->{password} if $c->session->{password}; | ||
| 146 | |||
| 147 | $c->res->status(401); | ||
| 148 | $c->res->header( 'WWW-Authenticate' => | ||
| 149 | qq!Basic realm="@{[ $c->config->{authentication}->{auth_name} || 'Require Authorization' ]}"! | ||
| 150 | ); | ||
| 151 | |||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | return; | ||
| 156 | } | ||
| 157 | |||
| 158 | =item logout | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub logout { | ||
| 163 | my $c = shift; | ||
| 164 | |||
| 165 | return unless $c->config->{authentication}->{auth_url}; | ||
| 166 | |||
| 167 | delete $c->session->{user} if $c->session->{user}; | ||
| 168 | delete $c->session->{password} if $c->session->{password}; | ||
| 169 | |||
| 170 | delete $c->req->{user} if $c->req->{user}; | ||
| 171 | delete $c->req->{password} if $c->req->{password}; | ||
| 172 | |||
| 173 | 1; | ||
| 174 | } | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =head1 SEE ALSO | ||
| 179 | |||
| 180 | L<Catalyst> | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Daisuke Murase, E<lt>typester@cpan.orgE<gt> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT AND LICENSE | ||
| 187 | |||
| 188 | Copyright (C) 2005 by Daisuke Murase | ||
| 189 | |||
| 190 | This library is free software; you can redistribute it and/or modify | ||
| 191 | it under the same terms as Perl itself. | ||
| 192 | |||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | 1; | ||