| 1 | package Plack::Middleware::Auth::Negotiate; | ||
| 2 | $Plack::Middleware::Auth::Negotiate::VERSION = '0.172130'; | ||
| 3 | # ABSTRACT: Negotiate authentication middleware (SPNEGO) | ||
| 4 | |||
| 5 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use parent 'Plack::Middleware'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Plack::Util::Accessor 'keytab'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Scalar::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use MIME::Base64; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use GSSAPI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | sub prepare_app { | ||
| 16 | my $self = shift; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub call { | ||
| 20 | my($self, $env) = @_; | ||
| 21 | |||
| 22 | my $auth = $env->{HTTP_AUTHORIZATION} | ||
| 23 | or return $self->unauthorized; | ||
| 24 | |||
| 25 | if ($auth =~ /^Negotiate (.*)$/) { | ||
| 26 | my $data = MIME::Base64::decode($1); | ||
| 27 | $ENV{KRB5_KTNAME} = $self->keytab if $self->keytab; | ||
| 28 | my $user = gssapi_verify($data); | ||
| 29 | if ($user) { | ||
| 30 | $env->{REMOTE_USER} = $user; | ||
| 31 | return $self->app->($env); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | return $self->unauthorized; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $UNAUTH_MSG = q{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | ||
| 39 | <html><head> | ||
| 40 | <title>401 Authorization Required</title> | ||
| 41 | </head><body> | ||
| 42 | <h1>Authorization Required</h1> | ||
| 43 | <p>This server could not verify that you | ||
| 44 | are authorized to access the document | ||
| 45 | requested. Either you supplied the wrong | ||
| 46 | credentials (e.g., bad password), or your | ||
| 47 | browser doesn't understand how to supply | ||
| 48 | the credentials required.</p> | ||
| 49 | </body></html> | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub unauthorized { | ||
| 53 | my $self = shift; | ||
| 54 | return [ | ||
| 55 | 401, | ||
| 56 | [ 'Content-Type' => 'text/html', | ||
| 57 | 'Content-Length' => length $UNAUTH_MSG, | ||
| 58 | 'WWW-Authenticate' => 'Negotiate' ], | ||
| 59 | [ $UNAUTH_MSG ], | ||
| 60 | ]; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub gssapi_verify { | ||
| 64 | my $gss_input_token = shift; | ||
| 65 | my $server_context; | ||
| 66 | my $status = GSSAPI::Context::accept( | ||
| 67 | $server_context, | ||
| 68 | GSS_C_NO_CREDENTIAL, | ||
| 69 | $gss_input_token, | ||
| 70 | GSS_C_NO_CHANNEL_BINDINGS, | ||
| 71 | my $gss_client_name, | ||
| 72 | undef, | ||
| 73 | my $gss_output_token, | ||
| 74 | my $out_flags, | ||
| 75 | my $out_time, | ||
| 76 | my $gss_delegated_cred); | ||
| 77 | |||
| 78 | $status or return gss_exit("Unable to accept security context", $status); | ||
| 79 | my $client_name; | ||
| 80 | $status = $gss_client_name->display($client_name); | ||
| 81 | $status or return gss_exit("Unable to display client name", $status); | ||
| 82 | return $client_name; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub gss_exit { | ||
| 86 | my $errmsg = shift; | ||
| 87 | my $status = shift; | ||
| 88 | |||
| 89 | my @major_errors = $status->generic_message(); | ||
| 90 | my @minor_errors = $status->specific_message(); | ||
| 91 | |||
| 92 | print STDERR "$errmsg:\n"; | ||
| 93 | foreach my $s (@major_errors) { | ||
| 94 | print STDERR " MAJOR::$s\n"; | ||
| 95 | } | ||
| 96 | foreach my $s (@minor_errors) { | ||
| 97 | print STDERR " MINOR::$s\n"; | ||
| 98 | } | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 1; # End of Plack::Middleware::Auth::Negotiate | ||
| 104 | |||
| 105 | __END__ | ||