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   3804 use Moo::Role;
  8         14  
  8         174  
3 8     8   6197 use Net::OAuth;
  8         4334  
  8         189  
4 8     8   43 use URI;
  8         9  
  8         2290  
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         3 for (qw/consumer_key consumer_secret token token_secret/) {
12 4 50       26 die "Required parameter $_ missing for " . __PACKAGE__ . " authentication"
13             unless exists( $auth->{$_} );
14             }
15             }
16              
17             sub _auth_OAuth1 {
18 2     2   17 my ( $self, $auth, $req) = @_;
19 2         8 my $q = URI->new;
20             # FIXME if we're sending a JSON payload we need to decode instead of this
21 2         98 $q->query($req->content);
22 2         56 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         547 $request->sign;
32 2         7887 $request->to_authorization_header;
33 2         1008 $req->header( Authorization => $request->to_authorization_header );
34             }
35              
36             sub _nonce {
37 2     2   7797 my @chars = ( 'A' .. 'Z', 'a' .. 'z', '0' .. '9' );
38 2         3 my $nonce = time;
39 2         5 for ( 1 .. 15 ) {
40 30         74 $nonce .= $chars[ rand @chars ];
41             }
42 2         16 return $nonce;
43             }
44             1;