blib/lib/PAGI/Middleware/Session/Store/Cookie.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 8 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 96.0% 96 / 100 CC 7 SCAR 6.7
CC
7
Cov
97%
_decrypt
4.0
get
3.0
new
2.0
CRAP 2.0
↑ Summary
1 package PAGI::Middleware::Session::Store::Cookie;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.001005';
7  
8 1 ▶use parent 'PAGI::Middleware::Session::Store';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::SHA qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Crypt::AuthEnc::GCM;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 PAGI::Middleware::Session::Store::Cookie - Encrypted client-side session store
19  
20 =head1 SYNOPSIS
21  
22 use PAGI::Middleware::Session::Store::Cookie;
23  
24 my $store = PAGI::Middleware::Session::Store::Cookie->new(
25 secret => 'at-least-32-bytes-of-secret-key!',
26 );
27  
28 # All methods return Futures
29 my $blob = await $store->set('session_id', { user_id => 123 });
30 my $data = await $store->get($blob);
31 await $store->delete('session_id');
32  
33 =head1 DESCRIPTION
34  
35 Stores session data encrypted in the client cookie itself using AES-256-GCM
36 (authenticated encryption). No server-side storage is needed.
37  
38 The C<set> method returns the encrypted blob (not the session ID).
39 The C<get> method accepts the encrypted blob and returns the decoded
40 session data, or undef if decryption/verification fails.
41  
42 B<Limitations:> Cookie size is limited to ~4KB. Large sessions will fail.
43 Session revocation requires server-side state (e.g., a blocklist).
44  
45 =cut
46  
47 13 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
48 13 my ($class, %args) = @_;
49 13 ▶ die "Store::Cookie requires 'secret'" unless $args{secret};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"secret"} 1 12 13
50  
51 12 my $self = $class->SUPER::new(%args);
52  
53 # Derive a 32-byte key from the secret via SHA-256
54 12 $self->{_key} = sha256($self->{secret});
55  
56 12 return $self;
57 }
58  
59 =head1 METHODS
60  
61 =head2 new
62  
63 my $store = PAGI::Middleware::Session::Store::Cookie->new(
64 secret => 'at-least-32-bytes-of-secret-key!',
65 );
66  
67 Creates a new cookie session store. The C<secret> parameter is required
68 and is used to derive the AES-256 encryption key via SHA-256.
69  
70 =head2 get
71  
72 my $future = $store->get($encrypted_blob);
73  
74 Decrypts and decodes the blob. Returns a Future resolving to the session
75 hashref, or undef if the blob is invalid, tampered, or cannot be decoded.
76  
77 =cut
78  
79 10 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
80 10 my ($self, $blob) = @_;
81  
82 10 ▶ return Future->done(undef) unless defined $blob && length $blob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $blob and length $blob 1 9 10
Condition66%2/3
defined $blob and length $blob
!ll&&!rl&&r
109
MC/DC50%1/2
defined $blob and length $blob
defined $blob length $blob
Truth table66%2/3
A and B
A= defined $blobB= length $blob
ABresult
0X0
100
111
83  
84 9 my $data = eval { $self->_decrypt($blob) };
85 9 return Future->done($data);
86 }
87  
88 =head2 set
89  
90 my $future = $store->set($id, $data);
91  
92 Encrypts the session data and returns a Future resolving to the encrypted
93 blob. This blob is what gets passed to C<State::inject> for storage
94 in the response cookie. Nothing is stored server-side.
95  
96 =cut
97  
98 11 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
99 11 my ($self, $id, $data) = @_;
100  
101 11 my $blob = $self->_encrypt($data);
102 11 return Future->done($blob);
103 }
104  
105 =head2 delete
106  
107 my $future = $store->delete($id);
108  
109 No-op for cookie stores (client manages cookie lifetime).
110 Returns a Future resolving to 1.
111  
112 =cut
113  
114 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
115 1 my ($self, $id) = @_;
116 1 return Future->done(1);
117 }
118  
119 11 ▶sub _encrypt {
Subroutine _encrypt: called CC=1 SCAR=0.0
120 11 my ($self, $data) = @_;
121  
122 11 my $json = encode_json($data);
123  
124 # Generate random 12-byte IV (standard for AES-GCM)
125 11 my $iv = random_bytes(12);
126  
127 11 my $gcm = Crypt::AuthEnc::GCM->new('AES', $self->{_key}, $iv);
128 11 my $ciphertext = $gcm->encrypt_add($json);
129 11 my $tag = $gcm->encrypt_done;
130  
131 # Pack: iv (12) + tag (16) + ciphertext
132 11 my $packed = $iv . $tag . $ciphertext;
133 11 return encode_base64($packed, '');
134 }
135  
136 9 ▶sub _decrypt {
Subroutine _decrypt: called CC=4 SCAR=13.9
137 9 my ($self, $blob) = @_;
138  
139 9 my $packed = decode_base64($blob);
140 9 ▶ return undef unless defined $packed && length($packed) > 28;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed and length $packed > 28 1 8 9
Condition66%2/3
defined $packed and length $packed > 28
!ll&&!rl&&r
018
MC/DC50%1/2
defined $packed and length $packed > 28
defined $packed length $packed > 28
Truth table66%2/3
A and B
A= defined $packedB= length $packed > 28
ABresult
0X0
100
111
141  
142 # Unpack: iv (12) + tag (16) + ciphertext
143 8 my $iv = substr($packed, 0, 12);
144 8 my $tag = substr($packed, 12, 16);
145 8 my $ciphertext = substr($packed, 28);
146  
147 8 my $gcm = Crypt::AuthEnc::GCM->new('AES', $self->{_key}, $iv);
148 8 my $json = $gcm->decrypt_add($ciphertext);
149 8 my $valid = $gcm->decrypt_done($tag);
150  
151 8 ▶ return undef unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 2 6 8
152  
153 6 return decode_json($json);
154 }
155  
156 1 1;
157  
158 __END__
↑ Summary