blib/lib/Google/Auth/Stores/FileTokenStore.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 55.0% 11 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 82.9% 73 / 88 CC 11 SCAR 9.5
CC
11
Cov
85%
load
4.2
delete
3.1
BUILD
3.1
CRAP 2.6
« blib/lib/Google/Auth.pm ↑ Summary blib/lib/Google/Auth/CloudSDK.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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 extends 'Google::Auth::Stores::TokenStore';
22  
23 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Google::Auth::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 has store_dir => (
28 is => 'ro',
29 required => 1,
30 );
31  
32 20 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.2
Pod undocumented
33 20 my ($self) = @_;
34 20 my $dir = $self->store_dir;
35 20 ▶ if (!-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $dir) 4 16 20
36 4 eval { make_path($dir) };
37 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
38 0 Google::Auth::Error->throw(
39 'Failed to create token store directory ' . $dir . ': ' . $@);
40 }
41 }
42 }
43  
44 10 ▶sub _file_path {
Subroutine _file_path: called CC=2 SCAR=7.6
45 10 my ($self, $id) = @_;
46  
47 # Sanitize ID to prevent path traversal?
48 # Assuming ID is a simple string like email or user_name
49 10 ▶ if ($id =~ /[\/\\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ m([/\\])) 0 10 10
50 0 Google::Auth::Error->throw(
51 'Invalid ID \'' . $id . '\': cannot contain path separators');
52 }
53 10 return File::Spec->catfile($self->store_dir, $id . '.json');
54 }
55  
56 4 ▶sub load {
Subroutine load: called CC=4 SCAR=14.2
Pod undocumented
57 4 my ($self, $id) = @_;
58 4 my $path = $self->_file_path($id);
59  
60 4 ▶ return unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 4 4
61  
62 4 ▶ open(my $fh, '<:encoding(UTF-8)', $path) or return; # Or throw?
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or return 4 0 4
63 4 local $/;
64 4 my $value = <$fh>;
65 4 ▶ close($fh) or Google::Auth::Error->throw("Failed to close $path: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or "Google::Auth::Error"->throw("Failed to close ${path}: $!") 4 0 4
66  
67 4 return $value;
68 }
69  
70 4 ▶sub store {
Subroutine store: called CC=3 SCAR=11.1
Pod undocumented
71 4 my ($self, $id, $value) = @_;
72 4 my $path = $self->_file_path($id);
73  
74 4 require Fcntl;
75 4 ▶ sysopen(my $fh, $path,
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $path, 577, 384 or "Google::Auth::Error"->throw("Failed to write to ${path}: $!") 4 0 4
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 4 binmode($fh, ':encoding(UTF-8)');
81  
82 4 print $fh $value;
83 4 ▶ close($fh) or Google::Auth::Error->throw("Failed to close $path: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or "Google::Auth::Error"->throw("Failed to close ${path}: $!") 4 0 4
84 4 return;
85 }
86  
87 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.3
Pod undocumented
88 2 my ($self, $id) = @_;
89 2 my $path = $self->_file_path($id);
90  
91 2 ▶ if (-f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 2 0 2
92 2 ▶ unlink($path)
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or "Google::Auth::Error"->throw('Failed to delete ' . $path . ': ' . $!) 2 0 2
93 or Google::Auth::Error->throw('Failed to delete ' . $path . ': ' . $!);
94 }
95 2 return;
96 }
97  
98 4 1;
« blib/lib/Google/Auth.pm ↑ Summary blib/lib/Google/Auth/CloudSDK.pm »