OpenIDConnect/Utils/Store.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 66.6% 20 / 30 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 89.5% 120 / 134 CC 18 SCAR 13.2
CC
18
Cov
88%
consume_authorization_code
7.8
get_authorization_code
6.1
create_authorization_code
4.1
CRAP 3.7
« OpenIDConnect/Utils/JWT.pm ↑ Summary OpenIDConnect/Role/Store.pm »
1 package Catalyst::Plugin::OpenIDConnect::Utils::Store;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Bytes::Random::Secure qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MIME::Base64 qw(encode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 with 'Catalyst::Plugin::OpenIDConnect::Role::Store';
13  
14 =head1 NAME
15  
16 Catalyst::Plugin::OpenIDConnect::Utils::Store - In-process memory store for OIDC state
17  
18 =head1 DESCRIPTION
19  
20 Provides in-process memory storage for authorization codes and OIDC session
21 state. Suitable for development and single-process deployments.
22  
23 B<Not suitable for multi-process servers such as FastCGI or pre-forking>
24 because each worker process has its own independent copy of the data.
25 For those deployments, use L<Catalyst::Plugin::OpenIDConnect::Utils::Store::Redis>
26 or another shared-backend store that consumes
27 L<Catalyst::Plugin::OpenIDConnect::Role::Store>.
28  
29 =head1 ATTRIBUTES
30  
31 =head2 codes
32  
33 Storage for authorization codes (code => {client_id, user, scope, ...})
34  
35 =cut
36  
37 has codes => (
38 is => 'ro',
39 isa => 'HashRef',
40 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 2 );
42  
43 =head2 sessions
44  
45 Storage for user sessions (session_id => {user, tokens, ...})
46  
47 =cut
48  
49 has sessions => (
50 is => 'ro',
51 isa => 'HashRef',
52 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 2 );
54  
55 =head2 logger
56  
57 Optional logger instance for debug/info logging.
58  
59 =cut
60  
61 2 has logger => (
62 is => 'ro',
63 isa => 'Maybe[Object]',
64 required => 0,
65 );
66  
67 # Private storage for refresh token JTIs (MED-1).
68  
69 has _refresh_tokens => (
70 is => 'ro',
71 isa => 'HashRef',
72 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 2 );
74  
75 # Secondary index: sub => { jti => 1 } Used for bulk revocation at logout.
76 has _rt_by_sub => (
77 is => 'ro',
78 isa => 'HashRef',
79 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 2 );
81  
82 =head1 METHODS
83  
84 =head2 create_authorization_code($client_id, $user, $scope, $redirect_uri, $nonce, $pkce)
85  
86 Creates an authorization code for the given parameters. C<$pkce> is an
87 optional hashref with keys C<code_challenge> and C<code_challenge_method>;
88 omit or pass C<undef> for non-PKCE flows.
89  
90 Returns the authorization code string.
91  
92 =cut
93  
94 24 ▶sub create_authorization_code {
Subroutine create_authorization_code: called CC=4 SCAR=14.0
Pod documented
95 24 my ( $self, $client_id, $user, $scope, $redirect_uri, $nonce, $pkce ) = @_;
96  
97 24 ▶ $self->logger->debug("Creating authorization code for client: $client_id") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 24 24
98  
99 24 my $code = _generate_secure_random();
100  
101 $self->codes->{$code} = {
102 client_id => $client_id,
103 user => $user,
104 scope => $scope,
105 redirect_uri => $redirect_uri,
106 nonce => $nonce,
107 created_at => time(),
108 expires_at => time() + 600, # 10 minutes
109 ( $pkce ? (
110 code_challenge => $pkce->{code_challenge},
111 code_challenge_method => $pkce->{code_challenge_method},
112 24 ▶ ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$pkce ? : 1 23 24
113 };
114  
115 24 ▶ $self->logger->debug("Authorization code created: $code (expires in 600 seconds)") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 24 24
116  
117 24 return $code;
118 }
119  
120 =head2 get_authorization_code($code)
121  
122 Retrieves an authorization code by value.
123  
124 Returns the code data hashref or undef if not found.
125  
126 =cut
127  
128 4 ▶sub get_authorization_code {
Subroutine get_authorization_code: called CC=6 SCAR=18.1
Pod documented
129 4 my ( $self, $code ) = @_;
130  
131 4 ▶ $self->logger->debug("Retrieving authorization code: $code") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 4 4
132  
133 4 my $code_data = $self->codes->{$code};
134  
135 4 ▶ return unless $code_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $code_data 2 2 4
136  
137 # Check if code is expired
138 2 ▶ if ( $code_data->{expires_at} < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_data->{"expires_at"} < time) 1 1 2
139 1 ▶ $self->logger->warn("Authorization code expired: $code") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 1 1
140 1 delete $self->codes->{$code};
141 1 return;
142 }
143  
144 1 ▶ $self->logger->debug("Authorization code found: $code") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 1 1
145 1 return $code_data;
146 }
147  
148 =head2 consume_authorization_code($code)
149  
150 Atomically deletes the authorization code and returns its data. Uses Perl's
151 C<delete> which fetches and removes the hash entry in a single operation,
152 making it race-free within a single process.
153  
154 Returns the code data hashref on success, or C<undef> if the code does not
155 exist or has expired.
156  
157 =cut
158  
159 5 ▶sub consume_authorization_code {
Subroutine consume_authorization_code: called CC=6 SCAR=20.5
Pod documented
160 5 my ( $self, $code ) = @_;
161  
162 5 ▶ $self->logger->debug("Consuming authorization code: $code") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 5 5
163  
164 # delete() is atomic within a single process: it removes and returns the
165 # value in one step, preventing two concurrent requests from both
166 # succeeding a check-then-delete sequence.
167 5 my $code_data = delete $self->codes->{$code};
168 5 ▶ return unless $code_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $code_data 2 3 5
169  
170 3 ▶ if ( $code_data->{expires_at} < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_data->{"expires_at"} < time) 0 3 3
171 0 ▶ $self->logger->warn("Authorization code expired at consume time: $code")
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 0 0
172 if $self->logger;
173 0 return;
174 }
175  
176 3 ▶ $self->logger->debug("Authorization code consumed: $code") if $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->logger 0 3 3
177 3 return $code_data;
178 }
179  
180 =head2 store_refresh_token($jti, $sub, $client_id, $ttl)
181  
182 Stores a refresh token JTI with the associated subject, client, and a TTL in
183 seconds. Called at token-issuance time so that the token endpoint can later
184 enforce single-use semantics via L</consume_refresh_token>.
185  
186 =cut
187  
188 5 ▶sub store_refresh_token {
Subroutine store_refresh_token: called CC=1 SCAR=0.0
Pod documented
189 5 my ( $self, $jti, $sub, $client_id, $ttl ) = @_;
190 5 $self->_refresh_tokens->{$jti} = {
191 sub => $sub,
192 client_id => $client_id,
193 exp => time() + $ttl,
194 };
195 # Secondary index by subject so all tokens can be revoked at logout.
196 5 $self->_rt_by_sub->{$sub}{$jti} = 1;
197 }
198  
199 =head2 consume_refresh_token($jti)
200  
201 Atomically removes the JTI from the store and returns the associated data
202 hashref, or C<undef> if absent or expired (already used / revoked / TTL
203 elapsed).
204  
205 =cut
206  
207 7 ▶sub consume_refresh_token {
Subroutine consume_refresh_token: called CC=3 SCAR=11.0
Pod documented
208 7 my ( $self, $jti ) = @_;
209 7 my $data = delete $self->_refresh_tokens->{$jti};
210 7 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 4 3 7
211 3 ▶ if ( $data->{exp} < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"exp"} < time) 1 2 3
212 1 delete $self->_rt_by_sub->{ $data->{sub} }{$jti};
213 1 return;
214 }
215 2 delete $self->_rt_by_sub->{ $data->{sub} }{$jti};
216 2 return $data;
217 }
218  
219 =head2 revoke_refresh_tokens_for_user($sub)
220  
221 Removes all refresh token JTIs for the given subject identifier from the store.
222 Called at logout time to prevent re-use of stolen tokens.
223  
224 =cut
225  
226 1 ▶sub revoke_refresh_tokens_for_user {
Subroutine revoke_refresh_tokens_for_user: called CC=3 SCAR=11.1
Pod documented
227 1 my ( $self, $sub ) = @_;
228 1 ▶ my $jtis = delete $self->_rt_by_sub->{$sub} // {};
Condition50%1/2
delete $self->_rt_by_sub->{$sub} // {}
l!l
10
MC/DC0%0/1
delete $self->_rt_by_sub->{$sub} // {}
delete $self->_rt_by_sub->{$sub}
Truth table50%1/2
A
A= delete $self->_rt_by_sub->{$sub}
Aresult
11
00
229 1 delete $self->_refresh_tokens->{$_} for keys %{$jtis};
230 }
231  
232 # Generate a cryptographically secure random string for codes and tokens.
233 # Uses Bytes::Random::Secure to draw from the OS CSPRNG (e.g. /dev/urandom),
234 # which is safe even after fork() — important for pre-forking servers.
235 24 ▶sub _generate_secure_random {
Subroutine _generate_secure_random: called CC=1 SCAR=0.0
236 # 120 random bytes -> 160 base64url characters; after stripping the
237 # non-alphanumeric "-" and "_" characters (roughly 3% of chars) we have
238 # well over 128 alphanumeric characters to work with.
239 24 my $bytes = random_bytes(120);
240 24 my $encoded = encode_base64url($bytes);
241 24 $encoded =~ s/[^a-zA-Z0-9]//g;
242 24 return substr( $encoded, 0, 128 );
243 }
244  
245 2 __PACKAGE__->meta->make_immutable;
246 2 1;
247  
248 =head1 AUTHOR
249  
250 Tim F. Rayner
251  
252 =head1 LICENSE
253  
254 This library is free software; you can redistribute it and/or modify
255 it under the terms of The Artistic License 2.0.
256  
257 =cut
« OpenIDConnect/Utils/JWT.pm ↑ Summary OpenIDConnect/Role/Store.pm »