Lemonldap/NG/Handler/Lib/AuthBasic.pm

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