Server/Command/Authenticate.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 97 CC 12 SCAR 33.7
CC
12
Cov
0%
validate
42.0
continue
42.0
run
6.0
CRAP 29.1
1 package Net::IMAP::Server::Command::Authenticate;
2  
3 use warnings;
4 use strict;
5  
6 use MIME::Base64;
7 use base qw/Net::IMAP::Server::Command/;
8  
9 __PACKAGE__->mk_accessors(qw(sasl pending_auth));
10  
11 sub validate {
12 my $self = shift;
13  
14 return $self->bad_command("Already logged in")
15 unless $self->connection->is_unauth;
16  
17 my @options = $self->parsed_options;
18 return $self->bad_command("Not enough options") if @options < 1;
19 return $self->bad_command("Too many options") if @options > 2;
20  
21 $self->untagged_response("BAD [ALERT] Plaintext authentication not over SSL is insecure -- your password was just exposed.")
22 if $options[0] eq "PLAIN" and not $self->connection->is_encrypted;
23  
24 return $self->no_command("Authentication type not supported")
25 unless $self->connection->capability =~ /\bAUTH=$options[0]\b/i;
26  
27 return 1;
28 }
29  
30 sub run {
31 my $self = shift;
32  
33 my($type, $arg) = $self->parsed_options;
34 $self->server->auth_class->require || $self->log( 1, $@ );
35 my $auth = $self->server->auth_class->new;
36 if ( grep {uc $type eq uc $_} $auth->sasl_provides ) {
37 $type = lc $type;
38 $type =~ s/\W/_/g;
39 my $function = "sasl_$type";
40 $self->sasl( $auth->$function() );
41 $self->pending_auth($auth);
42 $self->connection->pending(sub {$self->continue(@_)});
43 $self->continue( $arg || "");
44 } else {
45 $self->no_command("Authentication type not supported");
46 }
47 }
48  
49 sub continue {
50 my $self = shift;
51 my $line = shift;
52  
53 $self->connection->pending(undef);
54  
55 return $self->bad_command("Login cancelled")
56 if not defined $line or $line =~ /^\*[\r\n]+$/;
57  
58 $line =~ s/[\r\n]+$//;
59 my $decoded = decode_base64($line);
60 return $self->bad_command("Invalid base64")
61 if encode_base64($decoded, "") ne $line;
62  
63 my $response = $self->sasl->($decoded);
64 if ( ref $response ) {
65 $self->connection->pending(sub{$self->continue(@_)});
66 $self->out( "+ " . encode_base64($$response, "") );
67 } elsif (not $response) {
68 $self->no_command("Invalid login");
69 } elsif ($response < 0) {
70 $self->bad_command("Protocol failure");
71 } else {
72 $self->connection->auth( $self->pending_auth );
73 $self->ok_completed();
74 }
75 }
76  
77 1;