Security/APIKey.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 75.0% 18 / 24 Conditioncond 61.5% 8 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 81.8% 81 / 99 CC 20 SCAR 16.3
CC
20
Cov
85%
_raw_query_param
8.1
_cookie
6.3
new
5.0
CRAP 5.1
↑ Summary Security/HTTPBearer.pm »
1 package PAGI::FastAPI::Security::APIKey;
2  
3 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent 'PAGI::FastAPI::Security::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = qv('v0.0.6');
9 2 our $AUTHORITY = 'cpan:MANWAR';
10  
11 =encoding utf-8
12  
13 =head1 NAME
14  
15 PAGI::FastAPI::Security::APIKey - API key authentication scheme for PAGI::FastAPI
16  
17 =head1 VERSION
18  
19 Version v0.0.6
20  
21 =head1 SYNOPSIS
22  
23 use PAGI::FastAPI::Security::APIKey;
24  
25 # X-API-Key: <key> header
26 my $api_key = PAGI::FastAPI::Security::APIKey->new(
27 in => 'header',
28 name => 'X-API-Key',
29 );
30  
31 # ?api_key=<key> query parameter
32 my $api_key = PAGI::FastAPI::Security::APIKey->new(
33 in => 'query',
34 name => 'api_key',
35 );
36  
37 # api_key=<key> cookie
38 my $api_key = PAGI::FastAPI::Security::APIKey->new(
39 in => 'cookie',
40 name => 'api_key',
41 );
42  
43 $app->get('/items',
44 dependencies => [ $api_key->depends(key => 'api_key') ],
45 handler => async sub ($c) {
46 my $key = $c->stash->{api_key};
47 return { items => [] };
48 },
49 );
50  
51 =head1 DESCRIPTION
52  
53 Pulls the API key from the HTTP request's header, query string or some cookie,
54 according to the design of your API. The key verification is not done in this
55 class, though; you have to verify the key yourself in the route handler.
56  
57 Important: Unlike query parameters declared in the route, here a query
58 string is read directly without the need to declare it in the route's
59 C<query> parameter map.
60  
61 =head1 CONSTRUCTOR OPTIONS
62  
63 =over 4
64  
65 =item * C<in> - B<Required>. One of C<'header'>, C<'query'>, or C<'cookie'>.
66  
67 =item * C<name> - B<Required>. The header/parameter/cookie name to read
68 the key from.
69  
70 =item * C<auto_error> - (Optional) See L<PAGI::FastAPI::Security::Base>.
71 Default: true. Failure is a plain C<403 Forbidden> (no challenge header),
72 matching the convention used for API keys (there is no standard
73 challenge scheme for them, unlike Basic/Bearer).
74  
75 =back
76  
77 =cut
78  
79 2 my %VALID_IN = map { $_ => 1 } qw(header query cookie);
80  
81 7 ▶sub new ($class, %opts) {
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
82 croak "APIKey requires an 'in' option (header|query|cookie)"
83 7 ▶ unless defined $opts{in} && $VALID_IN{$opts{in}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"in"} and $VALID_IN{$opts{"in"}} 2 5 7
Condition100%3/3
defined $opts{"in"} and $VALID_IN{$opts{"in"}}
!ll&&!rl&&r
115
MC/DC100%2/2
defined $opts{"in"} and $VALID_IN{$opts{"in"}}
defined $opts{"in"} $VALID_IN{$opts{"in"}}
Truth table100%3/3
A and B
A= defined $opts{"in"}B= $VALID_IN{$opts{"in"}}
ABresult
0X0
100
111
84 croak "APIKey requires a 'name' option"
85 5 ▶ unless defined $opts{name} && length $opts{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"name"} and length $opts{"name"} 1 4 5
Condition66%2/3
defined $opts{"name"} and length $opts{"name"}
!ll&&!rl&&r
104
MC/DC50%1/2
defined $opts{"name"} and length $opts{"name"}
defined $opts{"name"} length $opts{"name"}
Truth table66%2/3
A and B
A= defined $opts{"name"}B= length $opts{"name"}
ABresult
0X0
100
111
86  
87 4 return $class->SUPER::new(%opts);
88 }
89  
90 7 ▶sub _extract ($self, $c) {
Subroutine _extract: called CC=4 SCAR=14.0
91 7 my $in = $self->{in};
92  
93 7 ▶ if ($in eq 'header') {
Branch: 3 branches
BranchTrueFalseTotal
if ($in eq 'header') { } 3 4 7
elsif ($in eq 'query') { } 2 2 4
elsif ($in eq 'cookie') { } 2 0 2
94 3 return $c->header($self->{name});
95 }
96 elsif ($in eq 'query') {
97 2 return _raw_query_param($c, $self->{name});
98 }
99 elsif ($in eq 'cookie') {
100 2 return _cookie($c, $self->{name});
101 }
102 0 return undef;
103 }
104  
105 # Read directly from the raw query string rather than $c->query_param(),
106 # since PAGI::FastAPI only populates $c->query_params with params the
107 # route explicitly declared a type for, an API key shouldn't need to
108 # be redundantly declared on every protected route.
109 2 ▶sub _raw_query_param ($c, $name) {
Subroutine _raw_query_param: called CC=7 SCAR=20.9
110 2 my $qs = $c->scope->{query_string};
111 2 ▶ return undef unless defined $qs && length $qs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qs and length $qs 0 2 2
Condition33%1/3
defined $qs and length $qs
!ll&&!rl&&r
002
MC/DC0%0/2
defined $qs and length $qs
defined $qs length $qs
Truth table33%1/3
A and B
A= defined $qsB= length $qs
ABresult
0X0
100
111
112  
113 2 for my $pair (split '&', $qs) {
114 2 my ($k, $v) = split '=', $pair, 2;
115 2 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 2 2
116 2 ▶ return _uri_unescape($v // '') if _uri_unescape($k) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if _uri_unescape($k) eq $name 1 1 2
Condition50%1/2
$v // ""
l!l
10
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
117 }
118 1 return undef;
119 }
120  
121 2 ▶sub _cookie ($c, $name) {
Subroutine _cookie: called CC=6 SCAR=18.4
122 2 my $header = $c->header('Cookie');
123 2 ▶ return undef unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 0 2 2
124  
125 2 for my $pair (split /;\s*/, $header) {
126 4 my ($k, $v) = split '=', $pair, 2;
127 4 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 4 4
128 4 ▶ return _uri_unescape($v // '') if $k eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $name 1 3 4
Condition50%1/2
$v // ""
l!l
10
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
129 }
130 1 return undef;
131 }
132  
133 4 ▶sub _uri_unescape ($str) {
Subroutine _uri_unescape: called CC=2 SCAR=7.0
134 4 ▶ return $str unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 4 4
135 4 $str =~ tr/+/ /;
136 4 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
137 4 return $str;
138 }
139  
140 =head1 AUTHOR
141  
142 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
143  
144 =head1 REPOSITORY
145  
146 L<https://github.com/manwar/PAGI-FastAPI-Security>
147  
148 =head1 BUGS
149  
150 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
151 I will be notified and then you'll automatically be notified of progress on your
152 bug as I make changes.
153  
154 =head1 SUPPORT
155  
156 You can find documentation for this module with the perldoc command.
157  
158 perldoc PAGI::FastAPI::Security::APIKey
159  
160 You can also look for information at:
161  
162 =over 4
163  
164 =item * BUG Report
165  
166 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
167  
168 =item * Search MetaCPAN
169  
170 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
171  
172 =back
173  
174 =head1 LICENSE AND COPYRIGHT
175  
176 Copyright (C) 2026 Mohammad Sajid Anwar.
177  
178 This program is free software; you can redistribute it and/or modify it under
179 the terms of the Artistic License (2.0). You may obtain a copy of the full
180 license at:
181  
182 L<http://www.perlfoundation.org/artistic_license_2_0>
183  
184 =cut
185  
186 2 1; # End of PAGI::FastAPI::Security::APIKey
↑ Summary Security/HTTPBearer.pm »