Changeset 1248

Show
Ignore:
Timestamp:
08/09/06 18:06:14
Author:
miyagawa
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin/Publish/MT.pm

    r1247 r1248  
    77 
    88use Net::MovableType; 
     9use SOAP::Transport::HTTP; # need to preload 
    910 
    1011sub register { 
     
    1819sub mt { 
    1920    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 
    2025    return $self->{mt} if $self->{mt}; 
    2126    $self->{mt} = Net::MovableType->new($self->conf->{rsd}); 
  • trunk/plagger/lib/Plagger/Plugin/UserAgent/AuthenRequest.pm

    r1246 r1248  
    77use List::Util qw/first/; 
    88 
    9 sub new
    10     my $self = shift->SUPER::new(@_)
    11  
    12     $self->inject; 
    13  
    14     $self
     9sub register
     10    my($self, $context) = @_
     11    $context->register_hook( 
     12        $self, 
     13        'useragent.request' => \&add_credentials, 
     14    )
    1515} 
    1616 
    17 sub register {} 
     17sub add_credentials { 
     18    my($self, $context, $args) = @_; 
    1819 
    19 sub inject { 
    20     my $self = shift; 
     20    my $creds = $self->conf->{credentials} || [ $self->conf ]; 
    2121 
    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}); 
    3926    } 
    4027} 
     
    5239  - module: UserAgent::AuthenRequest 
    5340    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 
    5846 
    5947=head1 DESCRIPTION 
     48 
     49This plugin hooks Plagger::UserAgent fetch method to add username and 
     50password to authenticated website. Since it hooks Plagger::UserAgent, 
     51the config will be enabled in all plugins that uses Plagger::UserAgent 
     52inside, e.g. from Aggregator::Simple to Publish::MT. 
    6053 
    6154=head1 AUTHOR 
  • trunk/plagger/lib/Plagger/UserAgent.pm

    r1173 r1248  
    88sub new { 
    99    my $class = shift; 
    10     my $self  = $class->SUPER::new(); 
     10    my $self  = $class->SUPER::new(@_); 
    1111 
    1212    my $conf = Plagger->context->conf->{user_agent}; 
     
    3737 
    3838    $res; 
     39} 
     40 
     41sub request { 
     42    my $self = shift; 
     43    my($req) = @_; 
     44    Plagger->context->run_hook('useragent.request', { ua => $self, url => $req->uri }); 
     45    $self->SUPER::request(@_); 
    3946} 
    4047 
  • trunk/plagger/t/plugins/UserAgent-AuthenRequest/authen.t

    r1246 r1248  
    2626  - module: UserAgent::AuthenRequest 
    2727    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 
    3132--- expected 
    3233unlike $warning, qr/401 Authorization Required/; 
    3334like $warning, qr!200: http://irisresearch!; 
    3435 
     36=== With auth as list 
     37--- input config 
     38plugins: 
     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 
     51unlike $warning, qr/401 Authorization Required/; 
     52like $warning, qr!200: http://irisresearch!;