| 1 | # -*- Mode: Perl; indent-tabs-mode: nil; -*- | ||
| 2 | |||
| 3 | package Wombat::Authenticator::BasicAuthenticator; | ||
| 4 | |||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Wombat::Authenticator::BasicAuthenticator - HTTP Basic authenticator | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Implementation of HTTP Basic authentication as specified in RFC 2617. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use base qw(Wombat::Authenticator::AuthenticatorBase); | ||
| 20 | use fields qw(); | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use MIME::Base64 (); | ||
| 25 | use Servlet::Http::HttpServletResponse (); | ||
| 26 | |||
| 27 | use constant METHOD_BASIC => 'BASIC'; | ||
| 28 | |||
| 29 | =pod | ||
| 30 | |||
| 31 | =head1 PUBLIC METHODS | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item authenticate($request, $response, $config) | ||
| 36 | |||
| 37 | Authenticate the user making this request, based on the specified | ||
| 38 | login configuration. Return true if any specified constraint has been | ||
| 39 | satisfied, or false if we have created a response already. | ||
| 40 | |||
| 41 | B<Parameters:> | ||
| 42 | |||
| 43 | =over | ||
| 44 | |||
| 45 | =item $request | ||
| 46 | |||
| 47 | the B<Wombat::HttpRequest> being processed | ||
| 48 | |||
| 49 | =item $response | ||
| 50 | |||
| 51 | the B<Wombat::HttpResponse> being created | ||
| 52 | |||
| 53 | =item $constraint | ||
| 54 | |||
| 55 | the B<Wombat::Deploy::LoginConfig> describing the authentication | ||
| 56 | procedure | ||
| 57 | |||
| 58 | =back | ||
| 59 | |||
| 60 | B<Throws:> | ||
| 61 | |||
| 62 | =over | ||
| 63 | |||
| 64 | =item B<Servlet::Util::IOException> | ||
| 65 | |||
| 66 | if an input or output error occurs | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub authenticate { | ||
| 73 | my $self = shift; | ||
| 74 | my $request = shift; | ||
| 75 | my $response = shift; | ||
| 76 | my $config = shift; | ||
| 77 | |||
| 78 | my $freq = $request->getRequest(); | ||
| 79 | my $fres = $request->getResponse(); | ||
| 80 | |||
| 81 | my $principal = $freq->getUserPrincipal(); | ||
| 82 | return 1 if $principal; | ||
| 83 | |||
| 84 | my $authorization = $request->getAuthorization(); | ||
| 85 | if ($authorization) { | ||
| 86 | $principal = $self->findPrincipal($authorization, | ||
| 87 | $self->getContainer()->getRealm()); | ||
| 88 | if ($principal) { | ||
| 89 | $self->register($request, $response, $principal, METHOD_BASIC); | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | my $realmName = $config->getRealmName() || | ||
| 95 | join ':', $freq->getServerName(), $freq->getServerPort(); | ||
| 96 | $fres->setHeader('WWW-Authenticate', qq(Basic realm="$realmName")); | ||
| 97 | |||
| 98 | my $code = Servlet::Http::HttpServletResponse::SC_UNAUTHORIZED; | ||
| 99 | $fres->setStatus($code); | ||
| 100 | |||
| 101 | return undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =item getName() | ||
| 107 | |||
| 108 | Return a short name for this Authenticator implementation. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub getName { | ||
| 113 | return 'Basic Authenticator'; | ||
| 114 | } | ||
| 115 | |||
| 116 | =pod | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | # private methods | ||
| 123 | |||
| 124 | sub findPrincipal { | ||
| 125 | my $self = shift; | ||
| 126 | my $authorization = shift; | ||
| 127 | my $realm = shift; | ||
| 128 | |||
| 129 | return undef unless $authorization; | ||
| 130 | return undef unless $authorization =~ s|^Basic ||; | ||
| 131 | |||
| 132 | my $unencoded = MIME::Base64::decode_base64($authorization); | ||
| 133 | my ($username, $password) = split /:/, $unencoded; | ||
| 134 | |||
| 135 | return $realm->authenticate($username, $password); | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | __END__ |