File Coverage

blib/lib/HTTP/Headers/ActionPack/Authorization/Basic.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 6 83.3
condition 7 12 58.3
subroutine 12 12 100.0
pod 6 6 100.0
total 64 70 91.4


line stmt bran cond sub pod time code
1             package HTTP::Headers::ActionPack::Authorization::Basic;
2             BEGIN {
3 4     4   30441 $HTTP::Headers::ActionPack::Authorization::Basic::AUTHORITY = 'cpan:STEVAN';
4             }
5             {
6             $HTTP::Headers::ActionPack::Authorization::Basic::VERSION = '0.09';
7             }
8             # ABSTRACT: The Basic Authorization Header
9              
10 4     4   29 use strict;
  4         8  
  4         133  
11 4     4   23 use warnings;
  4         8  
  4         121  
12              
13 4     4   23 use Carp qw[ confess ];
  4         7  
  4         280  
14 4     4   5758 use MIME::Base64 qw[ encode_base64 decode_base64 ];
  4         15095  
  4         790  
15              
16 4     4   3690 use parent 'HTTP::Headers::ActionPack::Core::Base';
  4         1369  
  4         51  
17              
18             sub BUILDARGS {
19 12     12 1 29 my $class = shift;
20 12   33     43 my $type = shift || confess "Must specify type";
21 12   33     43 my $credentials = shift || confess "Must provide credentials";
22              
23 12 100 100     116 if ( ref $credentials && ref $credentials eq 'HASH' ) {
    100 66        
24 3         56 return +{ auth_type => $type, %$credentials };
25             }
26             elsif ( ref $credentials && ref $credentials eq 'ARRAY' ) {
27 3         56 my ($username, $password) = @$credentials;
28 3         29 return +{ auth_type => $type, username => $username, password => $password };
29             }
30             else {
31 6         77 my ($username, $password) = split ':' => decode_base64( $credentials );
32 6         79 return +{ auth_type => $type, username => $username, password => $password };
33             }
34             }
35              
36             sub new_from_string {
37 3     3 1 20 my ($class, $header_string) = @_;
38 3         25 my ($type, $credentials) = split /\s/ => $header_string;
39 3 50       19 ($type eq 'Basic')
40             || confess "The type must be 'Basic', not '$type'";
41 3         44 $class->new( $type, $credentials );
42             }
43              
44 24     24 1 10740 sub auth_type { (shift)->{'auth_type'} }
45 24     24 1 89 sub username { (shift)->{'username'} }
46 24     24 1 161 sub password { (shift)->{'password'} }
47              
48             sub as_string {
49 12     12 1 24 my $self = shift;
50 12         35 join ' ' => $self->auth_type, encode_base64( (join ':' => $self->username, $self->password), '' )
51             }
52              
53             1;
54              
55             __END__