« blib/lib/Google/Auth/ExternalAccountCredentials.pm
↑ Summary
blib/lib/Google/Auth/RetryHelper.pm »
| 1 | # Copyright 2026 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::Stores::FileTokenStore; | ||||||||||
| 16 | |||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | extends 'Google::Auth::Stores::TokenStore'; | |||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | ▶ | use File::Path qw(make_path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use Google::Auth::Exceptions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | has store_dir => ( | |||||||||
| 28 | is => 'ro', | ||||||||||
| 29 | required => 1, | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 2 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||
| 33 | 2 | my ($self) = @_; | |||||||||
| 34 | 2 | my $dir = $self->store_dir; | |||||||||
| 35 | 2 | ▶ | if (!-d $dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | eval { make_path($dir) }; | |||||||||
| 37 | 2 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | Google::Auth::Error->throw( | |||||||||
| 39 | 'Failed to create token store directory ' . $dir . ': ' . $@); | ||||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub _file_path { | ||||||||
|
Subroutine
_file_path: not called CC=2 SCAR=17.9
| |||||||||||
| 45 | 0 | my ($self, $id) = @_; | |||||||||
| 46 | |||||||||||
| 47 | # Sanitize ID to prevent path traversal? | ||||||||||
| 48 | # Assuming ID is a simple string like email or user_name | ||||||||||
| 49 | 0 | ▶ | if ($id =~ /[\/\\]/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | Google::Auth::Error->throw( | |||||||||
| 51 | 'Invalid ID \'' . $id . '\': cannot contain path separators'); | ||||||||||
| 52 | } | ||||||||||
| 53 | 0 | return File::Spec->catfile($self->store_dir, $id . '.json'); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 57 | 0 | my ($self, $id) = @_; | |||||||||
| 58 | 0 | my $path = $self->_file_path($id); | |||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | return unless -f $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | open(my $fh, '<:encoding(UTF-8)', $path) or return; # Or throw? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | local $/; | |||||||||
| 64 | 0 | my $value = <$fh>; | |||||||||
| 65 | 0 | ▶ | close($fh) or Google::Auth::Error->throw("Failed to close $path: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 0 | return $value; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub store { | ||||||||
|
Subroutine
store: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 71 | 0 | my ($self, $id, $value) = @_; | |||||||||
| 72 | 0 | my $path = $self->_file_path($id); | |||||||||
| 73 | |||||||||||
| 74 | 0 | require Fcntl; | |||||||||
| 75 | 0 | ▶ | sysopen(my $fh, $path, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | Fcntl::O_CREAT() | Fcntl::O_WRONLY() | Fcntl::O_TRUNC(), 0600) | ||||||||||
| 77 | or Google::Auth::Error->throw("Failed to write to $path: $!"); | ||||||||||
| 78 | |||||||||||
| 79 | # Set binmode for UTF-8 encoding | ||||||||||
| 80 | 0 | binmode($fh, ':encoding(UTF-8)'); | |||||||||
| 81 | |||||||||||
| 82 | 0 | print $fh $value; | |||||||||
| 83 | 0 | ▶ | close($fh) or Google::Auth::Error->throw("Failed to close $path: $!"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | return; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 88 | 0 | my ($self, $id) = @_; | |||||||||
| 89 | 0 | my $path = $self->_file_path($id); | |||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | if (-f $path) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | ▶ | unlink($path) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | or Google::Auth::Error->throw('Failed to delete ' . $path . ': ' . $!); | ||||||||||
| 94 | } | ||||||||||
| 95 | 0 | return; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 2 | 1; | |||||||||