blib/lib/Google/Auth.pm

filter: Statementstmt 91.1% 31 / 34 Branchbran 25.0% 1 / 4 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 75.0% 45 / 60 CC 6 SCAR 17.4
CC
6
Cov
76%
default
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.7
« blib/lib/Google/Auth/IDTokens/KeySources.pm ↑ Summary blib/lib/Google/Auth/Stores/FileTokenStore.pm »
1 # Copyright 2022 Google LLC and contributors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Auth;
16  
17 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 13 ▶use Google::Auth::EnvironmentVars;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Google::Auth::DefaultCredentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use Google::Auth::ComputeEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 13 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 13 our $VERSION = '0.15';
28 13 XSLoader::load('Google::Auth', $VERSION);
29  
30 =head1 NAME
31  
32 Google::Auth - Google Cloud ADC and OAuth 2.0 authentication for Perl
33  
34 =head1 VERSION
35  
36 Version 0.15
37  
38 =head1 SYNOPSIS
39  
40 use Google::Auth;
41  
42 # Load Application Default Credentials (ADC)
43 my $creds = Google::Auth->default(
44 ['https://www.googleapis.com/auth/cloud-platform']
45 );
46  
47 # Fetch a Bearer access token (cached and refreshed automatically)
48 my $access_token = $creds->get_token();
49  
50 # Use with a Google Cloud client library
51 use Google::Cloud::Spanner::V1::SpannerClient;
52 my $client = Google::Cloud::Spanner::V1::SpannerClient->new(
53 credentials => $creds,
54 );
55  
56 =head1 DESCRIPTION
57  
58 C<Google::Auth> implements Google Cloud Application Default Credentials (ADC)
59 and OAuth 2.0 authentication for Perl. It automatically discovers credentials
60 from the environment in the standard Google Cloud order:
61  
62 =over 4
63  
64 =item 1. The C<GOOGLE_APPLICATION_CREDENTIALS> environment variable (service account JSON, authorized user JSON, impersonated service account, or Workload Identity Federation external account config).
65  
66 =item 2. The well-known Google Cloud CLI ADC path (C<~/.config/gcloud/application_default_credentials.json>).
67  
68 =item 3. The system default path.
69  
70 =item 4. The Compute Engine / GKE / Cloud Run metadata server when running on GCP.
71  
72 =back
73  
74 It also provides OpenSSL-backed cryptographic helpers (JWT signing and ID token
75 verification), Workload Identity Federation (AWS EC2 IMDSv2 / ECS Task Roles,
76 Pluggable/URL/File-sourced credentials), service account impersonation, OAuth
77 2.0 web/installed-app authorizers, and the C<gcloud-auth> CLI utility.
78  
79 =head1 METHODS
80  
81 =head2 default( $scopes, $options )
82  
83 my $creds = Google::Auth->default();
84 my $creds = Google::Auth->default(
85 ['https://www.googleapis.com/auth/cloud-platform'],
86 { universe_domain => 'googleapis.com' },
87 );
88  
89 Resolves and returns a credentials object (subclass of
90 L<Google::Auth::Credentials>) for the current environment. C<$scopes> may be a
91 single scope string or an ArrayRef of OAuth 2.0 scope URLs. C<$options> is an
92 optional HashRef of additional parameters forwarded to the credential loader.
93  
94 Throws C<Google::Auth::DefaultCredentialsError> (defined in
95 C<Google::Auth::Exceptions>) if no credentials can be found.
96  
97 =cut
98  
99 #[%- Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms %]
100 4 ▶sub default {
Subroutine default: called CC=6 SCAR=24.8
Pod documented
101 4 my ($self, $scopes, $options) = @_;
102 4 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
04
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
103  
104 4 my $dc = Google::Auth::DefaultCredentials->new();
105  
106 4 ▶ my $creds =
Condition33%2/6
$dc->from_env($scopes, %$options) || $dc->from_well_known_path($scopes, %$options) || $dc->from_system_default_path($scopes, %$options)
l!l&&r!l&&!r
100
$dc->from_env($scopes, %$options) or $dc->from_well_known_path($scopes, %$options)
l!l&&r!l&&!r
100
MC/DC0%0/3
$dc->from_env($scopes, %$options) || $dc->from_well_known_path($scopes, %$options) || $dc->from_system_default_path($scopes, %$options)
$dc->from_env($scopes, %$options) $dc->from_well_known_path($scopes, %$options) $dc->from_system_default_path($scopes, %$options)
Truth table25%1/4
A or B || C
A= $dc->from_env($scopes, %$options)B= $dc->from_well_known_path($scopes, %$options)C= $dc->from_system_default_path($scopes, %$options)
ABCresult
1XX1
01X1
0011
0000
107 $dc->from_env($scopes, %$options)
108 || $dc->from_well_known_path($scopes, %$options)
109 || $dc->from_system_default_path($scopes, %$options);
110  
111 1 ▶ return $creds if $creds;
Branch: 1 branch
BranchTrueFalseTotal
if $creds 1 0 1
112  
113 0 ▶ if (Google::Auth::ComputeEngine->on_gce(%$options)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Google::Auth::ComputeEngine"->on_gce(%$options)) 0 0 0
114 0 return Google::Auth::ComputeEngine->new(scope => $scopes, %$options);
115 }
116  
117 Google::Auth::DefaultCredentialsError->throw(
118 0 'Your credentials were not found. To set up Application Default ' .
119 'Credentials for your environment, see ' .
120 'https://cloud.google.com/docs/authentication/external/set-up-adc');
121 }
122  
123 # I have no idea why my perlcritic throws this
124 #[%- Perl::Critic::Policy::Modules::RequireEndWithOne %]
125 # End of Google::Auth
126 13 1;
127  
128 =head1 CONFIGURATION AND ENVIRONMENT
129  
130 =over 4
131  
132 =item C<GOOGLE_APPLICATION_CREDENTIALS>
133  
134 Path to a JSON credentials file (service account, authorized user, impersonated
135 service account, or external account / WIF configuration).
136  
137 =item C<GOOGLE_CLOUD_QUOTA_PROJECT>
138  
139 Optional Google Cloud project ID used for quota and billing attribution.
140  
141 =item C<GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES>
142  
143 Set to C<1> to allow Pluggable Credentials to execute external commands. Default is C<0> (disabled).
144  
145 =item C<GOOGLE_EXTERNAL_ACCOUNT_ALLOW_CUSTOM_UNIVERSES>
146  
147 Set to C<1> to allow custom universes in credentials files loaded from JSON. Default is C<0> (disabled).
148  
149 =back
150  
151 =head1 SEE ALSO
152  
153 L<Google::Auth::DefaultCredentials>, L<Google::Auth::ServiceAccountCredentials>,
154 L<Google::Auth::ComputeEngine>, L<Google::Auth::UserRefreshCredentials>,
155 L<Google::Auth::ImpersonatedServiceAccountCredentials>,
156 L<Google::Auth::ExternalAccountCredentials>,
157 L<Google::Auth::IDTokens::Verifier>, L<Google::Auth::WebUserAuthorizer>
158  
159 =head1 AUTHOR
160  
161 C.J. Collier, C<< <cjac at google.com> >>
162  
163 =head1 BUGS AND SUPPORT
164  
165 Please report any bugs or feature requests to the GitHub issue tracker at
166 L<https://github.com/GoogleCloudPlatform/google-auth-library-perl/issues> or via RT at
167 L<https://rt.cpan.org/Dist/Display.html?Name=Google-Auth>.
168  
169 You can also look for information at:
170  
171 =over 4
172  
173 =item * GitHub Issue Tracker
174  
175 L<https://github.com/GoogleCloudPlatform/google-auth-library-perl/issues>
176  
177 =item * RT: CPAN's Request Tracker
178  
179 L<https://rt.cpan.org/Dist/Display.html?Name=Google-Auth>
180  
181 =item * MetaCPAN
182  
183 L<https://metacpan.org/release/Google-Auth>
184  
185 =back
186  
187 =head1 LICENSE AND COPYRIGHT
188  
189 Copyright 2020 Google LLC and contributors
190  
191 This program is released under the following license: Apache 2.0
192  
193 =cut
194  
« blib/lib/Google/Auth/IDTokens/KeySources.pm ↑ Summary blib/lib/Google/Auth/Stores/FileTokenStore.pm »