blib/lib/Mojolicious/Plugin/Kinde.pm

filter: Statementstmt 34.6% 17 / 49 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 40.0% 4 / 10 Podpod 100% 1 / 1 totaltotal 22.4% 22 / 98 CC 14 SCAR 34.3
CC
14
Cov
22%
_validate
72.0
register
20.0
_validate_route
12.0
CRAP 31.0
↑ Summary
1 package Mojolicious::Plugin::Kinde;
2 # ABSTRACT: A Mojo helper and route condition to extract Kinde auth header, verify JWT token, and return the claims
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v0.0.2';
10  
11 1 has jwt => undef;
12 1 has iss => '';
13 1 has audience => '';
14  
15 0 ▶sub register {
Subroutine register: not called CC=4 SCAR=30.0
Pod documented
16 0 my ( $self, $app, $conf ) = @_;
17  
18 0 ▶ my $jwks_url = $conf->{jwks_url} || $app->config->{kinde}->{jwks_url};
Condition0%0/3
$conf->{'jwks_url'} || $app->config->{'kinde'}{'jwks_url'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{'jwks_url'} || $app->config->{'kinde'}{'jwks_url'}
$conf->{'jwks_url'} $app->config->{'kinde'}{'jwks_url'}
Truth table0%0/3
A || B
A= $conf->{'jwks_url'}B= $app->config->{'kinde'}{'jwks_url'}
ABresult
1X1
011
000
19 0 my $jwks_keys = Mojo::UserAgent->new->get($jwks_url)->result->json('/keys');
20 0 $self->jwt( Mojo::JWT->new( jwks => $jwks_keys ) );
21  
22 0 ▶ $self->iss( $conf->{iss} || $app->config->{kinde}->{iss} );
Condition0%0/3
$conf->{'iss'} || $app->config->{'kinde'}{'iss'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{'iss'} || $app->config->{'kinde'}{'iss'}
$conf->{'iss'} $app->config->{'kinde'}{'iss'}
Truth table0%0/3
A || B
A= $conf->{'iss'}B= $app->config->{'kinde'}{'iss'}
ABresult
1X1
011
000
23 0 ▶ $self->audience( $conf->{audience} || $app->config->{kinde}->{audience} );
Condition0%0/3
$conf->{'audience'} || $app->config->{'kinde'}{'audience'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{'audience'} || $app->config->{'kinde'}{'audience'}
$conf->{'audience'} $app->config->{'kinde'}{'audience'}
Truth table0%0/3
A || B
A= $conf->{'audience'}B= $app->config->{'kinde'}{'audience'}
ABresult
1X1
011
000
24  
25 0 ▶ $app->helper( get_kinde_claims => sub { _validate_auth_header( $self, @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
26  
27 0 ▶ $app->routes->add_condition( kinde_auth => sub { _validate_route( $self, @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
28  
29 } ## end sub register
30  
31 0 ▶sub _validate {
Subroutine _validate: not called CC=8 SCAR=42.8
32 0 my ( $self, $c, $token ) = @_;
33  
34 0 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) { } 0 0 0
35 0 ▶ my $token_data = $token ? $self->jwt->decode($token) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 0 0 0
36  
37 0 ▶ Mojo::Exception->throw('The token does not exist or is not valid') unless $token_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_data 0 0 0
38 0 ▶ Mojo::Exception->throw('The `iss` claim does not match') unless $token_data->{iss} eq $self->iss;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_data->{"iss"} eq $self->iss 0 0 0
39 0 ▶ Mojo::Exception->throw('The signature `alg` is not RS256') unless $self->jwt->algorithm eq 'RS256';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->jwt->algorithm eq "RS256" 0 0 0
40 Mojo::Exception->throw('The expected audience is missing')
41 if $self->audience
42 0 ▶ && ( scalar grep { $_ eq $self->audience } @{ $token_data->{aud} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->audience and scalar grep({$_ eq $self->audience;} @{$$token_data{"aud"};}) == 0 0 0 0
Condition0%0/3
$self->audience and scalar grep({$_ eq $self->audience;} @{$$token_data{"aud"};}) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->audience and scalar grep({$_ eq $self->audience;} @{$$token_data{"aud"};}) == 0
$self->audience scalar grep({$_ eq $self->audience;} @{$$token_data{"aud"};}) == 0
Truth table0%0/3
A and B
A= $self->audienceB= scalar grep({$_ eq $self->audience;} @{$$token_data{"aud"};}) == 0
ABresult
0X0
100
111
43  
44 0 $c->stash->{kinde_user} = { id => $token_data->{'sub'} };
45  
46 0 return $token_data;
47  
48 } else {
49  
50 0 return undef;
51  
52 } ## end if ($token)
53  
54 } ## end sub _validate
55  
56 0 ▶sub _validate_auth_header {
Subroutine _validate_auth_header: not called CC=2 SCAR=17.9
57 0 my ( $self, $c ) = @_;
58  
59 0 my $headers = $c->req->headers;
60 0 my $auth = $headers->header('Authorization');
61 0 ▶ my $token = $auth ? ( split( ' ', $auth ) )[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$auth ? : 0 0 0
62  
63 0 return $self->_validate( $c, $token );
64  
65 } ## end sub _validate_auth_header
66  
67 0 ▶sub _validate_route {
Subroutine _validate_route: not called CC=3 SCAR=24.8
68 0 my ( $self, $route, $c, $captures, $arg ) = @_;
69  
70 0 my $headers = $c->req->headers;
71 0 my $auth = $headers->header('Authorization');
72 0 ▶ my $token = $auth ? ( split( ' ', $auth ) )[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$auth ? : 0 0 0
73  
74 0 ▶ return $self->_validate( $c, $token ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_validate($c, $token) ? : 0 0 0
75  
76 } ## end sub _validate_route
77  
78  
79 1 1;
80  
81 =pod
82  
83 =encoding UTF-8
84  
85 =head1 NAME
86  
87 Mojolicious::Plugin::Kinde - A Mojo helper and route condition to extract Kinde auth header, verify JWT token, and return the claims
88  
89 =head1 VERSION
90  
91 version v0.0.1
92  
93 =head1 SYNOPSIS
94  
95 package MyApp;
96 use Mojo::Base 'Mojolicious';
97  
98 sub startup {
99 my $self = shift;
100  
101 $self->plugin( 'Mojolicious::Plugin::Kinde' ); # config can also be supplied here
102  
103 $self->routes->get('/api')->requires( kinde_auth => {} );
104  
105 }
106
107 ...
108
109 # myapp_mojo.pl (config)
110  
111 {
112 kinde => {
113 jwks_url => 'https://your-domain.kinde.com/.well-known/jwks.json',
114 iss => 'https://your-domain.kinde.com',
115 },
116 }
117
118 ...
119
120 my $claims = $c->get_kinde_claims
121  
122 =head1 DESCRIPTION
123  
124 Mojolicious::Plugin::Kinde creates a helper method and a route condition. Both retrieve the JWT
125 token from the C<Authorization> header, verify the JWT, and do some sanity checks on the claims.
126 The C<get_kinde_claims> helper will return the claims extracted from the token.
127  
128 The sanity checks include:
129  
130 =over 4
131  
132 =item confirm C<iss> claim matches the value from C<kinde> config
133  
134 =item confirm the C<alg> is C<RS256>
135  
136 =item confirm the C<aud> array contains the value from C<kinde> config (if not empty)
137  
138 =back
139  
140 =head1 HELPERS
141  
142 =head2 get_kinde_claims()
143  
144 Verifies the JWT from C<Authorization> header and returns the extracted claims.
145  
146 =head1 ROUTE CONDITIONS
147  
148 =head2 kinde_auth
149  
150 Verifies the JWT from C<Authorization> header and returns boolean for the route condition.
151  
152 =head1 METHODS
153  
154 Mojolicious::Plugin::Kinde inherits all methods from Mojolicious::Plugin and implements the following new ones.
155  
156 =head2 register
157  
158 $plugin->register(Mojolicious->new);
159  
160 Register conditions in Mojolicious application.
161  
162 =head1 SEE ALSO
163  
164 =over 4
165  
166 =item L<Mojo::JWT>
167  
168 =item L<Kinde|https://kinde.com/> fantastic auth service
169  
170 =back
171  
172 =head1 SUPPORT
173
174 =head2 Bugs / Feature Requests
175
176 Please report any bugs or feature requests through the issue tracker at
177 https://github.com/cngarrison/Mojolicious-Plugin-Kinde/issues. You will
178 be notified automatically of any progress on your issue.
179
180 =head2 Source Code
181
182 This is open source software. The code repository is available for
183 public review and contribution under the terms of the license.
184  
185 https://github.com/cngarrison/Mojolicious-Plugin-Kinde
186  
187 git clone git://github.com/cngarrison/Mojolicious-Plugin-Kinde.git
188  
189 =head1 AUTHOR
190  
191 Charlie Garrison <cng@cngarrison.com>
192  
193 =head1 COPYRIGHT AND LICENSE
194  
195 This software is copyright (c) 2023 by Charlie Garrison.
196  
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
199  
200 =cut
201  
202 __END__
↑ Summary