Changeset 1248
- Timestamp:
- 08/09/06 18:06:14
- Files:
-
- trunk/plagger/lib/Plagger/Plugin/Publish/MT.pm (modified) (2 diffs)
- trunk/plagger/lib/Plagger/Plugin/UserAgent/AuthenRequest.pm (modified) (2 diffs)
- trunk/plagger/lib/Plagger/UserAgent.pm (modified) (2 diffs)
- trunk/plagger/t/plugins/UserAgent-AuthenRequest/authen.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plagger/lib/Plagger/Plugin/Publish/MT.pm
r1247 r1248 7 7 8 8 use Net::MovableType; 9 use SOAP::Transport::HTTP; # need to preload 9 10 10 11 sub register { … … 18 19 sub mt { 19 20 my $self = shift; 21 22 # hack to replace XMLRPC::Lite internal UserAgent class so we can add credentials 23 local $SOAP::Transport::HTTP::Client::USERAGENT_CLASS = "Plagger::UserAgent"; 24 20 25 return $self->{mt} if $self->{mt}; 21 26 $self->{mt} = Net::MovableType->new($self->conf->{rsd}); trunk/plagger/lib/Plagger/Plugin/UserAgent/AuthenRequest.pm
r1246 r1248 7 7 use List::Util qw/first/; 8 8 9 sub new{10 my $self = shift->SUPER::new(@_);11 12 $self->inject;13 14 $self;9 sub register { 10 my($self, $context) = @_; 11 $context->register_hook( 12 $self, 13 'useragent.request' => \&add_credentials, 14 ); 15 15 } 16 16 17 sub register {} 17 sub add_credentials { 18 my($self, $context, $args) = @_; 18 19 19 sub inject { 20 my $self = shift; 20 my $creds = $self->conf->{credentials} || [ $self->conf ]; 21 21 22 { 23 no warnings 'redefine'; 24 25 *LWP::UserAgent::__request__ = \&LWP::UserAgent::request; 26 *LWP::UserAgent::request = sub { 27 my $agent = shift; 28 my $req = shift; 29 30 my $auth = first { $req->uri =~ /$_/ } keys %{ $self->conf }; 31 $auth = $self->conf->{$auth}; 32 33 if ( $auth && ($auth->{auth}||'basic') eq 'basic' ) { # todo: other authentication support 34 $req->headers->authorization_basic( $auth->{username}, $auth->{password} ); 35 } 36 37 $agent->__request__( $req, @_ ); 38 }; 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}); 39 26 } 40 27 } … … 52 39 - module: UserAgent::AuthenRequest 53 40 config: 54 '^http://example.com/': 55 auth: basic 56 username: username 57 password: password 41 host: example.com:80 42 auth: basic 43 realm: Security Area 44 username: username 45 password: password 58 46 59 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. 60 53 61 54 =head1 AUTHOR trunk/plagger/lib/Plagger/UserAgent.pm
r1173 r1248 8 8 sub new { 9 9 my $class = shift; 10 my $self = $class->SUPER::new( );10 my $self = $class->SUPER::new(@_); 11 11 12 12 my $conf = Plagger->context->conf->{user_agent}; … … 37 37 38 38 $res; 39 } 40 41 sub request { 42 my $self = shift; 43 my($req) = @_; 44 Plagger->context->run_hook('useragent.request', { ua => $self, url => $req->uri }); 45 $self->SUPER::request(@_); 39 46 } 40 47 trunk/plagger/t/plugins/UserAgent-AuthenRequest/authen.t
r1246 r1248 26 26 - module: UserAgent::AuthenRequest 27 27 config: 28 '^http://irisresearch.library.cornell.edu/': 29 username: test 30 password: this 28 host: irisresearch.library.cornell.edu:80 29 realm: "User: test Pass:" 30 username: test 31 password: this 31 32 --- expected 32 33 unlike $warning, qr/401 Authorization Required/; 33 34 like $warning, qr!200: http://irisresearch!; 34 35 36 === With auth as list 37 --- input config 38 plugins: 39 - module: Subscription::Config 40 config: 41 feed: 42 - http://irisresearch.library.cornell.edu/control/authBasic/authTest/ 43 - module: UserAgent::AuthenRequest 44 config: 45 credentials: 46 - host: irisresearch.library.cornell.edu:80 47 realm: "User: test Pass:" 48 username: test 49 password: this 50 --- expected 51 unlike $warning, qr/401 Authorization Required/; 52 like $warning, qr!200: http://irisresearch!;
