File Coverage

blib/lib/WWW/JSON/Role/Authentication/OAuth1.pm
Criterion Covered Total %
statement 24 24 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 31 32 96.8


line stmt bran cond sub pod time code
1             package WWW::JSON::Role::Authentication::OAuth1;
2 8     8   3251 use Moo::Role;
  8         13  
  8         148  
3 8     8   5357 use Net::OAuth;
  8         4080  
  8         179  
4 8     8   40 use URI;
  8         10  
  8         2028  
5             $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
6             requires 'authentication';
7             requires 'ua';
8              
9             sub _validate_OAuth1 {
10 1     1   2 my ( $self, $auth ) = @_;
11 1         2 for (qw/consumer_key consumer_secret token token_secret/) {
12 4 50       27 die "Required parameter $_ missing for " . __PACKAGE__ . " authentication"
13             unless exists( $auth->{$_} );
14             }
15             }
16              
17             sub _auth_OAuth1 {
18 2     2   3 my ( $self, $auth, $req) = @_;
19 2         7 my $q = URI->new;
20             # FIXME if we're sending a JSON payload we need to decode instead of this
21 2         88 $q->query($req->content);
22 2         54 my $request = Net::OAuth->request("protected resource")->new(
23             %$auth,
24             request_url => $req->uri,
25             request_method => $req->method,
26             signature_method => 'HMAC-SHA1',
27             timestamp => time(),
28             nonce => _nonce(),
29             extra_params => {$q->query_form},
30             );
31 2         745 $request->sign;
32 2         8492 $request->to_authorization_header;
33 2         1176 $req->header( Authorization => $request->to_authorization_header );
34             }
35              
36             sub _nonce {
37 2     2   8171 my @chars = ( 'A' .. 'Z', 'a' .. 'z', '0' .. '9' );
38 2         4 my $nonce = time;
39 2         6 for ( 1 .. 15 ) {
40 30         67 $nonce .= $chars[ rand @chars ];
41             }
42 2         20 return $nonce;
43             }
44             1;