OpenIDConnect.pm

filter: Statementstmt 48.3% 60 / 124 Branchbran 6.2% 3 / 48 Conditioncond 5.1% 2 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 70.8% 17 / 24 Podpod 100% 1 / 1 totaltotal 32.0% 83 / 259 CC 40 SCAR 46.6
CC
40
Cov
31%
_oidc_build_jwt_handler
306.0
__ANON__
110.0
_oidc_store
30.0
CRAP 106.1
« OpenIDConnect/Controller/Root.pm ↑ Summary OpenIDConnect/Utils/Store/Redis.pm »
1 package Catalyst::Plugin::OpenIDConnect;
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 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Catalyst::Plugin::OpenIDConnect::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catalyst::Plugin::OpenIDConnect::Utils::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Catalyst::Plugin::OpenIDConnect::Utils::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Catalyst::Plugin::OpenIDConnect::Role::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = '0.16';
22  
23 =head1 NAME
24  
25 Catalyst::Plugin::OpenIDConnect - OpenID Connect provider plugin for Catalyst
26  
27 =head1 DESCRIPTION
28  
29 A Catalyst plugin implementing the OpenID Connect specification,
30 providing OAuth 2.0 authentication and authorization. Note that this plugin
31 does not implement the OIDC Client role; it is intended for applications
32 acting as OIDC providers (authorization servers).
33  
34 This plugin provides the core OpenIDConnect functionality (JWT handling,
35 state management, and a reusable controller). To use it in your application,
36 you must create a controller in your app's namespace that extends the plugin's
37 controller (see below). This allows you to keep full control over your routing
38 while cooperating with ACL and other route-processing plugins.
39  
40 =head1 CONFIGURATION
41  
42 package MyApp;
43 use Catalyst qw/
44 OpenIDConnect
45 Session
46 Session::Store::File
47 Session::State::Cookie
48 /;
49  
50 MyApp->config(
51 'Plugin::OpenIDConnect' => {
52 issuer => {
53 url => 'http://localhost:5000',
54 private_key_file => '/path/to/private.pem',
55 public_key_file => '/path/to/public.pem',
56 key_id => 'key-123',
57 },
58 clients => {
59 'my-client' => {
60 client_secret => 'secret123',
61 redirect_uris => ['http://localhost:3000/callback'],
62 response_types => ['code'],
63 grant_types => ['authorization_code'],
64 scope => 'openid profile email',
65 },
66 },
67 },
68 );
69  
70 =head1 CREATING THE OPENIDCONNECT CONTROLLER
71  
72 To enable the OpenIDConnect endpoints, create a controller in your app that extends
73 the plugin's controller. Create the file C<lib/MyApp/Controller/OpenIDConnect.pm>
74 (where MyApp is your app's namespace) with the following content:
75  
76 package MyApp::Controller::OpenIDConnect;
77  
78 use Moose;
79 use namespace::autoclean;
80  
81 BEGIN { extends 'Catalyst::Plugin::OpenIDConnect::Controller::Root' }
82  
83 __PACKAGE__->meta->make_immutable;
84 1;
85  
86 Then, in your main app module, explicitly load this controller before setup:
87  
88 package MyApp;
89 use Catalyst qw/
90 OpenIDConnect
91 Session
92 Session::Store::File
93 Session::State::Cookie
94 /;
95
96 # Load the controller before setup so Catalyst discovers it
97 use MyApp::Controller::OpenIDConnect;
98
99 MyApp->config(...);
100 MyApp->setup(...);
101  
102 Setting up the controller in this way allows you to keep full control over your
103 routing, and avoid namespace conflicts with ACL and other route-processing plugins.
104 The plugin's controller will automatically mount the standard OpenID Connect
105 endpoints (e.g. C</authorize>, C</token>, C</userinfo>) under the C</openidconnect>
106 path, so you can access them at C</openidconnect/authorize>, etc.
107  
108 =head1 ROUTES ADDED TO THE APPLICATION
109  
110 The plugin's controller adds the following routes to the application:
111  
112 GET /.well-known/openid-configuration
113 GET /openidconnect/authorize
114 POST /openidconnect/token
115 GET /openidconnect/userinfo
116 GET /openidconnect/jwks
117 POST /openidconnect/logout
118  
119 =cut
120  
121 1 requires 'config', 'log', 'uri_for', 'user', 'request', 'response';
122  
123 # Per-application-class storage for JWT and Store instances.
124 # Keyed by consuming application class name so that multiple Catalyst apps
125 # loaded in the same Perl interpreter each hold their own instances and cannot
126 # accidentally overwrite each other's JWT keys or stores (MED-3).
127 1 my %_oidc_jwt_by_class;
128 my %_oidc_store_by_class;
129  
130 =head1 ATTRIBUTES
131  
132 =head2 _oidc_jwt
133  
134 JWT handler instance.
135  
136 =cut
137  
138 # Accessor method for JWT handler
139 6 ▶sub _oidc_jwt {
Subroutine _oidc_jwt: called CC=5 SCAR=17.5
140 6 my ($self, $value) = @_;
141 6 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
142 6 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 4 6
143 2 ▶ die 'JWT handler must be an instance of Catalyst::Plugin::OpenIDConnect::Utils::JWT'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value and $value->isa("Catalyst::Plugin::OpenIDConnect::Utils::JWT") 0 2 2
Condition33%1/3
ref $value and $value->isa("Catalyst::Plugin::OpenIDConnect::Utils::JWT")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $value and $value->isa("Catalyst::Plugin::OpenIDConnect::Utils::JWT")
ref $value $value->isa("Catalyst::Plugin::OpenIDConnect::Utils::JWT")
Truth table33%1/3
A and B
A= ref $valueB= $value->isa("Catalyst::Plugin::OpenIDConnect::Utils::JWT")
ABresult
0X0
100
111
144 unless ref $value && $value->isa('Catalyst::Plugin::OpenIDConnect::Utils::JWT');
145 2 $_oidc_jwt_by_class{$class} = $value;
146 }
147 6 return $_oidc_jwt_by_class{$class};
148 }
149  
150 =head2 _oidc_store
151  
152 State and code storage.
153  
154 =cut
155  
156 # Accessor method for Store handler
157 0 ▶sub _oidc_store {
Subroutine _oidc_store: not called CC=5 SCAR=34.0
158 0 my ($self, $value) = @_;
159 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
160 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
161 0 ▶ die 'Store handler must implement Catalyst::Plugin::OpenIDConnect::Role::Store'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value and $value->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store") 0 0 0
Condition0%0/3
ref $value and $value->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $value->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store")
ref $value $value->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store")
Truth table0%0/3
A and B
A= ref $valueB= $value->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store")
ABresult
0X0
100
111
162 unless ref $value && $value->DOES('Catalyst::Plugin::OpenIDConnect::Role::Store');
163 0 $_oidc_store_by_class{$class} = $value;
164 }
165 0 return $_oidc_store_by_class{$class};
166 }
167  
168 =head1 METHODS
169  
170 =head2 setup
171  
172 Catalyst setup hook - initialize the plugin. Note that this hook can effectively be blocked
173 in the consuming app by a similar setup method checking for configuration and deleting
174 $config->{issuer} if not properly configured. This allows the consuming app to control whether
175 the plugin initializes or not, which is useful for example in FastCGI deployments where multiple
176 apps share the same codebase but only some are OIDC providers.
177  
178 =cut
179  
180 0 ▶after 'setup' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
181 0 my ($app) = @_;
182
183 0 ▶ my $config = $app->config->{'Plugin::OpenIDConnect'} || {};
Condition0%0/2
$app->config->{'Plugin::OpenIDConnect'} || {}
l!l
00
MC/DC0%0/1
$app->config->{'Plugin::OpenIDConnect'} || {}
$app->config->{'Plugin::OpenIDConnect'}
Truth table0%0/2
A
A= $app->config->{'Plugin::OpenIDConnect'}
Aresult
11
00
184
185 0 ▶ $app->log->debug('OpenID Connect plugin setup starting') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
186
187 # Only initialize if properly configured
188 0 ▶ if ( $config->{issuer} && $config->{issuer}{private_key_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'issuer'} and $config->{'issuer'}{'private_key_file'}) { } 0 0 0
Condition0%0/3
$config->{"issuer"} and $config->{"issuer"}{"private_key_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"issuer"} and $config->{"issuer"}{"private_key_file"}
$config->{"issuer"} $config->{"issuer"}{"private_key_file"}
Truth table0%0/3
A and B
A= $config->{"issuer"}B= $config->{"issuer"}{"private_key_file"}
ABresult
0X0
100
111
189 0 ▶ try {
Subroutine __ANON__: not called CC=10 SCAR=47.0
190 0 ▶ $app->log->debug('Initializing OpenID Connect with issuer: ' . $config->{issuer}{url}) if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
191
192 # Create JWT handler
193 0 my $jwt = $app->_oidc_build_jwt_handler($config);
194 0 $app->_oidc_jwt($jwt);
195 0 ▶ $app->log->debug('JWT handler initialized successfully') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
196
197 # Create store - class and constructor args are configurable so that
198 # shared-memory backends (e.g. Redis) can be used under FastCGI.
199 my $store_class = $config->{store_class}
200 0 ▶ || 'Catalyst::Plugin::OpenIDConnect::Utils::Store';
Condition0%0/2
$config->{'store_class'} || 'Catalyst::Plugin::OpenIDConnect::Utils::Store'
l!l
00
MC/DC0%0/1
$config->{'store_class'} || 'Catalyst::Plugin::OpenIDConnect::Utils::Store'
$config->{'store_class'}
Truth table0%0/2
A
A= $config->{'store_class'}
Aresult
11
00
201 0 ▶ my $store_args = { %{ $config->{store_args} || {} } };
Condition0%0/2
$config->{'store_args'} || {}
l!l
00
MC/DC0%0/1
$config->{'store_args'} || {}
$config->{'store_args'}
Truth table0%0/2
A
A= $config->{'store_args'}
Aresult
11
00
202  
203 # Allow the Redis password to be supplied via the environment so
204 # that secrets are not embedded in application config files.
205 0 ▶ if ( !exists $store_args->{password} && defined $ENV{REDIS_PASSWORD} && $ENV{REDIS_PASSWORD} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"} and $ENV{"REDIS_PASSWORD"} ne "") 0 0 0
Condition0%0/6
not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"} and $ENV{"REDIS_PASSWORD"} ne ""
!ll&&!rl&&r
000
not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"} and $ENV{"REDIS_PASSWORD"} ne ""
not exists $store_args->{"password"} defined $ENV{"REDIS_PASSWORD"} $ENV{"REDIS_PASSWORD"} ne ""
Truth table0%0/4
not(A) and B and C
A= not exists $store_args->{"password"}B= defined $ENV{"REDIS_PASSWORD"}C= $ENV{"REDIS_PASSWORD"} ne ""
ABCresult
0XX0
10X0
1100
1111
206 0 $store_args->{password} = $ENV{REDIS_PASSWORD};
207 }
208  
209 # Dynamically load the store class (no-op if already loaded)
210 0 require Module::Runtime;
211 0 Module::Runtime::require_module($store_class);
212  
213 0 my $store = $store_class->new(
214 logger => $app->log,
215 %$store_args,
216 );
217 0 ▶ die "store_class '$store_class' does not implement Role::Store"
Branch: 1 branch
BranchTrueFalseTotal
unless $store->DOES("Catalyst::Plugin::OpenIDConnect::Role::Store") 0 0 0
218 unless $store->DOES('Catalyst::Plugin::OpenIDConnect::Role::Store');
219  
220 0 $app->_oidc_store($store);
221 0 ▶ $app->log->debug("State store initialized ($store_class)") if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
222 }
223 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
224 0 $app->log->error("Failed to initialize OpenID Connect plugin: $_");
225 0 die $_;
226 0 };
227 } else {
228 0 $app->log->warn('OpenID Connect plugin not configured (missing issuer.private_key_file)');
229 }
230 1 };
231  
232 0 ▶before setup_finalize => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
233 0 my ($app) = @_;
234 0 ▶ my $config = $app->config->{'Plugin::OpenIDConnect'} || {};
Condition0%0/2
$app->config->{'Plugin::OpenIDConnect'} || {}
l!l
00
MC/DC0%0/1
$app->config->{'Plugin::OpenIDConnect'} || {}
$app->config->{'Plugin::OpenIDConnect'}
Truth table0%0/2
A
A= $app->config->{'Plugin::OpenIDConnect'}
Aresult
11
00
235 0 ▶ $config->{debug} ||= $app->debug;
Condition0%0/3
$config->{"debug"} ||= $app->debug
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"debug"} ||= $app->debug
$config->{"debug"} $app->debug
Truth table0%0/3
A ||= B
A= $config->{"debug"}B= $app->debug
ABresult
1X1
011
000
236 1 };
237  
238 =head2 openidconnect()
239  
240 Returns the OIDC context/handler object for use in controllers.
241  
242 =cut
243  
244 0 ▶sub openidconnect {
Subroutine openidconnect: not called CC=1 SCAR=6.9
Pod documented
245 0 my ($c) = @_;
246 0 return Catalyst::Plugin::OpenIDConnect::Context->new( catalyst => $c );
247 }
248  
249 =head2 _oidc_build_jwt_handler($config)
250  
251 Builds and configures the JWT handler from config.
252  
253 =cut
254  
255 0 ▶sub _oidc_build_jwt_handler {
Subroutine _oidc_build_jwt_handler: not called CC=17 SCAR=57.2
256 0 my ( $c, $config ) = @_;
257  
258 0 ▶ $c->log->debug('Building JWT handler from configuration') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
259  
260 0 ▶ my $issuer_cfg = $config->{issuer} || {};
Condition0%0/2
$config->{'issuer'} || {}
l!l
00
MC/DC0%0/1
$config->{'issuer'} || {}
$config->{'issuer'}
Truth table0%0/2
A
A= $config->{'issuer'}
Aresult
11
00
261 0 ▶ my $issuer_url = $issuer_cfg->{url} || $c->uri_for('/')->as_string;
Condition0%0/3
$issuer_cfg->{'url'} || $c->uri_for('/')->as_string
l!l&&r!l&&!r
000
MC/DC0%0/2
$issuer_cfg->{'url'} || $c->uri_for('/')->as_string
$issuer_cfg->{'url'} $c->uri_for('/')->as_string
Truth table0%0/3
A || B
A= $issuer_cfg->{'url'}B= $c->uri_for('/')->as_string
ABresult
1X1
011
000
262  
263 0 ▶ $c->log->debug("JWT issuer URL: $issuer_url") if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
264  
265 # Load private key
266 my $private_key_file = $issuer_cfg->{private_key_file}
267 0 ▶ or die 'issuer.private_key_file is required';
Branch: 1 branch
BranchTrueFalseTotal
my $private_key_file = $issuer_cfg->{"private_key_file"} or die "issuer.private_key_file is required" 0 0 0
268  
269 0 ▶ $c->log->debug("Loading private key from: $private_key_file") if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
270
271 0 ▶ open my $key_fh, '<', $private_key_file
Branch: 1 branch
BranchTrueFalseTotal
open my $key_fh, "<", $private_key_file or die "Cannot read private key file: $!" 0 0 0
272 or die "Cannot read private key file: $!";
273 0 my $key_data = do { local $/; <$key_fh> };
274 0 close $key_fh;
275  
276 0 my $private_key = Crypt::OpenSSL::RSA->new_private_key($key_data);
277 0 ▶ $c->log->debug('Private key loaded successfully') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
278  
279 # Load or derive public key
280 0 my $public_key;
281 0 ▶ if ( my $public_key_file = $issuer_cfg->{public_key_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $public_key_file = $issuer_cfg->{'public_key_file'}) { } 0 0 0
282 0 ▶ $c->log->debug("Loading public key from: $public_key_file") if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
283
284 0 ▶ open $key_fh, '<', $public_key_file
Branch: 1 branch
BranchTrueFalseTotal
open $key_fh, "<", $public_key_file or die "Cannot read public key file: $!" 0 0 0
285 or die "Cannot read public key file: $!";
286 0 my $pub_data = do { local $/; <$key_fh> };
287 0 close $key_fh;
288 0 $public_key = Crypt::OpenSSL::RSA->new_public_key($pub_data);
289 0 ▶ $c->log->debug('Public key loaded successfully') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
290 } else {
291 0 ▶ $c->log->debug('Deriving public key from private key') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
292 # Extract public key from private key
293 0 $public_key = Crypt::OpenSSL::RSA->new_public_key(
294 $private_key->get_public_key_string()
295 );
296 0 ▶ $c->log->debug('Public key derived successfully') if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
297 }
298  
299 0 ▶ my $key_id = $issuer_cfg->{key_id} || 'default';
Condition0%0/2
$issuer_cfg->{'key_id'} || 'default'
l!l
00
MC/DC0%0/1
$issuer_cfg->{'key_id'} || 'default'
$issuer_cfg->{'key_id'}
Truth table0%0/2
A
A= $issuer_cfg->{'key_id'}
Aresult
11
00
300 0 ▶ $c->log->debug("Using key ID: $key_id") if $config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"debug"} 0 0 0
301  
302 0 return Catalyst::Plugin::OpenIDConnect::Utils::JWT->new(
303 private_key => $private_key,
304 public_key => $public_key,
305 key_id => $key_id,
306 issuer => $issuer_url,
307 logger => $c->log,
308 );
309 }
310  
311 1 1;
312  
313 =head1 AUTHOR
314  
315 Tim F. Rayner
316  
317 =head1 LICENSE
318  
319 This library is free software; you can redistribute it and/or modify
320 it under the terms of The Artistic License 2.0.
321  
322 =cut
« OpenIDConnect/Controller/Root.pm ↑ Summary OpenIDConnect/Utils/Store/Redis.pm »