DataAPI/Auth/OAuth.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 12 / 12 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 4 totaltotal 96.6% 115 / 119 CC 8 SCAR 6.7
CC
8
Cov
100%
_oauth_request
4.0
get_authorize_token_url
3.0
get_request_token
2.0
CRAP 2.0
« DataAPI/Role/HasContent.pm ↑ Summary DataAPI/Types.pm »
1 package Net::Google::DataAPI::Auth::OAuth;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Net::Google::DataAPI::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 with 'Net::Google::DataAPI::Role::Auth';
5 1 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
10 1 our $VERSION = '0.05';
11  
12 1 has [qw(consumer_key consumer_secret)] => ( is => 'ro', isa => 'Str', required => 1 );
13  
14 1 for my $attr (qw(
15 request_token
16 request_token_secret
17 access_token
18 access_token_secret
19 )) {
20 4 has $attr => (
21 is => 'rw',
22 isa => 'Str',
23 clearer => "clear_$attr",
24 predicate => "has_$attr"
25 );
26 }
27  
28 1 has scope => ( is => 'ro', isa => 'ArrayRef[Str]', required => 1, auto_deref => 1 );
29 1 has callback => ( is => 'ro', isa => 'Net::Google::DataAPI::Types::URI', coerce => 1 );
30 1 has signature_method => ( is => 'ro', isa => 'Str', default => 'HMAC-SHA1' );
31 1 has authorize_token_hd => ( is => 'ro', isa => 'Str', default => 'default' );
32 1 has authorize_token_hl => ( is => 'ro', isa => 'Str', default => 'en' );
33 1 has mobile => ( is => 'ro', isa => 'Bool', default => 0 );
34 1 has ua => ( is => 'ro', isa => 'LWP::UserAgent', required => 1, lazy_build => 1 );
35 sub _build_ua {
36 3 ▶ LWP::UserAgent->new( max_redirect => 0 );
Subroutine _build_ua: called CC=1 SCAR=0.0
37 }
38  
39 1 my $url_hash = {
40 get_request_token_url => 'https://www.google.com/accounts/OAuthGetRequestToken',
41 authorize_token_url => 'https://www.google.com/accounts/OAuthAuthorizeToken',
42 get_access_token_url => 'https://www.google.com/accounts/OAuthGetAccessToken',
43 };
44  
45 1 ▶while ( my ($key, $url) = each %$url_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$url_hash) 3 1 4
46 12 ▶ has $key => ( is => 'ro', isa => 'URI', required => 1, default => sub {URI->new($url)} );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48  
49 sub get_request_token {
50 7 ▶ my ($self, $args) = @_;
Subroutine get_request_token: called CC=2 SCAR=6.9
Pod undocumented
51 7 ▶ my $res = $self->_oauth_request(
Condition100%2/2
$self->callback || 'oob'
l!l
61
MC/DC100%1/1
$self->callback || 'oob'
$self->callback
Truth table100%2/2
A
A= $self->callback
Aresult
11
00
52 'request token',
53 {
54 request_url => $self->get_request_token_url,
55 extra_params => {
56 scope => join(' ', $self->scope),
57 },
58 callback => $self->callback || 'oob',
59 }
60 );
61 3 my ($token, $secret) = $self->_res_to_token($res);
62 3 $self->request_token($token);
63 3 $self->request_token_secret($secret);
64 3 return $self;
65 }
66  
67 sub get_authorize_token_url {
68 3 ▶ my ($self) = @_;
Subroutine get_authorize_token_url: called CC=3 SCAR=11.0
Pod undocumented
69 3 ▶ $self->has_request_token or $self->get_request_token;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_request_token or $self->get_request_token 2 1 3
70 3 my $url = $self->authorize_token_url;
71 3 ▶ $url->query_form(
Branch: 1 branch
BranchTrueFalseTotal
$self->mobile ? : 2 1 3
72 oauth_token => $self->request_token,
73 hd => $self->authorize_token_hd,
74 hl => $self->authorize_token_hl,
75 $self->mobile ? ( btmpl => 'mobile' ) : (),
76 );
77 3 return $url;
78 }
79  
80 sub get_access_token {
81 5 ▶ my ($self, $args) = @_;
Subroutine get_access_token: called CC=2 SCAR=6.9
Pod undocumented
82 my $res = $self->_oauth_request(
83 'access token',
84 {
85 request_url => $self->get_access_token_url,
86 token => $self->request_token,
87 token_secret => $self->request_token_secret,
88 5 ▶ %{$args || {}},
Branch: 1 branch
BranchTrueFalseTotal
$args or {} 2 3 5
89 }
90 );
91 # now clear them.
92 3 $self->clear_request_token;
93 3 $self->clear_request_token_secret;
94 3 my ($token, $secret) = $self->_res_to_token($res);
95 3 $self->access_token($token);
96 3 $self->access_token_secret($secret);
97 }
98  
99 sub _oauth_request {
100 12 ▶ my ($self, $type, $args) = @_;
Subroutine _oauth_request: called CC=4 SCAR=13.9
101 12 my $req = $self->_make_oauth_request($type, $args);
102 10 my $res = $self->ua->get($req->to_url);
103 10 ▶ unless ($res && $res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res and $res->is_success) 4 6 10
Condition100%3/3
$res and $res->is_success
!ll&&!rl&&r
226
MC/DC100%2/2
$res and $res->is_success
$res $res->is_success
Truth table100%3/3
A and B
A= $resB= $res->is_success
ABresult
0X0
100
111
104 4 ▶ confess sprintf "request failed: %s", $res ? $res->status_line : 'no response returned';
Branch: 1 branch
BranchTrueFalseTotal
$res ? : 2 2 4
105 }
106 6 return $res;
107 }
108  
109 sub _make_oauth_request {
110 18 ▶ my ($self, $type, $args) = @_;
Subroutine _make_oauth_request: called CC=1 SCAR=0.0
111 18 my $req = Net::OAuth->request($type)->new(
112 version => '1.0',
113 consumer_key => $self->consumer_key,
114 consumer_secret => $self->consumer_secret,
115 request_method => 'GET',
116 signature_method => $self->signature_method,
117 timestamp => time,
118 nonce => Digest::SHA1::sha1_base64(time . $$ . rand),
119 %$args,
120 );
121 13 $req->sign;
122 13 return $req;
123 }
124  
125 sub _res_to_token {
126 6 ▶ my ($self, $res) = @_;
Subroutine _res_to_token: called CC=1 SCAR=0.0
127 6 my $uri = URI->new;
128 6 $uri->query($res->content);
129 6 my %query = $uri->query_form;
130 6 return @query{qw(oauth_token oauth_token_secret)};
131 }
132  
133 sub sign_request {
134 6 ▶ my ($self, $req) = @_;
Subroutine sign_request: called CC=1 SCAR=0.0
Pod undocumented
135 6 my $sign = $self->_make_oauth_request(
136 'protected resource',
137 {
138 request_url => $req->uri,
139 request_method => $req->method,
140 token => $self->access_token,
141 token_secret => $self->access_token_secret,
142 }
143 );
144 3 $req->header(Authorization => $sign->to_authorization_header);
145 3 return $req;
146 }
147  
148 1 __PACKAGE__->meta->make_immutable;
149 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 1 1;
152 __END__
153  
154 =head1 NAME
155  
156 Net::Google::DataAPI::Auth::OAuth - (DEPRECATED) OAuth 1.0 support for Google Data APIs
157  
158 =head1 SYNOPSIS
159  
160 use Net::Google::DataAPI::Auth::OAuth;
161  
162 my $auth = Net::Google::DataAPI::Auth::OAuth->new(
163 consumer_key => 'consumer.example.com',
164 consumer_secret => 'mys3cr3t',
165 scope => ['http://spreadsheets.google.com/feeds/'],
166 );
167 my $url = $auth->get_authorize_token_url;
168  
169 # show the user $url and get $verifier
170  
171 $auth->get_access_token({verifier => $verifier}) or die;
172 my $token = $auth->access_token;
173 my $secret = $auth->access_token_secret;
174  
175 =head1 DESCRIPTION
176  
177 Net::Google::DataAPI::Auth::OAuth interacts with google OAuth service
178 and adds Authorization header to given request.
179  
180 =head1 DEPRECATED!
181  
182 OAuth1.0 protocol is deprecated.
183 Use L<Net::Google::DataAPI::Auth::OAuth2> instead.
184  
185 =head1 ATTRIBUTES
186  
187 You can make Net::Google::DataAPI::Auth::OAuth instance with those arguments below:
188  
189 =over 2
190  
191 =item * consumer_key
192  
193 Consumer key. You can get it at L<https://www.google.com/accounts/ManageDomains>.
194  
195 =item * consumer_secret
196  
197 The consumer secret paired with the consumer key.
198  
199 =item * scope
200  
201 URL identifying the service(s) to be accessed. You can see the list of the urls to use at L<http://code.google.com/intl/en-US/apis/gdata/faq.html#AuthScopes>.
202  
203 =item * callback
204  
205 OAuth callback url. 'oob' will be used if you don't specify it.
206  
207 =item * signature_method
208  
209 Signature method. The default is 'HMAC-SHA1'.
210  
211 =item * authorize_token_hd
212  
213 Set hosted domain account for hosted google apps users. Defaults to 'default'.
214  
215 =item * authorize_token_hl
216  
217 An ISO 639 country code to set authorize user interface language. Defaults to 'en'.
218  
219 =item * mobile
220  
221 A boolean value whether you use this auth with mobile or not. defaults to 0.
222  
223 =back
224  
225 See L<http://code.google.com/intl/en-US/apis/accounts/docs/OAuth_ref.html> for details.
226  
227 =head1 AUTHOR
228  
229 Nobuo Danjou E<lt>danjou@soffritto.orgE<gt>
230  
231 =head1 SEE ALSO
232  
233 L<Net::Google::AuthSub>
234  
235 L<Net::OAuth>
236  
237 L<Net::Twitter::Role::OAuth>
238  
239 =head1 LICENSE
240  
241 This library is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself.
243  
244 =cut
« DataAPI/Role/HasContent.pm ↑ Summary DataAPI/Types.pm »