File Coverage

blib/lib/Catalyst/Plugin/Authentication/CDBI/Basic.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 14 0.0
condition 0 13 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 13 59 22.0


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Authentication::CDBI::Basic;
2 2     2   36242 use strict;
  2         7  
  2         86  
3 2     2   2001 use NEXT;
  2         11640  
  2         68  
4 2     2   2154 use MIME::Base64;
  2         2747  
  2         1019  
5              
6             our $VERSION = '0.02';
7              
8             =head1 NAME
9              
10             Catalyst::Plugin::Authentication::CDBI::Basic - (DEPRECATED) Basic Authorization with Catalyst
11              
12             =head1 SYNOPSIS
13              
14             use Catalyst qw/Session::FastMmap Authentication::CDBI Authentication::CDBI::Basic/;
15              
16             __PACKAGE__->config(
17             authentication => {
18             # Configure Autentication::CDBI
19             :
20             # and
21             basic => {
22             realm => 'Require Authorization', # Basic realm
23              
24             no_session => 1, # disable auth caching (optional)
25              
26             # auto error responsing
27             # use View::TT
28             template => '401.tt',
29             view => 'MyApp::V::TT',
30              
31             # or plain text
32             error_msg => 'Authentication Failed !',
33             },
34             },
35             );
36              
37             =head1 DEPRECATION NOTICE
38              
39             This module has been deprecated. The use of a new Authentication style is recommended.
40              
41             See L<Catalyst::Plugin::Authetnication> for detail.
42              
43             =head1 DESCRIPTION
44              
45             This plugin privide Basic Authorization mechanism for Catalyst Application.
46              
47             This plugin is required C::P::Authentication::CDBI, for users info.
48             And also use Session Plugin for authorization caching (optional but recommanded).
49              
50             =head1 METHODS
51              
52             =over 4
53              
54             =item prepare
55              
56             =cut
57              
58             sub prepare {
59 0     0 1   my $c = shift;
60 0           $c = $c->NEXT::prepare(@_);
61              
62 0   0       my $auth_header = $c->req->header('Authorization') || '';
63 0 0         $c->log->debug("Authorization: $auth_header") if $auth_header;
64 0 0         if ($auth_header =~ /^Basic (.+)$/) {
65 0           my ( $username, $password ) = split q{:}, decode_base64($1);
66              
67 0           $c->log->debug("username: $username");
68 0           $c->log->debug("password: $password");
69              
70 0 0 0       ( $c->can('session_login') and !$c->config->{authentication}->{basic}->{no_session} )
    0 0        
71             ? $c->session_login($username, $password)
72             : $c->login($username, $password)
73             if $username and $password;
74             }
75              
76 0 0         unless ( $c->req->{user} ) {
77 0   0       $c->res->header('WWW-Authenticate' => q[Basic realm="]
78             . ( $c->config->{authentication}->{basic}->{realm} || 'Require Authorization' )
79             . q["] );
80              
81 0 0 0       if ( $c->config->{authentication}->{basic}->{error_msg}
82             or $c->config->{authentication}->{basic}->{view} & $c->config->{authentication}->{basic}->{template} ) {
83 0           $c->res->status(401);
84              
85 0 0         if ( $c->config->{authentication}->{basic}->{error_msg} ) {
86 0           $c->res->body( $c->config->{authentication}->{basic}->{error_msg} );
87             }
88             else {
89 0           $c->stash->{template} = $c->config->{authentication}->{basic}->{template};
90 0           $c->forward( $c->config->{authentication}->{basic}->{view} );
91             }
92             }
93             }
94              
95 0           $c;
96             }
97              
98             =back
99              
100             =head1 AUTHOR
101              
102             Daisuke Murase E<lt>typester@cpan.orgE<gt>
103              
104             =head1 COPYRIGHT
105              
106             This program is free software; you can redistribute
107             it and/or modify it under the same terms as Perl itself.
108              
109             The full text of the license can be found in the
110             LICENSE file included with this module.
111              
112              
113             =head1 SEE ALSO
114              
115             L<Catalyst::Plugin::Authentication::CDBI>
116              
117             =cut
118              
119             1;