« Gungho/Component/RobotRules/Storage/Cache.pm
↑ Summary
Gungho/Component/RobotRules/Storage/DB_File.pm »
| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/Authentication/Basic.pm 1736 2007-05-15T09:58:12.998753Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Component::Authentication::Basic; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component::Authentication); | ||
| 10 | use MIME::Base64 (); | ||
| 11 | use URI; | ||
| 12 | |||
| 13 | __PACKAGE__->mk_classdata($_) for qw(basic_authentication); | ||
| 14 | |||
| 15 | sub setup | ||
| 16 | { | ||
| 17 | my $self = shift; | ||
| 18 | $self->basic_authentication({}); | ||
| 19 | |||
| 20 | my $list = $self->config->{credentials}{basic}; | ||
| 21 | if ($list) { | ||
| 22 | foreach my $conf (@$list) { | ||
| 23 | $self->set_basic_credentials(@$conf); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | $self->next::method(@_); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub authenticate | ||
| 31 | { | ||
| 32 | my ($self, $proxy, $auth_param, $req, $res) = @_; | ||
| 33 | |||
| 34 | my ($user, $pass) = $self->get_basic_credentials($req->url, $auth_param->{realm}); | ||
| 35 | |||
| 36 | return 0 unless defined $user and defined $pass; | ||
| 37 | |||
| 38 | my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization"; | ||
| 39 | my $auth_value = "Basic " . MIME::Base64::encode("$user:$pass", ""); | ||
| 40 | |||
| 41 | # Check if this is a repeated fialure to auth | ||
| 42 | my $r = $res; | ||
| 43 | while ($r) { | ||
| 44 | my $auth = $r->request->header($auth_header); | ||
| 45 | if ($auth && $auth eq $auth_value) { | ||
| 46 | # here we know this failed before | ||
| 47 | $res->header("Client-Warning" => | ||
| 48 | "Credentials for '$user' failed before"); | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | $r = $r->previous; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $referral = $req->clone; | ||
| 55 | $referral->header($auth_header => $auth_value); | ||
| 56 | |||
| 57 | $self->send_request($referral); | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub set_basic_credentials | ||
| 62 | { | ||
| 63 | my $self = shift; | ||
| 64 | my ($uri, $realm, $uid, $pass) = @_; | ||
| 65 | |||
| 66 | if (! eval { $uri->isa('URI') }) { | ||
| 67 | $uri = URI->new($uri); | ||
| 68 | } | ||
| 69 | |||
| 70 | $self->basic_authentication()->{lc ($uri->host_port)}{$realm} = [$uid, $pass]; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub get_basic_credentials | ||
| 74 | { | ||
| 75 | my $self = shift; | ||
| 76 | my ($uri, $realm) = @_; | ||
| 77 | |||
| 78 | if (! eval { $uri->isa('URI') }) { | ||
| 79 | $uri = URI->new($uri); | ||
| 80 | } | ||
| 81 | |||
| 82 | if (exists $self->basic_authentication()->{lc($uri->host_port)}{$realm}) { | ||
| 83 | return @{$self->basic_authentication()->{lc($uri->host_port)}{$realm}}; | ||
| 84 | } | ||
| 85 | return (undef, undef); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |