File Coverage

blib/lib/Plack/Middleware/LemonLDAP/BasicAuth.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Plack::Middleware::LemonLDAP::BasicAuth;
2              
3             # ABSTRACT: Middleware to provide LemonLDAP support for Plack applications
4              
5 1     1   862 use strict;
  1         2  
  1         42  
6 1     1   5 use warnings;
  1         2  
  1         35  
7              
8 1     1   801 use parent 'Plack::Middleware::Auth::Basic';
  1         363  
  1         5  
9              
10 1     1   35639 use HTTP::Headers;
  1         10282  
  1         49  
11 1     1   12 use MIME::Base64 qw(decode_base64);
  1         2  
  1         87  
12 1     1   475 use SOAP::Lite;
  0            
  0            
13              
14             use Plack::Util::Accessor qw(portal cookiename);
15              
16             our $VERSION = 0.02;
17              
18             sub prepare_app {
19             my ($self) = shift;
20              
21             $self->authenticator( sub{ $self->_auth_lemonldap(@_) } );
22             $self->cookiename( 'lemonldap' ) unless $self->cookiename;
23              
24             $self->SUPER::prepare_app( @_ );
25             }
26              
27             sub call {
28             my ($self, $env) = @_;
29              
30             $self->SUPER::call( $env );
31             }
32              
33             sub _auth_lemonldap {
34             my ($self, $user, $password, $env) = @_;
35              
36             my $xheader = $env->{'X_FORWARDED_FOR'};
37             $xheader .= ", " if ($xheader);
38             $xheader .= $env->{REMOTE_ADDR};
39              
40             my $soap_headers = HTTP::Headers->new( "X-Forwarded-For" => $xheader );
41              
42             my $soap = SOAP::Lite->proxy(
43             $self->portal || '',
44             default_headers => $soap_headers,
45             )->uri('urn:Lemonldap::NG::Common::CGI::SOAPService');
46              
47             my $response = $soap->getCookies( $user, $password );
48             my $cv;
49              
50             # Catch SOAP errors
51             if ( $response->fault ) {
52             return;
53             }
54             else {
55             my $res = $response->result();
56              
57             # If authentication failed, display error
58             if ( $res->{errorCode} ) {
59             return;
60             }
61              
62             $cv = $res->{cookies}->{ $self->cookiename };
63             }
64              
65             return 1;
66             }
67              
68             1;
69              
70             __END__