| 1 | package Plagger::Plugin::UserAgent::AuthenRequest; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw/Plagger::Plugin/; | ||
| 5 | |||
| 6 | use List::Util qw/first/; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my($self, $context) = @_; | ||
| 10 | $context->register_hook( | ||
| 11 | $self, | ||
| 12 | 'useragent.init' => \&add_credentials, | ||
| 13 | ); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub add_credentials { | ||
| 17 | my($self, $context, $args) = @_; | ||
| 18 | |||
| 19 | my $creds = $self->conf->{credentials} || [ $self->conf ]; | ||
| 20 | |||
| 21 | for my $auth (@$creds) { | ||
| 22 | $context->log(info => "Adding credential to $auth->{realm} at $auth->{host}"); | ||
| 23 | $args->{ua}->credentials($auth->{host}, $auth->{realm}, $auth->{username}, $auth->{password}); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | 1; | ||
| 28 | |||
| 29 | __END__ |