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