blib/lib/SignalWire/Security/SessionManager.pm

filter: Statementstmt 98.0% 101 / 103 Branchbran 68.4% 26 / 38 Conditioncond 46.1% 12 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 10 totaltotal 75.9% 164 / 216 CC 30 SCAR 22.4
CC
30
Cov
83%
debug_token
20.7
validate_token
13.6
_random_hex
4.6
CRAP 9.4
« blib/lib/SignalWire/SWML/Service.pm ↑ Summary blib/lib/SignalWire/Prefabs/Concierge.pm »
1 package SignalWire::Security::SessionManager;
2 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has 'token_expiry_secs' => (
11 is => 'ro',
12 8 ▶ default => sub { 900 }, # 15 minutes
Subroutine __ANON__: called CC=1 SCAR=0.0
13 40 );
14  
15 has 'secret_key' => (
16 is => 'ro',
17 418 ▶ default => sub { _random_hex(32) },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 40 );
19  
20 has '_debug_mode' => (
21 is => 'rw',
22 424 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 40 );
24  
25 427 ▶sub _random_hex {
Subroutine _random_hex: called CC=4 SCAR=15.2
26 427 my ($len) = @_;
27 # Use /dev/urandom for cryptographically secure random bytes.
28 # Die on failure rather than falling back to weak randomness.
29 427 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<:raw", "/dev/urandom") 427 0 427
30 427 my $bytes;
31 427 my $read = read($fh, $bytes, $len);
32 427 close $fh;
33 427 ▶ if (defined $read && $read == $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read and $read == $len) 427 0 427
Condition33%1/3
defined $read and $read == $len
!ll&&!rl&&r
00427
MC/DC0%0/2
defined $read and $read == $len
defined $read $read == $len
Truth table33%1/3
A and B
A= defined $readB= $read == $len
ABresult
0X0
100
111
34 427 return unpack('H*', $bytes);
35 }
36 }
37 0 die "FATAL: Cannot generate secure random bytes - /dev/urandom unavailable. "
38 . "This is required for session security.\n";
39 }
40  
41 1 ▶sub _random_urlsafe {
Subroutine _random_urlsafe: called CC=2 SCAR=6.9
42 1 my ($len) = @_;
43 1 my $bytes = '';
44 1 for (1 .. $len) {
45 16 $bytes .= chr(int(rand(256)));
46 }
47 1 return MIME::Base64::encode_base64url($bytes, '');
48 }
49  
50 2 ▶sub create_session {
Subroutine create_session: called CC=2 SCAR=7.0
Pod undocumented
51 2 my ($self, $call_id) = @_;
52 2 ▶ $call_id //= _random_urlsafe(16);
Condition66%2/3
$call_id //= _random_urlsafe(16)
l!l&&r!l&&!r
110
MC/DC0%0/2
$call_id //= _random_urlsafe(16)
$call_id _random_urlsafe(16)
Truth table66%2/3
A //= B
A= $call_idB= _random_urlsafe(16)
ABresult
1X1
011
000
53 2 return $call_id;
54 }
55  
56 9 ▶sub generate_token {
Subroutine generate_token: called CC=1 SCAR=0.0
Pod undocumented
57 9 my ($self, $function_name, $call_id) = @_;
58 9 my $expiry = int(time()) + $self->token_expiry_secs;
59 9 my $nonce = _random_hex(8);
60  
61 9 my $message = "$call_id:$function_name:$expiry:$nonce";
62 9 my $signature = hmac_sha256_hex($message, $self->secret_key);
63  
64 9 my $token = "$call_id.$function_name.$expiry.$nonce.$signature";
65 9 return MIME::Base64::encode_base64url($token, '');
66 }
67  
68 # Alias
69 3 ▶sub create_tool_token {
Subroutine create_tool_token: called CC=1 SCAR=0.0
Pod undocumented
70 3 my ($self, $function_name, $call_id) = @_;
71 3 return $self->generate_token($function_name, $call_id);
72 }
73  
74 29 ▶sub _timing_safe_compare {
Subroutine _timing_safe_compare: called CC=1 SCAR=0.0
75 29 my ($a, $b) = @_;
76 # Compare HMAC of both values for constant-time comparison
77 29 my $key = 'timing-safe-token-comparison';
78 29 my $hmac_a = hmac_sha256_hex($a, $key);
79 29 my $hmac_b = hmac_sha256_hex($b, $key);
80 29 return $hmac_a eq $hmac_b;
81 }
82  
83 15 ▶sub validate_token {
Subroutine validate_token: called CC=13 SCAR=26.1
Pod undocumented
84 15 my ($self, $call_id, $function_name, $token) = @_;
85  
86 15 ▶ return 0 unless $call_id && $function_name && $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $call_id and $function_name and $token 3 12 15
Condition83%5/6
$call_id and $function_name and $token
!ll&&!rl&&r
1212
$call_id and $function_name
!ll&&!rl&&r
1014
MC/DC66%2/3
$call_id and $function_name and $token
$call_id $function_name $token
Truth table75%3/4
A and B and C
A= $call_idB= $function_nameC= $token
ABCresult
0XX0
10X0
1100
1111
87  
88 12 my $decoded;
89 12 eval {
90 12 $decoded = MIME::Base64::decode_base64url($token);
91 };
92 12 ▶ return 0 if $@ || !$decoded;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $decoded 0 12 12
Condition33%1/3
$@ or not $decoded
l!l&&r!l&&!r
0012
MC/DC0%0/2
$@ or not $decoded
$@ not $decoded
Truth table33%1/3
A or not(B)
A= $@B= not $decoded
ABresult
1X1
011
000
93  
94 12 my @parts = split(/\./, $decoded);
95 12 ▶ return 0 unless @parts == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts == 5 2 10 12
96  
97 10 my ($token_call_id, $token_function, $token_expiry, $token_nonce, $token_signature) = @parts;
98  
99 # Verify function matches
100 10 ▶ return 0 unless _timing_safe_compare($token_function, $function_name);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_function, $function_name) 1 9 10
101  
102 # Check expiry
103 9 my $expiry = eval { int($token_expiry) };
104 9 ▶ return 0 if $@ || !defined $expiry;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $expiry 0 9 9
Condition33%1/3
$@ or not defined $expiry
l!l&&r!l&&!r
009
MC/DC0%0/2
$@ or not defined $expiry
$@ not defined $expiry
Truth table33%1/3
A or not(B)
A= $@B= not defined $expiry
ABresult
1X1
011
000
105 9 ▶ return 0 if $expiry < time();
Branch: 1 branch
BranchTrueFalseTotal
if $expiry < time 1 8 9
106  
107 # Recreate and verify signature
108 8 my $message = "$token_call_id:$token_function:$token_expiry:$token_nonce";
109 8 my $expected_signature = hmac_sha256_hex($message, $self->secret_key);
110 8 ▶ return 0 unless _timing_safe_compare($token_signature, $expected_signature);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_signature, $expected_signature) 1 7 8
111  
112 # Verify call_id
113 7 ▶ return 0 unless _timing_safe_compare($token_call_id, $call_id);
Branch: 1 branch
BranchTrueFalseTotal
unless _timing_safe_compare($token_call_id, $call_id) 2 5 7
114  
115 5 return 1;
116 }
117  
118 # Alias with different parameter order for backward compat
119 1 ▶sub validate_tool_token {
Subroutine validate_tool_token: called CC=1 SCAR=0.0
Pod undocumented
120 1 my ($self, $function_name, $token, $call_id) = @_;
121 1 return $self->validate_token($call_id, $function_name, $token);
122 }
123  
124 # Legacy methods - no-ops for API compat (mirroring Python's
125 # stateless SessionManager.activate_session/end_session/etc. which
126 # accept the args, validate they're present, and return success).
127 4 ▶sub activate_session {
Subroutine activate_session: called CC=1 SCAR=0.0
Pod undocumented
128 4 my ($self, $call_id) = @_;
129 4 return 1;
130 }
131  
132 4 ▶sub end_session {
Subroutine end_session: called CC=1 SCAR=0.0
Pod undocumented
133 4 my ($self, $call_id) = @_;
134 4 return 1;
135 }
136  
137 4 ▶sub get_session_metadata {
Subroutine get_session_metadata: called CC=1 SCAR=0.0
Pod undocumented
138 4 my ($self, $call_id) = @_;
139 4 return {};
140 }
141  
142 4 ▶sub set_session_metadata {
Subroutine set_session_metadata: called CC=1 SCAR=0.0
Pod undocumented
143 4 my ($self, $call_id, $key, $value) = @_;
144 4 return 1;
145 }
146  
147 3 ▶sub debug_token {
Subroutine debug_token: called CC=13 SCAR=30.3
Pod undocumented
148 3 my ($self, $token) = @_;
149 3 ▶ return { error => 'debug mode not enabled' } unless $self->_debug_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_debug_mode 1 2 3
150  
151 2 my $decoded;
152 2 eval { $decoded = MIME::Base64::decode_base64url($token) };
153 2 ▶ if ($@ || !$decoded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $decoded) 0 2 2
Condition33%1/3
$@ or not $decoded
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or not $decoded
$@ not $decoded
Truth table33%1/3
A or not(B)
A= $@B= not $decoded
ABresult
1X1
011
000
154 return {
155 0 ▶ valid_format => JSON::false,
Branch: 1 branch
BranchTrueFalseTotal
defined $token ? : 0 0 0
Condition0%0/2
$@ // "decode failed"
l!l
00
MC/DC0%0/1
$@ // "decode failed"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
156 error => $@ // 'decode failed',
157 token_length => defined $token ? length($token) : 0,
158 };
159 }
160  
161 2 my @parts = split(/\./, $decoded);
162 2 ▶ if (@parts != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts != 5) 1 1 2
163 return {
164 1 valid_format => JSON::false,
165 parts_count => scalar @parts,
166 token_length => length($token),
167 };
168 }
169  
170 1 my ($tc, $tf, $te, $tn, $ts) = @parts;
171 1 my $current = int(time());
172 1 my $expiry = eval { int($te) };
173 1 ▶ my $expired = defined $expiry ? ($expiry < $current ? 1 : 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $expiry ? : 1 0 1
$expiry < $current ? : 0 1 1
174  
175 return {
176 1 ▶ valid_format => JSON::true,
Branch: 3 branches
BranchTrueFalseTotal
length $tc > 8 ? : 0 1 1
length $ts > 8 ? : 1 0 1
defined $expiry && !$expired ? : 1 0 1
Condition33%1/3
defined $expiry and not $expired
!ll&&!rl&&r
001
MC/DC0%0/2
defined $expiry and not $expired
defined $expiry not $expired
Truth table33%1/3
A and not(B)
A= defined $expiryB= not $expired
ABresult
0X0
100
111
177 components => {
178 call_id => (length($tc) > 8 ? substr($tc, 0, 8) . '...' : $tc),
179 function => $tf,
180 expiry => $te,
181 nonce => $tn,
182 signature => (length($ts) > 8 ? substr($ts, 0, 8) . '...' : $ts),
183 },
184 status => {
185 current_time => $current,
186 is_expired => $expired,
187 expires_in_seconds => (defined $expiry && !$expired ? $expiry - $current : 0),
188 },
189 };
190 }
191  
192 40 1;
« blib/lib/SignalWire/SWML/Service.pm ↑ Summary blib/lib/SignalWire/Prefabs/Concierge.pm »