OpenIDConnect/Context.pm

filter: Statementstmt 82.5% 71 / 86 Branchbran 50.0% 24 / 48 Conditioncond 38.7% 12 / 31 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 90.9% 10 / 11 Podpod 100% 8 / 8 totaltotal 63.3% 128 / 202 CC 39 SCAR 24.5
CC
39
Cov
65%
scope_handler
30.0
get_user_claims
14.1
store
13.1
CRAP 11.6
« OpenIDConnect/Utils/Store/Redis.pm ↑ Summary OpenIDConnect/Utils/JWT.pm »
1 package Catalyst::Plugin::OpenIDConnect::Context;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Catalyst::Plugin::OpenIDConnect::Utils::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Per-application-class storage for extension-point callbacks.
8 # Keyed by the consuming application class name so that multiple Catalyst apps
9 # loaded in the same interpreter each hold their own handlers.
10 1 my %_scope_handler_by_class;
11 my %_claims_provider_by_class;
12  
13 =head1 NAME
14  
15 Catalyst::Plugin::OpenIDConnect::Context - OIDC provider context object
16  
17 =head1 DESCRIPTION
18  
19 Context object passed to controllers for accessing OIDC functionality.
20  
21 =head1 ATTRIBUTES
22  
23 =head2 catalyst
24  
25 The Catalyst application instance.
26  
27 =cut
28  
29 1 has catalyst => (
30 is => 'ro',
31 required => 1,
32 );
33  
34 =head1 METHODS
35  
36 =head2 jwt()
37  
38 Returns the JWT handler instance.
39  
40 =cut
41  
42 1 ▶sub jwt {
Subroutine jwt: called CC=3 SCAR=12.3
Pod documented
43 1 my ($self) = @_;
44 1 ▶ $self->catalyst->log->debug('Retrieving JWT handler') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 1 1
45 1 my $jwt = $self->catalyst->_oidc_jwt();
46 1 ▶ unless ($jwt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($jwt) 0 1 1
47 0 $self->catalyst->log->error('OpenID Connect JWT handler not initialized');
48 0 die 'OpenID Connect JWT handler not initialized. Check your Plugin::OpenIDConnect configuration (issuer.private_key_file and issuer.public_key_file required).';
49 }
50 1 return $jwt;
51 }
52  
53 =head2 store()
54  
55 Returns the state store instance.
56  
57 =cut
58  
59 2 ▶sub store {
Subroutine store: called CC=10 SCAR=25.7
Pod documented
60 2 my ($self) = @_;
61 2 ▶ $self->catalyst->log->debug('Retrieving state store') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 2 2
62 2 my $store = $self->catalyst->_oidc_store();
63 2 ▶ return $store if $store;
Branch: 1 branch
BranchTrueFalseTotal
if $store 1 1 2
64  
65 1 ▶ $self->catalyst->log->debug('Creating new state store instance') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 1 1
66  
67 my $store_class = $self->config->{store_class}
68 1 ▶ || 'Catalyst::Plugin::OpenIDConnect::Utils::Store';
Condition50%1/2
$self->config->{'store_class'} || 'Catalyst::Plugin::OpenIDConnect::Utils::Store'
l!l
01
MC/DC0%0/1
$self->config->{'store_class'} || 'Catalyst::Plugin::OpenIDConnect::Utils::Store'
$self->config->{'store_class'}
Truth table50%1/2
A
A= $self->config->{'store_class'}
Aresult
11
00
69 1 ▶ my $store_args = { %{ $self->config->{store_args} || {} } };
Condition50%1/2
$self->config->{'store_args'} || {}
l!l
01
MC/DC0%0/1
$self->config->{'store_args'} || {}
$self->config->{'store_args'}
Truth table50%1/2
A
A= $self->config->{'store_args'}
Aresult
11
00
70  
71 # Allow the Redis password to be supplied via the environment so
72 # that secrets are not embedded in application config files.
73 1 ▶ 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 1 1
Condition33%2/6
not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"} and $ENV{"REDIS_PASSWORD"} ne ""
!ll&&!rl&&r
100
not exists $store_args->{"password"} and defined $ENV{"REDIS_PASSWORD"}
!ll&&!rl&&r
010
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 table25%1/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
74 0 $store_args->{password} = $ENV{REDIS_PASSWORD};
75 }
76  
77 1 require Module::Runtime;
78 1 Module::Runtime::require_module($store_class);
79  
80 1 my $new_store = $store_class->new(
81 logger => $self->catalyst->log,
82 %$store_args,
83 );
84 1 ▶ $self->catalyst->_oidc_store($new_store) if $self->catalyst->can('_oidc_store');
Branch: 1 branch
BranchTrueFalseTotal
if $self->catalyst->can("_oidc_store") 1 0 1
85 1 return $new_store;
86 }
87  
88 =head2 config()
89  
90 Returns the OIDC configuration.
91  
92 =cut
93  
94 34 ▶sub config {
Subroutine config: called CC=2 SCAR=6.9
Pod documented
95 34 my ($self) = @_;
96 34 ▶ return $self->catalyst->config->{'Plugin::OpenIDConnect'} || {};
Condition100%2/2
$self->catalyst->config->{'Plugin::OpenIDConnect'} || {}
l!l
304
MC/DC100%1/1
$self->catalyst->config->{'Plugin::OpenIDConnect'} || {}
$self->catalyst->config->{'Plugin::OpenIDConnect'}
Truth table100%2/2
A
A= $self->catalyst->config->{'Plugin::OpenIDConnect'}
Aresult
11
00
97 }
98  
99 =head2 get_client($client_id)
100  
101 Retrieves a client configuration by client ID.
102  
103 =cut
104  
105 4 ▶sub get_client {
Subroutine get_client: called CC=5 SCAR=16.2
Pod documented
106 4 my ( $self, $client_id ) = @_;
107 4 ▶ $self->catalyst->log->debug("Looking up client: $client_id") if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 4 4
108 4 ▶ my $clients = $self->config->{clients} || {};
Condition100%2/2
$self->config->{'clients'} || {}
l!l
31
MC/DC100%1/1
$self->config->{'clients'} || {}
$self->config->{'clients'}
Truth table100%2/2
A
A= $self->config->{'clients'}
Aresult
11
00
109 4 my $client = $clients->{$client_id};
110 4 ▶ if ($client) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client) { } 2 2 4
111 2 ▶ $self->catalyst->log->debug("Found client configuration for: $client_id") if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 2 2
112 } else {
113 2 $self->catalyst->log->warn("Client not found: $client_id");
114 }
115 4 return $client;
116 }
117  
118 =head2 scope_handler($handler)
119  
120 Sets or retrieves the custom scope validation/processing handler.
121  
122 When a handler is set, it is called during authorization after the built-in
123 scope intersection with the client's registered scopes. The handler receives
124 the Catalyst context object and the effective, space-separated scope string,
125 and must return a list of scopes. Throwing an exception from the handler
126 will reject the authorization request with an C<invalid_scope> error.
127  
128 Handlers are stored per consuming application class, so setting a handler
129 once (e.g. during application startup) applies to all subsequent requests.
130  
131 $c->openidconnect->scope_handler(sub {
132 my ($c, $scope_string) = @_;
133 my @scopes = split /\s+/, $scope_string;
134 for my $scope (@scopes) {
135 die "Unknown scope: $scope" unless valid_scope($scope);
136 }
137 return @scopes;
138 });
139  
140 =cut
141  
142 0 ▶sub scope_handler {
Subroutine scope_handler: not called CC=5 SCAR=34.0
Pod documented
143 0 my ( $self, $handler ) = @_;
144 0 ▶ my $class = ref( $self->catalyst ) || $self->catalyst;
Condition0%0/3
ref $self->catalyst || $self->catalyst
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self->catalyst || $self->catalyst
ref $self->catalyst $self->catalyst
Truth table0%0/3
A || B
A= ref $self->catalystB= $self->catalyst
ABresult
1X1
011
000
145 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
146 0 ▶ die 'scope_handler must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $handler and ref $handler ne "CODE" 0 0 0
Condition0%0/3
defined $handler and ref $handler ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $handler and ref $handler ne "CODE"
defined $handler ref $handler ne "CODE"
Truth table0%0/3
A and B
A= defined $handlerB= ref $handler ne "CODE"
ABresult
0X0
100
111
147 if defined $handler && ref $handler ne 'CODE';
148 0 $_scope_handler_by_class{$class} = $handler;
149 0 return $self;
150 }
151 0 return $_scope_handler_by_class{$class};
152 }
153  
154 =head2 claims_provider($provider)
155  
156 Sets or retrieves the custom claims provider.
157  
158 When a provider is set, it is called by C<get_user_claims()> instead of the
159 default config-based field mapping. The provider receives the Catalyst
160 context object and the user object (hash reference or object with accessors),
161 and must return a hash reference of JWT claims.
162  
163 Providers are stored per consuming application class, so setting a provider
164 once (e.g. during application startup) applies to all subsequent requests.
165  
166 $c->openidconnect->claims_provider(sub {
167 my ($c, $user) = @_;
168 return {
169 sub => $user->id,
170 name => $user->full_name,
171 custom_claim => $user->some_attribute,
172 };
173 });
174  
175 =cut
176  
177 3 ▶sub claims_provider {
Subroutine claims_provider: called CC=5 SCAR=24.7
Pod documented
178 3 my ( $self, $provider ) = @_;
179 3 ▶ my $class = ref( $self->catalyst ) || $self->catalyst;
Condition33%1/3
ref $self->catalyst || $self->catalyst
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self->catalyst || $self->catalyst
ref $self->catalyst $self->catalyst
Truth table33%1/3
A || B
A= ref $self->catalystB= $self->catalyst
ABresult
1X1
011
000
180 3 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
181 0 ▶ die 'claims_provider must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $provider and ref $provider ne "CODE" 0 0 0
Condition0%0/3
defined $provider and ref $provider ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $provider and ref $provider ne "CODE"
defined $provider ref $provider ne "CODE"
Truth table0%0/3
A and B
A= defined $providerB= ref $provider ne "CODE"
ABresult
0X0
100
111
182 if defined $provider && ref $provider ne 'CODE';
183 0 $_claims_provider_by_class{$class} = $provider;
184 0 return $self;
185 }
186 3 return $_claims_provider_by_class{$class};
187 }
188  
189 =head2 get_user_claims($user)
190  
191 Extracts user claims based on the configured user_claims mapping.
192  
193 The user parameter can be a hash reference or an object with accessor methods.
194  
195 =cut
196  
197 3 ▶sub get_user_claims {
Subroutine get_user_claims: called CC=12 SCAR=26.4
Pod documented
198 3 my ( $self, $user ) = @_;
199  
200 3 ▶ $self->catalyst->log->debug('Extracting user claims') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 3 3
201  
202 # Delegate to the custom claims provider when one has been registered.
203 3 ▶ if ( my $provider = $self->claims_provider ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->claims_provider) 0 3 3
204 0 ▶ $self->catalyst->log->debug('Using custom claims provider') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 0 0
205 0 return $provider->( $self->catalyst, $user );
206 }
207  
208 my $claims_config = $self->config->{user_claims} || {
209 3 ▶ sub => 'id',
Condition100%2/2
$self->config->{'user_claims'} || {'sub', 'id', 'name', 'name', 'email', 'email'}
l!l
21
MC/DC100%1/1
$self->config->{'user_claims'} || {'sub', 'id', 'name', 'name', 'email', 'email'}
$self->config->{'user_claims'}
Truth table100%2/2
A
A= $self->config->{'user_claims'}
Aresult
11
00
210 name => 'name',
211 email => 'email',
212 };
213  
214 3 my %claims;
215  
216 3 for my $claim_name ( keys %$claims_config ) {
217 11 my $accessor = $claims_config->{$claim_name};
218 11 my @parts = split /\./, $accessor;
219  
220 11 my $value = $user;
221 11 for my $part (@parts) {
222 13 ▶ last unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 13 13
223 13 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 8 5 13
224 8 $value = $value->{$part};
225 } else {
226 5 ▶ $value = $value->$part() if $value->can($part);
Branch: 1 branch
BranchTrueFalseTotal
if $value->can($part) 3 2 5
227 }
228 }
229  
230 11 ▶ $claims{$claim_name} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 11 0 11
231 }
232  
233 3 ▶ $self->catalyst->log->debug('User claims extracted: ' . join(', ', keys %claims)) if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 3 3
234  
235 3 return \%claims;
236 }
237  
238 =head2 get_discovery()
239  
240 Returns the OpenID Connect provider configuration document.
241  
242 =cut
243  
244 2 ▶sub get_discovery {
Subroutine get_discovery: called CC=4 SCAR=14.6
Pod documented
245 2 my ($self) = @_;
246  
247 2 ▶ $self->catalyst->log->debug('Building OpenID Connect discovery document') if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 2 2
248  
249 2 my $c = $self->catalyst;
250 2 ▶ my $issuer_url = $self->config->{issuer}{url} || $c->uri_for('/')->as_string;
Condition33%1/3
$self->config->{'issuer'}{'url'} || $c->uri_for('/')->as_string
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->config->{'issuer'}{'url'} || $c->uri_for('/')->as_string
$self->config->{'issuer'}{'url'} $c->uri_for('/')->as_string
Truth table33%1/3
A || B
A= $self->config->{'issuer'}{'url'}B= $c->uri_for('/')->as_string
ABresult
1X1
011
000
251  
252 # Extract scheme and authority from issuer URL to ensure endpoints match issuer scheme
253 2 my $base_url = $issuer_url;
254 2 $base_url =~ s{/$}{}; # Remove trailing slash if present
255  
256 2 ▶ $self->catalyst->log->debug("Discovery document built for issuer: $issuer_url") if $self->config->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"debug"} 0 2 2
257  
258 return {
259 2 issuer => $issuer_url,
260 authorization_endpoint => "$base_url/openidconnect/authorize",
261 token_endpoint => "$base_url/openidconnect/token",
262 userinfo_endpoint => "$base_url/openidconnect/userinfo",
263 jwks_uri => "$base_url/openidconnect/jwks",
264 end_session_endpoint => "$base_url/openidconnect/logout",
265 registration_endpoint => undef,
266 scopes_supported => [qw(openid profile email phone address)],
267 response_types_supported => [qw(code)],
268 response_modes_supported => [qw(query fragment form_post)],
269 grant_types_supported => [qw(authorization_code refresh_token)],
270 subject_types_supported => [qw(public pairwise)],
271 id_token_signing_alg_values_supported => ['RS256'],
272 userinfo_signing_alg_values_supported => ['RS256'],
273 request_parameter_supported => 1,
274 request_uri_parameter_supported => 1,
275 claims_supported => [
276 qw(
277 sub name given_name family_name middle_name nickname
278 preferred_username profile picture website email email_verified
279 gender birthdate zoneinfo locale phone_number phone_number_verified
280 address updated_at
281 )
282 ],
283 claim_types_supported => [qw(normal aggregated distributed)],
284 };
285 }
286  
287 1 __PACKAGE__->meta->make_immutable;
288 1 1;
289  
290 =head1 AUTHOR
291  
292 Tim F. Rayner
293  
294 =head1 LICENSE
295  
296 This library is free software; you can redistribute it and/or modify
297 it under the terms of The Artistic License 2.0.
298  
299 =cut
« OpenIDConnect/Utils/Store/Redis.pm ↑ Summary OpenIDConnect/Utils/JWT.pm »