diff options
author | franck cuny <franck@lumberjaph.net> | 2010-09-15 11:18:36 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-09-15 11:18:36 +0200 |
commit | 1a6d578ccb2315d09ce6d0694912f9ca196d986b (patch) | |
tree | d68442d1e269b88c64c3a5243c6309c4169c2e81 | |
parent | add pod (diff) | |
download | net-http-spore-1a6d578ccb2315d09ce6d0694912f9ca196d986b.tar.gz |
auths mw extends auth
-rw-r--r-- | lib/Net/HTTP/Spore/Middleware/Auth.pm | 10 | ||||
-rw-r--r-- | lib/Net/HTTP/Spore/Middleware/Auth/Basic.pm | 17 | ||||
-rw-r--r-- | lib/Net/HTTP/Spore/Middleware/Auth/OAuth.pm | 21 |
3 files changed, 44 insertions, 4 deletions
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. |