RestApi/Auth/OAuth2Client.pm

filter: Statementstmt 41.2% 33 / 80 Branchbran 0.0% 0 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 53.3% 8 / 15 Podpod 22.2% 2 / 9 totaltotal 36.3% 44 / 121 CC 9 SCAR 16.8
CC
9
Cov
35%
access_token
12.0
userinfo
12.0
headers
6.0
CRAP 5.3
« RestApi/SheetsApi4/Range.pm ↑ Summary RestApi/SheetsApi4/Worksheet.pm »
1 package Google::RestApi::Auth::OAuth2Client;
2  
3 2 our $VERSION = '2.2.5';
4  
5 2 ▶use Google::RestApi::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # this was taken from Net::Google::DataAPI::Auth::OAuth2 and had a moose-ectomy. this will
8 # get rid of warnings about switching to Moo instead of Moose::Any.
9 #
10 # NOTE NOTE NOTE: to generate a config file and token file for use by this module, see:
11 # bin/google_restapi_oauth_token_creator in this package.
12  
13 2 ▶use Net::OAuth2::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Net::OAuth2::Profile::WebServer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Storable qw( retrieve );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use parent 'Google::RestApi::Auth';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 142 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
21 142 my $class = shift;
22  
23 142 my %p = @_;
24 # config_file must be resolved before merge_config_file can read it.
25 142 resolve_config_file_path(\%p, 'config_file');
26 142 my $self = merge_config_file(%p);
27 # token_file may have come from the YAML; resolve relative names against
28 # the auth config_file's dir first, then the caller-supplied config_dir.
29 142 resolve_config_file_path($self, 'token_file');
30 141 state $check = signature(
31 bless => !!0,
32 named => [
33 config_dir => ReadableDir, { optional => 1 },
34 config_file => ReadableFile, { optional => 1 },
35 token_file => ReadableFile, { optional => 1 },
36 client_id => Str,
37 client_secret => Str,
38 scope => ArrayRef[Str], { optional => 1 },
39 state => Str, { default => '' },
40 # Google blocked the old out-of-band (OOB) redirect in 2023; the
41 # loopback address is the sanctioned replacement for desktop apps.
42 # See bin/google_restapi_oauth_token_creator, which overrides this
43 # with a live 127.0.0.1:<port> callback it listens on.
44 redirect_uri => Str, { default => 'http://localhost' },
45 site => Str, { default => 'https://accounts.google.com' },
46 authorize_path => Str, { default => '/o/oauth2/auth' },
47 access_token_path => Str, { default => '/o/oauth2/token' },
48 userinfo_url => Str, { default => 'https://www.googleapis.com/oauth2/v1/userinfo' },
49 ],
50 );
51 141 $self = $check->(%$self);
52  
53 $self->{scope} ||= [ # when added to default above, check silently fails to compile.
54 141 ▶ 'https://www.googleapis.com/auth/userinfo.profile',
Condition50%1/2
$$self{"scope"} ||= ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"]
l!l
0141
MC/DC0%0/1
$$self{"scope"} ||= ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"]
$$self{"scope"}
Truth table50%1/2
A
A= $$self{"scope"}
Aresult
11
00
55 'https://www.googleapis.com/auth/userinfo.email',
56 ];
57  
58 141 return bless $self, $class;
59 }
60  
61 0 ▶sub headers {
Subroutine headers: not called CC=2 SCAR=17.9
Pod undocumented
62 0 my $self = shift;
63 0 ▶ return $self->{headers} if $self->{headers};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"headers"} 0 0 0
64  
65 $self->access_token(
66 refresh_token => retrieve($self->{token_file})->{refresh_token},
67 0 auto_refresh => 1,
68 );
69 0 $self->refresh_token();
70 0 my $access_token = $self->access_token()->access_token();
71 0 INFO("Successfully attained access token");
72  
73 0 $self->{headers} = [ Authorization => "Bearer $access_token" ];
74  
75 0 return $self->{headers};
76 }
77  
78 0 ▶sub authorize_url {
Subroutine authorize_url: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $self = shift;
80 0 my $server = $self->oauth2_webserver();
81 return $server->authorize(
82 0 scope => join(' ', @{ $self->{scope} }), @_
83 );
84 }
85  
86 0 ▶sub access_token {
Subroutine access_token: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my $self = shift;
88  
89 0 ▶ if (scalar @_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
elsif (@_) { } 0 0 0
90 0 state $check = signature(positional => [Str]);
91 0 my ($code) = $check->(@_);
92 0 my $server = $self->oauth2_webserver();
93 0 $self->{access_token} = $server->get_access_token($code);
94 0 DEBUG("Created access token:\n", Dump($self->{access_token}));
95 } elsif (@_) {
96 0 state $check = signature(
97 bless => !!0,
98 named => [
99 refresh_token => Str,
100 auto_refresh => Bool,
101 ],
102 );
103 0 my $p = $check->(@_);
104 0 $p->{profile} = $self->oauth2_webserver();
105 # DEBUG("Building access token from:\n", Dump($p)); # shows secret in the logs.
106 0 $self->{access_token} = Net::OAuth2::AccessToken->new(%$p);
107 }
108  
109 0 return $self->{access_token};
110 }
111  
112 0 ▶sub refresh_token {
Subroutine refresh_token: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ($self, $refresh_token) = @_;
114 0 DEBUG("About to refresh token");
115 0 my $server = $self->oauth2_webserver();
116 0 $server->update_access_token($self->access_token());
117 0 return $self->access_token()->refresh();
118 }
119  
120 2 ▶sub refresh_headers {
Subroutine refresh_headers: called CC=1 SCAR=0.0
Pod documented
121 2 my $self = shift;
122 2 INFO("Refreshing auth headers after 401");
123 2 delete $self->{headers};
124 2 return $self->headers();
125 }
126  
127 0 ▶sub oauth2_client {
Subroutine oauth2_client: not called CC=2 SCAR=17.9
Pod undocumented
128 0 my $self = shift;
129 0 ▶ if (!$self->{oauth2_client}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"oauth2_client"}) 0 0 0
130 0 DEBUG("Creating OAuth2 client");
131 $self->{oauth2_client} = Net::OAuth2::Client->new(
132 $self->{client_id},
133 $self->{client_secret},
134 site => $self->{site},
135 authorize_path => $self->{authorize_path},
136 access_token_path => $self->{access_token_path},
137 refresh_token_path => $self->{access_token_path},
138 0 );
139 }
140 0 return $self->{oauth2_client};
141 }
142  
143 0 ▶sub oauth2_webserver {
Subroutine oauth2_webserver: not called CC=2 SCAR=17.9
Pod undocumented
144 0 my $self = shift;
145 0 ▶ if (!$self->{oauth2_webserver}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"oauth2_webserver"}) 0 0 0
146 0 DEBUG("Creating OAuth2 web server");
147 0 my $client = $self->oauth2_client();
148 $self->{oauth2_webserver} = $client->web_server(
149 redirect_uri => $self->{redirect_uri},
150 state => $self->{state},
151 0 );
152 }
153 0 return $self->{oauth2_webserver};
154 }
155  
156 # not currently used
157 0 ▶sub userinfo {
Subroutine userinfo: not called CC=3 SCAR=24.8
Pod undocumented
158 0 my $self = shift;
159 0 my $token = $self->access_token();
160 0 my $url = URI->new($self->{userinfo_url});
161 0 my $res = $token->get($url);
162 0 ▶ $res->is_success or LOGDIE 'userinfo request failed: ' . $res->as_string;
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success or LOGDIE('userinfo request failed: ' . $res->as_string) 0 0 0
163 0 ▶ my %res_params = $self->oauth2_webserver()->params_from_response($res)
Branch: 1 branch
BranchTrueFalseTotal
my(%res_params) = $self->oauth2_webserver->params_from_response($res) or LOGDIE("params_from_response for userinfo response failed") 0 0 0
164 or LOGDIE 'params_from_response for userinfo response failed';
165 0 return \%res_params;
166 }
167  
168 2 1;
169  
170 __END__
« RestApi/SheetsApi4/Range.pm ↑ Summary RestApi/SheetsApi4/Worksheet.pm »