From 1a6d578ccb2315d09ce6d0694912f9ca196d986b Mon Sep 17 00:00:00 2001 From: franck cuny Date: Wed, 15 Sep 2010 11:18:36 +0200 Subject: auths mw extends auth --- lib/Net/HTTP/Spore/Middleware/Auth.pm | 10 ++++++++++ lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm | 17 +++++++++++++++-- lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm | 21 +++++++++++++++++++-- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 lib/Net/HTTP/Spore/Middleware/Auth.pm (limited to 'lib/Net') diff --git a/lib/Net/HTTP/Spore/Middleware/Auth.pm b/lib/Net/HTTP/Spore/Middleware/Auth.pm new file mode 100644 index 0000000..619215c --- /dev/null +++ b/lib/Net/HTTP/Spore/Middleware/Auth.pm @@ -0,0 +1,10 @@ +package Net::HTTP::Spore::Middleware::Auth; + +use Moose; +extends 'Net::HTTP::Spore::Middleware'; + +sub should_authenticate { $_[1]->env->{'spore.authentication'} } + +sub call { die "should be implemented" } + +1; diff --git a/lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm b/lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm index 18c1e16..ce7d6d8 100644 --- a/lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm +++ b/lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm @@ -1,9 +1,11 @@ package Net::HTTP::Spore::Middleware::Auth::Basic; +# ABSTRACT: middleware for Basic authentication + use Moose; -use MIME::Base64; +extends 'Net::HTTP::Spore::Middleware::Auth'; -extends 'Net::HTTP::Spore::Middleware'; +use MIME::Base64; has username => (isa => 'Str', is => 'rw', predicate => 'has_username'); has password => (isa => 'Str', is => 'rw', predicate => 'has_password'); @@ -11,6 +13,8 @@ has password => (isa => 'Str', is => 'rw', predicate => 'has_password'); sub call { my ( $self, $req ) = @_; + return unless $self->should_authenticate($req); + if ( $self->has_username && $self->has_password ) { $req->header( 'Authorization' => 'Basic ' @@ -22,3 +26,12 @@ sub call { } 1; + +=head1 SYNOPSIS + + my $client = Net::HTTP::Spore->new_from_spec('github.json'); + $client->enable('Auth::Basic', username => 'xxx', password => 'yyy'); + +=head1 DESCRIPTION + +Net::HTTP::Spore::Middleware::Auth::Basic is a middleware to handle Basic authentication mechanism. diff --git a/lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm b/lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm index e389bed..3fb5bf0 100644 --- a/lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm +++ b/lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm @@ -1,7 +1,9 @@ package Net::HTTP::Spore::Middleware::Auth::OAuth; +# ABSTRACT: middleware for OAuth authentication + use Moose; -extends 'Net::HTTP::Spore::Middleware'; +extends 'Net::HTTP::Spore::Middleware::Auth'; use Net::OAuth; use MIME::Base64; @@ -15,7 +17,7 @@ has [qw/consumer_key consumer_secret token token_secret/] => ( sub call { my ( $self, $req ) = @_; - return unless $req->env->{'spore.authentication'} == 1; + return unless $self->should_authenticate($req); my $uri = $req->uri; my $request = Net::OAuth->request('protected resource')->new( @@ -38,3 +40,18 @@ sub call { } 1; + +=head1 SYNOPSIS + + my $client = Net::HTTP::Spore->new_from_spec('twitter.json'); + $client->enable( + 'Auth::OAuth', + consumer_key => 'xxx', + consumer_secret => 'yyy', + token => '123', + token_secret => '456' + ); + +=head1 DESCRIPTION + +Net::HTTP::Spore::Middleware::Auth::OAuth is a middleware to handle OAuth mechanism. This middleware should be loaded as the last middleware, because it requires all parameters to be setted to calculate the signature. -- cgit 1.4.1