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