File Coverage

lib/Google/RestApi/Auth/ServiceAccount.pm
Criterion Covered Total %
statement 13 28 46.4
branch 0 2 0.0
condition n/a
subroutine 4 6 66.6
pod 0 3 0.0
total 17 39 43.5


line stmt bran cond sub pod time code
1              
2             our $VERSION = '1.0.2';
3              
4             use Google::RestApi::Setup;
5 1     1   639  
  1         2  
  1         15  
6             use WWW::Google::Cloud::Auth::ServiceAccount ();
7 1     1   21382  
  1         613996  
  1         51  
8             use parent 'Google::RestApi::Auth';
9 1     1   9  
  1         2  
  1         10  
10             my $class = shift;
11              
12 1     1 0 3 my %p = @_;
13             # order is important, resolve the overall config file first.
14 1         3 resolve_config_file_path(\%p, 'config_file');
15             resolve_config_file_path(\%p, 'account_file');
16 1         9
17 1         3 my $self = merge_config_file(%p);
18             state $check = compile_named(
19 0           config_dir => ReadableDir, { optional => 1 },
20 0           config_file => ReadableFile, { optional => 1 },
21             account_file => ReadableFile,
22             scope => ArrayRef[Str],
23             );
24             $self = $check->(%$self);
25             $self = bless $self, $class;
26 0            
27 0           my $auth = WWW::Google::Cloud::Auth::ServiceAccount->new(
28             credentials_path => $self->{account_file},
29             # undocumented feature of WWW::Google::Cloud::Auth::ServiceAccount
30             scope => join(' ', @{ $self->{scope} }),
31             );
32 0           $self->{auth} = $auth;
  0            
33              
34 0           return $self;
35             }
36 0            
37             my $self = shift;
38             my $access_token = $self->access_token();
39             $self->{headers} = [ Authorization => "Bearer $access_token" ];
40 0     0 0   return $self->{headers};
41 0           }
42 0            
43 0           my $self = shift;
44             $self->{access_token} = $self->{auth}->get_token()
45             or LOGDIE "Service Account Auth failed";
46             return $self->{access_token};
47 0     0 0   }
48              
49 0 0         1;
50 0            
51              
52             =head1 NAME
53              
54             Google::RestApi::Auth::ServiceAccount - Service Account support for Google Rest APIs
55              
56             =head1 SYNOPSIS
57              
58             use Google::RestApi::Auth::ServiceAccount;
59              
60             my $sa = Google::RestApi::Auth::ServiceAccount->new(
61             account_file => <path_to_account_json_file>,
62             scope => ['http://spreadsheets.google.com/feeds/'],
63             );
64             # generate an access token from the code returned from Google:
65             my $token = $sa->access_token($code);
66              
67             =head1 AUTHOR
68              
69             Robin Murray E<lt>mvsjes@cpan.ork<gt>.
70              
71             =head1 SEE ALSO
72              
73             L<WWW::Google::Cloud::Auth::ServiceAccount>
74              
75             L<https://developers.google.com/accounts/docs/OAuth2>
76              
77             =head1 LICENSE
78              
79             This library is free software; you can redistribute it and/or modify
80             it under the same terms as Perl itself.