| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/Authentication.pm 1657 2007-04-10T02:26:11.598323Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # all rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Component::Authentication; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component); | ||
| 10 | use Carp qw(croak); | ||
| 11 | use HTTP::Status(); | ||
| 12 | use HTTP::Headers::Util(); | ||
| 13 | |||
| 14 | sub authenticate | ||
| 15 | { | ||
| 16 | croak ref($_[0]) . "::authenticate() unimplemented"; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub check_authentication_challenge | ||
| 20 | { | ||
| 21 | my ($c, $req, $res) = @_; | ||
| 22 | |||
| 23 | my $handled = 0; | ||
| 24 | |||
| 25 | # Check if there was a Auth challenge. If yes and Gungho is configured | ||
| 26 | # to support authentication, then do the auth magic | ||
| 27 | my $code = $res->code; | ||
| 28 | |||
| 29 | if ( $code == &HTTP::Status::RC_UNAUTHORIZED || | ||
| 30 | $code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED ) | ||
| 31 | { | ||
| 32 | my $proxy = ($code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED); | ||
| 33 | my $ch_header = $proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; | ||
| 34 | my @challenge = $res->header($ch_header); | ||
| 35 | |||
| 36 | if (! @challenge) { | ||
| 37 | $c->log->debug("Response from " . $req->uri . " returned with code = $code, but is missing Authenticate header"); | ||
| 38 | $res->header("Client-Warning" => "Missing Authenticate header"); | ||
| 39 | goto DONE; | ||
| 40 | } | ||
| 41 | CHALLENGE: | ||
| 42 | for my $challenge (@challenge) { | ||
| 43 | $challenge =~ tr/,/;/; # "," is used to separate auth-params!! | ||
| 44 | ($challenge) = HTTP::Headers::Util::split_header_words($challenge); | ||
| 45 | my $scheme = lc(shift(@$challenge)); | ||
| 46 | shift(@$challenge); # no value | ||
| 47 | $challenge = { @$challenge }; # make rest into a hash | ||
| 48 | for (keys %$challenge) { # make sure all keys are lower case | ||
| 49 | $challenge->{lc $_} = delete $challenge->{$_}; | ||
| 50 | } | ||
| 51 | |||
| 52 | unless ($scheme =~ /^([a-z]+(?:-[a-z]+)*)$/) { | ||
| 53 | $c->log->debug("Response from " . $req->uri . " returned with code = $code, bad authentication scheme '$scheme'"); | ||
| 54 | $res->header("Client-Warning" => "Bad authentication scheme '$scheme'"); | ||
| 55 | goto DONE; | ||
| 56 | } | ||
| 57 | $scheme = ucfirst $1; # untainted now | ||
| 58 | |||
| 59 | if (! $c->has_feature("Authentication::$scheme")) { | ||
| 60 | $c->log->debug("Response from " . $req->uri . " returned with code = $code, but authentication scheme '$scheme' is unsupported"); | ||
| 61 | goto DONE; | ||
| 62 | } | ||
| 63 | |||
| 64 | # now attempt to authenticate | ||
| 65 | return $c->authenticate($proxy, $challenge, $req, $res); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | DONE: | ||
| 70 | return $handled; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ |