Lemonldap/NG/Handler/Lib/AuthBasic.pm

filter: Statementstmt 49.3% 37 / 75 Branchbran 12.5% 2 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 6 totaltotal 44.1% 49 / 111 CC 9 SCAR 19.0
CC
9
Cov
43%
createSession
20.0
retrieveSession
12.0
ua
6.0
CRAP 6.7
« Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm ↑ Summary Plack/Middleware/Auth/LemonldapNG.pm »
1 package Lemonldap::NG::Handler::Lib::AuthBasic;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use SOAP::Lite; # link protected portalRequest
10 1 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Lemonldap::NG::Common::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '2.19.0';
15 1 our @ISA = ('Exporter');
16 1 our @EXPORT = qw(fetchId retrieveSession createSession hideCookie goToPortal);
17 1 our @EXPORT_OK = @EXPORT;
18 1 our $_ua;
19  
20 sub ua {
21 0 ▶ my ($class) = @_;
Subroutine ua: not called CC=2 SCAR=17.9
Pod undocumented
22 0 ▶ return $_ua if $_ua;
Branch: 1 branch
BranchTrueFalseTotal
if $Lemonldap::NG::Handler::Lib::AuthBasic::_ua 0 0 0
23 $_ua = Lemonldap::NG::Common::UserAgent->new( {
24 lwpOpts => $class->tsv->{lwpOpts},
25 lwpSslOpts => $class->tsv->{lwpSslOpts}
26 }
27 0 );
28 0 return $_ua;
29 }
30  
31 ## @rmethod protected fetchId
32 # Get user session id from Authorization header
33 # Unlike usual processing, session id is computed from user creds,
34 # so that it remains secret but handler can easily get it.
35 # It is still changed from time to time - once a day - to prevent from
36 # using indefinitely a session id disclosed accidentally or maliciously.
37 # @return session id
38 sub fetchId {
39 1 ▶ my ( $class, $req ) = @_;
Subroutine fetchId: called CC=2 SCAR=9.2
Pod undocumented
40 1 ▶ if ( my $creds = $req->env->{'HTTP_AUTHORIZATION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $creds = $req->env->{'HTTP_AUTHORIZATION'}) { } 0 1 1
41 0 $creds =~ s/^Basic\s+//;
42 0 my $pepper = int( time / $class->tsv->{timeout} ) . $class->tsv->{keyH};
43 0 return sha256_hex( $creds . $pepper );
44 }
45 else {
46 1 return 0;
47 }
48 }
49  
50 ## @rmethod protected boolean retrieveSession(id)
51 # Tries to retrieve the session whose index is id,
52 # and if needed, ask portal to create it through a SOAP request
53 # @return true if the session was found, false else
54 sub retrieveSession {
55 0 ▶ my ( $class, $req, $id ) = @_;
Subroutine retrieveSession: not called CC=3 SCAR=24.8
Pod undocumented
56  
57 # First check if session already exists
58 0 ▶ if ( my $res =
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $class->Lemonldap::NG::Handler::Main::retrieveSession($req, $id)) 0 0 0
59 $class->Lemonldap::NG::Handler::Main::retrieveSession( $req, $id ) )
60 {
61 0 return $res;
62 }
63  
64 # Then ask portal to create it
65 0 ▶ if ( $class->createSession( $req, $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->createSession($req, $id)) { } 0 0 0
66 0 return $class->Lemonldap::NG::Handler::Main::retrieveSession( $req,
67 $id );
68 }
69 else {
70 0 return 0;
71 }
72 }
73  
74 ## @rmethod protected boolean createSession(id)
75 # Send a create session request to the Portal
76 # @return true if the session is created, else false
77 sub createSession {
78 0 ▶ my ( $class, $req, $id ) = @_;
Subroutine createSession: not called CC=4 SCAR=30.0
Pod undocumented
79  
80 # Add client IP as X-Forwarded-For IP in request
81 0 my $xheader = $req->env->{'HTTP_X_FORWARDED_FOR'};
82 0 ▶ $xheader .= ", " if ($xheader);
Branch: 1 branch
BranchTrueFalseTotal
if $xheader 0 0 0
83 0 $xheader .= $req->address;
84  
85 #my $soapHeaders = HTTP::Headers->new( "X-Forwarded-For" => $xheader );
86 ## TODO: use adminSession or sessions
87 #my $soapClient = SOAP::Lite->proxy(
88 # $class->tsv->{portal}->() . '/sessions',
89 # default_headers => $soapHeaders
90 #)->uri('urn:Lemonldap/NG/Common/PSGI/SOAPService');
91  
92 0 my $creds = $req->env->{'HTTP_AUTHORIZATION'};
93 0 $creds =~ s/^Basic\s+//;
94 0 my ( $user, $pwd ) = ( decode_base64($creds) =~ /^(.*?):(.*)$/ );
95 0 $class->logger->debug("AuthBasic authentication for user: $user");
96  
97 #my $soapRequest = $soapClient->getCookies( $user, $pwd, $id );
98 0 my $url = $class->tsv->{portal}->($req) . "/sessions/global/$id?auth";
99 0 $url =~ s#//sessions/#/sessions/#g;
100 0 my $get = HTTP::Request->new( POST => $url );
101 0 $get->header( 'X-Forwarded-For' => $xheader );
102 0 $get->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
103 0 $get->header( Accept => 'application/json' );
104 $get->content(
105 build_urlencoded(
106 user => $user,
107 password => $pwd,
108 secret => $class->tsv->{cipher}->encrypt(time),
109 (
110 $class->tsv->{authChoiceAuthBasic}
111 ? ( $class->tsv->{authChoiceParam} =>
112 $class->tsv->{authChoiceAuthBasic} )
113 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$class->tsv->{'authChoiceAuthBasic'} ? : 0 0 0
114 )
115 )
116 );
117 0 my $resp = $class->ua->request($get);
118  
119 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
120 0 $class->auditLog(
121 $req,
122 message => "Good REST authentication for $user",
123 code => "REST_AUTHENTICATION_SUCCESS",
124 user => $user,
125 );
126 0 return 1;
127 }
128 else {
129 0 $class->auditLog(
130 $req,
131 message =>
132 ( "Authentication failed for $user: " . $resp->status_line ),
133 code => "REST_AUTHENTICATION_FAILURE",
134 user => $user,
135 status => $resp->status_line,
136 );
137 0 return 0;
138 }
139 }
140  
141 ## @rmethod protected void hideCookie()
142 # Hide user credentials to the protected application
143 sub hideCookie {
144 0 ▶ my ( $class, $req ) = @_;
Subroutine hideCookie: not called CC=1 SCAR=6.9
Pod undocumented
145 0 $class->logger->debug("removing Authorization header");
146 0 $class->unset_header_in( $req, 'Authorization' );
147 }
148  
149 ## @rmethod protected int goToPortal(string url, string arg)
150 # If user is asked to authenticate, return $class->AUTH_REQUIRED,
151 # else redirect him to the portal to display some message defined by $arg
152 # @param $url Url requested
153 # @param $arg optionnal GET parameters
154 # @return AUTH_REDIRECT or AUTH_REQUIRED constant
155 sub goToPortal {
156 1 ▶ my ( $class, $req, $url, $arg ) = @_;
Subroutine goToPortal: called CC=2 SCAR=7.4
Pod undocumented
157 1 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 0 1 1
158 0 return $class->Lemonldap::NG::Handler::Main::goToPortal( $req, $url,
159 $arg );
160 }
161 else {
162 1 $class->set_header_out( $req,
163 'WWW-Authenticate' => 'Basic realm="LemonLDAP::NG"' );
164 1 return $class->AUTH_REQUIRED;
165 }
166 }
167  
168 1 1;
« Lemonldap/NG/Handler/Lib/ZimbraPreAuth.pm ↑ Summary Plack/Middleware/Auth/LemonldapNG.pm »