| 1 | package Mojolicious::Plugin::SPNEGO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Net::LDAP::SPNEGO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::Socket::Timeout; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mojo::Util qw(b64_decode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.5.2'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | my %cCache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | sub register { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | my $app = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | my $plugin_cfg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | $app->helper( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ntlm_auth => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=33 SCAR=70.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my $helper_cfg = ref ${_}[0] eq 'HASH' ? ${_}[0] : { @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my $cfg = { %$plugin_cfg, %$helper_cfg }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $cId = $c->tx->connection; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $authorization = $c->req->headers->header(($cfg->{web_proxy_mode} ? 'Proxy-' : '' ) .'Authorization') // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$c->req->headers->header(($cfg->{"web_proxy_mode"} ? "Proxy-" : "") . 'Authorization') // ""
MC/DC0%0/1
$c->req->headers->header(($cfg->{"web_proxy_mode"} ? "Proxy-" : "") . 'Authorization') // ""
$c->req->headers->header(($cfg->{"web_proxy_mode"} ? "Proxy-" : "") . 'Authorization')
Truth table0%0/2
A
A= $c->req->headers->header(($cfg->{"web_proxy_mode"} ? "Proxy-" : "") . 'Authorization')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my ($AuthBase64) = ($authorization =~ /^NTLM\s(.+)$/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # $c->app->log->debug("AuthBase64: $AuthBase64") if $AuthBase64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $cCache = $cCache{$cId} //= { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$cCache{$cId} //= {"status", $AuthBase64 ? "expectType1" : "init"}
MC/DC0%0/1
$cCache{$cId} //= {"status", $AuthBase64 ? "expectType1" : "init"}
$cCache{$cId}
Truth table0%0/2
A
A= $cCache{$cId}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | status => $AuthBase64 ? 'expectType1' : 'init' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return 1 if $cCache->{status} eq 'authenticated'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my ($status) = ($cCache->{status} =~ /^expect(Type[13])/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # $c->app->log->debug("status: $status") if $status; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($AuthBase64 and $status){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$AuthBase64 and $status
MC/DC0%0/2
$AuthBase64 and $status
$AuthBase64
$status
Truth table0%0/3
A and B
A= $AuthBase64B= $status
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | for ($status){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $timeout = $cfg->{timeout} // 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cfg->{"timeout"} // 10
MC/DC0%0/1
$cfg->{"timeout"} // 10
$cfg->{"timeout"}
Truth table0%0/2
A
A= $cfg->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | my $ldap = $cCache->{ldapObj} //= Net::LDAP::SPNEGO->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | $cfg->{ad_server}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | debug=>($cfg->{ldap_debug}//$ENV{SPNEGO_LDAP_DEBUG}//0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | onerror=> sub { my $msg = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $c->log->error("LDAP ERROR: " . $msg->error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $msg | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | timeout=>$timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | verify => $cfg->{verify} // 'none' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/10
$cCache->{"ldapObj"} //= "Net::LDAP::SPNEGO"->new($cfg->{"ad_server"}, "debug", ($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0, "onerror", sub {
my $msg = shift();
$c->log->error('LDAP ERROR: ' . $msg->error);
return $msg;
}
, "timeout", $timeout, "verify", $cfg->{"verify"} // "none")
($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0
$cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}
$cfg->{"verify"} // "none"
MC/DC0%0/2
$cCache->{"ldapObj"} //= "Net::LDAP::SPNEGO"->new($cfg->{"ad_server"}, "debug", ($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0, "onerror", sub {
my $msg = shift();
$c->log->error('LDAP ERROR: ' . $msg->error);
return $msg;
}
, "timeout", $timeout, "verify", $cfg->{"verify"} // "none")
$cCache->{"ldapObj"}
"Net::LDAP::SPNEGO"->new($cfg->{"ad_server"}, "debug", ($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0, "onerror", sub {
my $msg = shift();
$c->log->error('LDAP ERROR: ' . $msg->error);
return $msg;
}
, "timeout", $timeout, "verify", $cfg->{"verify"} // "none")
MC/DC0%0/2
($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0
$cfg->{"ldap_debug"}
$ENV{"SPNEGO_LDAP_DEBUG"}
MC/DC0%0/1
$cfg->{"verify"} // "none"
$cfg->{"verify"}
Truth table0%0/3
A //= B
A= $cCache->{"ldapObj"}B= "Net::LDAP::SPNEGO"->new($cfg->{"ad_server"}, "debug", ($cfg->{"ldap_debug"} // $ENV{"SPNEGO_LDAP_DEBUG"}) // 0, "onerror", sub {
my $msg = shift();
$c->log->error('LDAP ERROR: ' . $msg->error);
return $msg;
}
, "timeout", $timeout, "verify", $cfg->{"verify"} // "none")
Truth table0%0/3
A // B
A= $cfg->{"ldap_debug"}B= $ENV{"SPNEGO_LDAP_DEBUG"}
Truth table0%0/2
A
A= $cfg->{"verify"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ) or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($ldap->uri !~ m{^ldaps://}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $msg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($cfg->{verify}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $msg = $ldap->start_tls(verify => $cfg->{verify}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | elsif ($cfg->{start_tls}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $msg = $ldap->start_tls($cfg->{start_tls}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($msg and $msg->is_error()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$msg and $msg->is_error
MC/DC0%0/2
$msg and $msg->is_error
$msg
$msg->is_error
Truth table0%0/3
A and B
A= $msgB= $msg->is_error
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $c->log->error($msg->error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # Read/Write timeouts via setsockopt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $socket = $ldap->socket(sasl_layer=>0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | IO::Socket::Timeout->enable_timeouts_on($socket); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $socket->read_timeout($timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $socket->write_timeout($timeout); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | /^Type1/ && do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $c->app->log->debug("Bind Type1 ..."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $mesg = $ldap->bind_type1($AuthBase64); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($mesg->{ntlm_type2_base64}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $c->res->headers->header( ($cfg->{web_proxy_mode} ? 'Proxy' : 'WWW' ) . '-Authenticate' => 'NTLM '.$mesg->{ntlm_type2_base64}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $c->render( text => 'Waiting for Type3 NTLM Token', status => $cfg->{web_proxy_mode} ? 407 : 401); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $cCache->{status} = 'expectType3'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # lets try with a new connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $ldap->unbind; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | delete $cCache->{ldapObj}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | /^Type3/ && do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $c->app->log->debug("Bind Type3 as user '".$ldap->_get_user_from_ntlm_type3(b64_decode($AuthBase64))."'"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $mesg = $ldap->bind_type3($AuthBase64); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (my $user = $mesg->{ldap_user_entry}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if (my $cb = $cfg->{auth_success_cb}){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (not $cb or $cb->($c,$user,$ldap)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $cb or &$cb($c, $user, $ldap)
MC/DC0%0/2
not $cb or &$cb($c, $user, $ldap)
not $cb
&$cb($c, $user, $ldap)
Truth table0%0/3
not(A) or B
A= not $cbB= &$cb($c, $user, $ldap)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $cCache->{status} = 'authenticated'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $ldap->unbind; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | delete $cCache->{ldapObj}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return 1 if $cCache->{status} eq 'authenticated'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $c->res->headers->header( ($cfg->{web_proxy_mode} ? 'Proxy' : 'WWW') . '-Authenticate' => 'NTLM' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $c->render( text => 'Waiting for Type 1 NTLM Token', status => $cfg->{web_proxy_mode} ? 407 : 401 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $cCache->{status} = 'expectType1'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||