blib/lib/Lemonldap/NG/Portal/Auth/Kerberos.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Lemonldap/NG/Portal/UserDB/Null.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/SAML.pm »
1 package Lemonldap::NG::Portal::Auth::Kerberos;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use GSSAPI;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use MIME::Base64;
7 use Lemonldap::NG::Portal::Main::Constants qw(
8 PE_BADCREDENTIALS
9 PE_ERROR
10 PE_FIRSTACCESS
11 PE_OK
12 PE_SENDRESPONSE
13 );
14  
15 our $VERSION = '2.23.1';
16  
17 extends 'Lemonldap::NG::Portal::Main::Auth';
18  
19 has allowedDomains => ( is => 'rw', isa => 'ArrayRef' );
20 has keytab => ( is => 'rw' );
21 has Name => ( is => 'ro', default => 'Kerberos' );
22  
23 has auth_id => ( is => 'ro', default => 'krb' );
24  
25 with 'Lemonldap::NG::Portal::Auth::_Ajax';
26  
27 # INITIALIZATION
28 sub init {
29 my $self = shift;
30 my $file;
31 my $domains;
32 unless ( $file = $self->conf->{krbKeytab} ) {
33 $self->logger->error('Keytab not defined');
34 return 0;
35 }
36  
37 if ( $domains = $self->conf->{krbAllowedDomains} ) {
38 $self->allowedDomains( [ split /[\s,]+/, $domains ] );
39 }
40  
41 $self->keytab("FILE:$file");
42 return 1;
43 }
44  
45 # Initializes display
46 # When called from Choice, $from_self will not be set
47 sub initDisplay {
48 my ( $self, $req, $from_self ) = @_;
49  
50 $self->SUPER::initDisplay($req);
51  
52 $self->p->setHiddenFormValue( $req, kerberos => 0, '', 0 );
53  
54 if ( $self->conf->{krbByJs} ) {
55  
56 # During session upgrade, script is already injected by extractFormInfo
57 if ( $req->data->{_krbInitAlreadyDone} ) {
58 $req->data->{customScript} =~
59 s/kerberos.min.js/kerberosChoice.min.js/g;
60 }
61 else {
62 $req->data->{customScript} .=
63 '<script type="text/javascript" src="'
64 . $self->p->staticPrefix
65 . '/common/js/'
66 . ( $from_self ? 'kerberos' : 'kerberosChoice' )
67 . '.min.js"></script>';
68 }
69  
70 if ( $req->pdata->{krbDone} ) {
71 $req->data->{customScript} .=
72 '<script type="application/init">'
73 . '{ "krbDone": 1 }'
74 . '</script>';
75  
76 }
77 }
78 }
79  
80 sub auth_route {
81 my ( $self, $req ) = @_;
82  
83 my $auth = $req->env->{HTTP_AUTHORIZATION};
84 if ($auth) {
85 return $self->_handle_ajax_response( $req, $auth );
86 }
87 else {
88 return $self->_request_ajax_credential($req);
89 }
90 }
91  
92 sub extractFormInfo {
93 my ( $self, $req ) = @_;
94  
95 # This is used when a Combination stack defined different UserDB
96 # for different Kerberos domains
97 if ( $req->data->{_krbUser} ) {
98 $self->logger->debug(
99 'Kerberos ticket already validated for ' . $req->data->{_krbUser} );
100 return $self->_checkDomains($req);
101 }
102  
103 my $token_id = $req->param('ajax_auth_token');
104 if ($token_id) {
105 my $token = $self->get_auth_token( $req, $token_id );
106 if ( $token->{user} ) {
107 $req->pdata->{krbDone} = 1;
108 return $self->_krbAuthFinish( $req, $token->{user} );
109 }
110 else {
111 return PE_ERROR;
112 }
113 }
114  
115 # Non-AJAX auth
116 my $auth = $req->env->{HTTP_AUTHORIZATION};
117 if ($auth) {
118 return $self->_handle_response( $req, $auth );
119 }
120 else {
121 return $self->_request_credential($req);
122 }
123 }
124  
125 sub _request_credential {
126 my ( $self, $req ) = @_;
127  
128 # Case 1: simple usage or first Kerberos Ajax request
129 # => return 401 to initiate Kerberos
130 if ( !$self->{conf}->{krbByJs} or $req->param('kerberos') ) {
131 $self->logger->debug('Initialize Kerberos dialog');
132  
133 # Case 1.1: Ajax request
134 if ( $req->wantJSON ) {
135 $req->response( $self->_request_ajax_credential($req) );
136 }
137  
138 # Case 1.2: HTML request: display error and initiate Kerberos
139 # dialog
140 else {
141 $req->error(PE_BADCREDENTIALS);
142 push @{ $req->respHeaders }, 'WWW-Authenticate' => 'Negotiate';
143 my ( $tpl, $prms ) = $self->p->display($req);
144 $req->response(
145 $self->p->sendHtml(
146 $req, $tpl,
147 params => $prms,
148 code => 401
149 )
150 );
151 }
152 return PE_SENDRESPONSE;
153 }
154  
155 # Case 2: Ajax Kerberos request has failed, and javascript has reloaded
156 # page with "kerberos=0". Return an error to be able to switch to
157 # another backend (Combination)
158 elsif ( defined $req->param('kerberos') ) {
159 $self->userLogger->warn(
160 'Kerberos authentication has failed, back to portal');
161 $self->p->setHiddenFormValue( $req, kerberos => 0, '', 0 );
162 return PE_BADCREDENTIALS;
163 }
164  
165 # Case 3: Display kerberos auth page (with javascript)
166 else {
167 $self->logger->debug( 'Append ' . $self->Name . ' init/script' );
168  
169 # In some Combination scenarios, Kerberos may be called multiple
170 # times but we only need to initialize the display once
171 unless ( $req->data->{_krbInitAlreadyDone} ) {
172 $self->initDisplay( $req, 1 );
173 $req->data->{_krbInitAlreadyDone} = 1;
174 }
175  
176 $req->data->{waitingMessage} = 1;
177 return PE_FIRSTACCESS;
178 }
179 }
180  
181 sub _handle_response {
182 my ( $self, $req, $auth ) = @_;
183  
184 my $client_name = $self->_krb_get_user($auth);
185 if ($client_name) {
186 return $self->_krbAuthFinish( $req, $client_name );
187 }
188 else {
189 return PE_BADCREDENTIALS;
190 }
191 }
192  
193 sub _krbAuthFinish {
194 my ( $self, $req, $client_name ) = @_;
195  
196 $self->userLogger->notice("$client_name authentified by Kerberos");
197 $req->data->{_krbUser} = $client_name;
198 if ( $self->conf->{krbRemoveDomain} ) {
199 $client_name =~ s/^(.*)@.*$/$1/;
200 }
201 $req->user($client_name);
202 return $self->_checkDomains($req);
203 }
204  
205 sub _request_ajax_credential {
206 my ( $self, $req ) = @_;
207  
208 $self->logger->debug('Initialize Kerberos dialog');
209  
210 # Use sendTextResponse instead of sendJSONresponse to avoid normal header
211 # processing
212 return $self->p->sendTextResponse(
213 $req, '{"error":"Authentication required"}',
214 type => "application/json",
215 code => 401,
216 headers => [ 'WWW-Authenticate' => 'Negotiate' ]
217 );
218 }
219  
220 sub _handle_ajax_response {
221 my ( $self, $req, $auth ) = @_;
222  
223 my $client_name = $self->_krb_get_user($auth);
224 if ($client_name) {
225 return $self->ajax_success( $req, $client_name );
226 }
227 else {
228 $req->wantErrorRender(1);
229 return $self->p->doPE( $req, PE_BADCREDENTIALS );
230 }
231 }
232  
233 sub _krb_get_user {
234 my ( $self, $auth ) = @_;
235  
236 my $ticket;
237  
238 # Case 4: an "Authorization header" has been sent
239 if ( $auth =~ /^Negotiate (.*)$/ ) {
240 $ticket = $1;
241 }
242 else {
243 $self->userLogger->error('Bad authorization header');
244 return;
245 }
246  
247 # Case 5: Kerberos ticket received
248 $self->logger->debug("Kerberos ticket received: $ticket");
249 my $data;
250 eval { $data = MIME::Base64::decode($ticket) };
251 if ($@) {
252 $self->userLogger->error( 'Bad authorization header: ' . $@ );
253 return;
254 }
255 $ENV{KRB5_KTNAME} = $self->keytab;
256 $self->logger->debug( "Set KRB5_KTNAME env to " . $ENV{KRB5_KTNAME} );
257  
258 # NTML tickets are not part of GSSAPI and not recognized by MIT KRB5
259 # since they are a proprietary SSPI mechanism
260 if ( substr( $data, 0, 8 ) eq "NTLMSSP\0" ) {
261 $self->logger->error(
262 'Received ticket is actually a NTLM ticket instead of a Kerberos '
263 . 'ticket, make sure the workstation is correctly configured: '
264 . 'portal in trusted internet zone, clock synchronization, '
265 . 'correct reverse DNS configuration...' );
266 return;
267 }
268  
269 my $status = GSSAPI::Context::accept(
270 my $server_context,
271 GSS_C_NO_CREDENTIAL,
272 $data,
273 GSS_C_NO_CHANNEL_BINDINGS,
274 my $gss_client_name,
275 undef,
276 my $gss_output_token,
277 my $out_flags,
278 my $out_time,
279 my $gss_delegated_cred
280 );
281 unless ($status) {
282 $self->logger->error('Unable to accept security context');
283 foreach ( $status->generic_message(), $status->specific_message() ) {
284 $self->logger->error($_);
285 }
286 return;
287 }
288 my $client_name;
289 $status = $gss_client_name->display($client_name);
290 if ($status) {
291 $self->logger->debug("Received Kerberos principal: $client_name");
292 return $client_name;
293 }
294 $self->logger->error('Unable to display KRB client name');
295 foreach ( $status->generic_message(), $status->specific_message() ) {
296 $self->logger->error($_);
297 }
298 return;
299 }
300  
301 sub _checkDomains {
302 my ( $self, $req ) = @_;
303  
304 # If krbAllowedDomains is not defined, allow every domain
305 return PE_OK unless ( $self->allowedDomains );
306  
307 my ($domain) = $req->data->{_krbUser} =~ m/^.*@(.*)$/;
308 if ( grep { lc($_) eq lc($domain) } @{ $self->allowedDomains } ) {
309 return PE_OK;
310 }
311 else {
312 $self->userLogger->warn(
313 "Received kerberos domain $domain is not allowed");
314 return PE_BADCREDENTIALS;
315 }
316 }
317  
318 sub authenticate {
319 return PE_OK;
320 }
321  
322 sub authLogout {
323 return PE_OK;
324 }
325  
326 sub setAuthSessionInfo {
327 my ( $self, $req ) = @_;
328 $req->{sessionInfo}->{authenticationLevel} = $self->conf->{krbAuthnLevel};
329 $req->{sessionInfo}->{_krbUser} = $req->data->{_krbUser};
330 return PE_OK;
331 }
332  
333 sub getDisplayType {
334 return "logo";
335 }
336  
337 1;
« blib/lib/Lemonldap/NG/Portal/UserDB/Null.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Auth/SAML.pm »