| 1 | # Apache::AppSamurai::AuthBasic - AppSamurai authentication against webserver | ||
| 2 | # using basic authentication. | ||
| 3 | |||
| 4 | # $Id: AuthBasic.pm,v 1.18 2008/04/30 21:40:05 pauldoom Exp $ | ||
| 5 | |||
| 6 | ## | ||
| 7 | # Copyright (c) 2008 Paul M. Hirsch (paul@voltagenoir.org). | ||
| 8 | # All rights reserved. | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify it under | ||
| 11 | # the same terms as Perl itself. | ||
| 12 | ## | ||
| 13 | |||
| 14 | package Apache::AppSamurai::AuthBasic; | ||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use vars qw($VERSION @ISA); | ||
| 19 | $VERSION = substr(q$Revision: 1.18 $, 10, -1); | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | use Apache::AppSamurai::AuthBase; | ||
| 23 | |||
| 24 | # Below is used to make client connection to backend server to test auth | ||
| 25 | # and collect any cookies we want to keep | ||
| 26 | use LWP::UserAgent; | ||
| 27 | use HTTP::Request; | ||
| 28 | use MIME::Base64; | ||
| 29 | |||
| 30 | @ISA = qw( Apache::AppSamurai::AuthBase ); | ||
| 31 | |||
| 32 | sub Configure { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | # Pull defaults from AuthBase and save. | ||
| 36 | $self->SUPER::Configure(); | ||
| 37 | my $conft = $self->{conf}; | ||
| 38 | |||
| 39 | # Initial configuration. Put defaults here before the @_ args are | ||
| 40 | # pulled in. | ||
| 41 | $self->{conf} = { %{$conft}, | ||
| 42 | LoginUrl => 'https://127.0.0.1', # URL to authenticate | ||
| 43 | # aginst | ||
| 44 | KeepAuth => 0, # Keep Authorization: Basic XXX header | ||
| 45 | # and continue to send to the proxied | ||
| 46 | # servers. BE CAREFUL! | ||
| 47 | PassBackCookies => 0, # Pass all Set-Cookies back to | ||
| 48 | # client browser | ||
| 49 | AllowRedirect => 0, # Follow redirects (Keep off and get | ||
| 50 | # the URL right!) | ||
| 51 | UserAgent => '', # The User-Agent: header to report | ||
| 52 | RequireRealm => '', # If set, this realm must match that | ||
| 53 | # returned by the backend server | ||
| 54 | SuccessCode => 200, # Auth considered a failure unless | ||
| 55 | # this code is returned after login | ||
| 56 | Timeout => 10, # Timeout for connecting to auth server | ||
| 57 | PassMin => 3, | ||
| 58 | PassChars => '\w\d !\@\#\$\%\^\&\*,\.\?\-_=\+', # NOTE: | ||
| 59 | # No : since that perplexes Mr. | ||
| 60 | # Basic Auth | ||
| 61 | @_, | ||
| 62 | }; | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub Initialize { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | # Holding space for alterlist items | ||
| 70 | $self->{alterlist} = {}; | ||
| 71 | |||
| 72 | # Create LWP client and empty request | ||
| 73 | $self->{client} = new LWP::UserAgent(timeout => $self->{conf}{Timeout}); | ||
| 74 | ($self->{client}) || ($self->AddError("Initialization of LWP::UserAgent failed: $!") && return 0); | ||
| 75 | $self->{request} = new HTTP::Request("GET", $self->{conf}{LoginUrl}); | ||
| 76 | ($self->{request}) || ($self->AddError("Initialization of HTTP::Request failed: $!") && return 0); | ||
| 77 | |||
| 78 | # Turn off all redirects if configured | ||
| 79 | ($self->{conf}{AllowRedirect} == 1) || ($self->{client}->requests_redirectable([])); | ||
| 80 | |||
| 81 | # Set the User-Agent for the request (You may want to use | ||
| 82 | # "HEADER:User-Agent" as the value in your Apache config. AppSamurai.pm | ||
| 83 | # will fill in the client's User-Agent: header value per-request, then.) | ||
| 84 | ($self->{conf}{UserAgent} eq '') || ($self->{request}->header('User-Agent' => $self->{conf}{UserAgent})); | ||
| 85 | |||
| 86 | $self->{init} = 1; | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | # Connect to the server to check that authentication is required, | ||
| 92 | # then send a second request with authentication and check for | ||
| 93 | # good return code | ||
| 94 | sub Authenticator { | ||
| 95 | my $self = shift; | ||
| 96 | my $user = shift; | ||
| 97 | my $pass = shift; | ||
| 98 | my ($response, $error, @tmp, $check, $realm); | ||
| 99 | |||
| 100 | # Initial connect and check return for 401 | ||
| 101 | # Note - This pre-check is to make sure the target is actually listening | ||
| 102 | $response = $self->{client}->request($self->{request}); | ||
| 103 | if ($response->code() != 401) { | ||
| 104 | # Well, that ain't right! | ||
| 105 | $self->AddError('error', "Failure or bad return code while connecting to \"" . $self->{conf}{LoginUrl} . "\": Code \"" . $response->code() . "\", $!"); | ||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Check for Basic in the list of WWW-Authorization values and | ||
| 110 | # optional realm | ||
| 111 | @tmp = $response->header("WWW-Authenticate"); | ||
| 112 | $check = 0; | ||
| 113 | foreach (@tmp) { | ||
| 114 | if (/^basic( realm=\"([^\"]*?)\")?$/i) { | ||
| 115 | $realm = $2; | ||
| 116 | |||
| 117 | if ($self->{conf}{RequireRealm} ne '') { | ||
| 118 | # Note - Realm matched without case sensitivity. | ||
| 119 | unless ($realm =~ /^\Q$self->{conf}{RequireRealm}\E$/i) { | ||
| 120 | $self->AddError('error', "URL \"" . $self->{conf}{LoginUrl} . "\" returned the wrong realm: \"" . $realm . "\" (Check BasicAuthRequireRealm setting)"); | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | $check = 1; | ||
| 126 | last; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | unless ($check) { | ||
| 130 | $self->AddError('error', "URL \"" . $self->{conf}{LoginUrl} . "\" did not list \"Basic\" as an allowed authentication method"); | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | # Set credentials in request | ||
| 135 | $self->{request}->authorization_basic($user, $pass); | ||
| 136 | |||
| 137 | # Connect with beans and check return | ||
| 138 | $response = $self->{client}->request($self->{request}); | ||
| 139 | |||
| 140 | if ($response->code() == $self->{conf}{SuccessCode}) { | ||
| 141 | # YAY! Now collect and store cookies and headers as directed | ||
| 142 | if (($self->{conf}{PassBackCookies}) && (@tmp = $response->header('set-cookie'))) { | ||
| 143 | foreach (@tmp) { | ||
| 144 | # Trim whitespace | ||
| 145 | s/^\s*(.*?)\s*$/$1/; | ||
| 146 | # Add to cookie alterlist for instance (will be sent back to web browser) | ||
| 147 | push(@{$self->{alterlist}->{cookie}}, "passback:$1"); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | if ($self->{conf}{KeepAuth}) { | ||
| 152 | push(@{$self->{alterlist}->{header}}, "add:Authorization:Basic " . encode_base64($user . ":" . $pass, '')); | ||
| 153 | } | ||
| 154 | |||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | # Save errors | ||
| 159 | if ($error = $response->status_line) { | ||
| 160 | $self->AddError('warn', "URL \"" . $self->{conf}{LoginUrl} . "\", Authentication failure: \"$user\": $error"); | ||
| 161 | } else { | ||
| 162 | $self->AddError('error', "URL \"" . $self->{conf}{LoginUrl} . "\", Fatal authentication failure: \"$user\": $!"); | ||
| 163 | } | ||
| 164 | # DEFAULT DENY # | ||
| 165 | return 0; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; # End of Apache::AppSamurai::AuthBasic | ||
| 169 | |||
| 170 | __END__ |