root/trunk/plagger/lib/Plagger/Plugin/UserAgent/AuthenRequest.pm

Revision 1248 (checked in by miyagawa, 14 years ago)

Update UserAgent?::AuthenRequest? to use $agent->credentials() instead of overriding request() method.
Hack Publish::MT and XMLRPC::Lite internal to use Plagger::UserAgent? instead of LWP.

Line 
1 package Plagger::Plugin::UserAgent::AuthenRequest;
2 use strict;
3 use warnings;
4 use base qw/Plagger::Plugin/;
5
6 use LWP::UserAgent;
7 use List::Util qw/first/;
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'useragent.request' => \&add_credentials,
14     );
15 }
16
17 sub add_credentials {
18     my($self, $context, $args) = @_;
19
20     my $creds = $self->conf->{credentials} || [ $self->conf ];
21
22     my $uri = URI->new($args->{url});
23     for my $auth (grep { $_->{host} eq $uri->host_port } @$creds) {
24         $context->log(info => "Adding credential to $auth->{realm} at $auth->{host}");
25         $args->{ua}->credentials($auth->{host}, $auth->{realm}, $auth->{username}, $auth->{password});
26     }
27 }
28
29 1;
30
31 __END__
32
33 =head1 NAME
34
35 Plagger::Plugin::UserAgent::AuthenRequest - Plagger plugin for authen request
36
37 =head1 SYNOPSYS
38
39   - module: UserAgent::AuthenRequest
40     config:
41       host: example.com:80
42       auth: basic
43       realm: Security Area
44       username: username
45       password: password
46
47 =head1 DESCRIPTION
48
49 This plugin hooks Plagger::UserAgent fetch method to add username and
50 password to authenticated website. Since it hooks Plagger::UserAgent,
51 the config will be enabled in all plugins that uses Plagger::UserAgent
52 inside, e.g. from Aggregator::Simple to Publish::MT.
53
54 =head1 AUTHOR
55
56 Daisuke Murase <typester@cpan.org>
57
58 =head1 SEE ALSO
59
60 L<Plagger>
61
62 =cut
Note: See TracBrowser for help on using the browser.