Security/Base.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 2 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 90.9% 10 / 11 Podpod 66.6% 2 / 3 totaltotal 79.6% 51 / 64 CC 8 SCAR 13.5
CC
8
Cov
81%
challenge_header
12.0
_run
4.0
new
3.0
CRAP 3.8
« Security/HTTPBearer.pm ↑ Summary Security/HTTPBasic.pm »
1 package PAGI::FastAPI::Security::Base;
2  
3 5 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use PAGI::FastAPI::Depends qw(Depends);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = qv('v0.0.6');
10 5 our $AUTHORITY = 'cpan:MANWAR';
11  
12 =encoding utf-8
13  
14 =head1 NAME
15  
16 PAGI::FastAPI::Security::Base - Internal base class for PAGI::FastAPI::Security schemes
17  
18 =head1 VERSION
19  
20 Version v0.0.6
21  
22 =head1 DESCRIPTION
23  
24 Main usage. This will provide you with the common C<depends()> wiring and
25 401/403 failure helpers on which L<PAGI::FastAPI::Security::HTTPBearer>,
26 L<PAGI::FastAPI::Security::HTTPBasic>, L<PAGI::FastAPI::Security::APIKey>,
27 and L<PAGI::FastAPI::Security::OAuth2::PasswordBearer> are based.
28  
29 Concrete classes will have to implemente C<_extract($c)>,
30 which will have to return extracted credential value/structure on success,
31 or C<undef> on failure.
32  
33 =cut
34  
35 10 ▶sub new ($class, %opts) {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
36 10 ▶ croak "$class is abstract; use a concrete scheme subclass" if $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $class eq __PACKAGE__ 0 10 10
37 return bless {
38 10 ▶ auto_error => $opts{auto_error} // 1,
Condition100%2/2
$opts{"auto_error"} // 1
l!l
28
MC/DC100%1/1
$opts{"auto_error"} // 1
$opts{"auto_error"}
Truth table100%2/2
A
A= $opts{"auto_error"}
Aresult
11
00
39 %opts,
40 }, $class;
41 }
42  
43 =head2 C<depends(%opts)>
44  
45 Returns a L<PAGI::FastAPI::Depends> object suitable for a route's
46 C<dependencies> list. C<%opts> supports the same C<key> option as
47 C<Depends()> itself.
48  
49 dependencies => [ $scheme->depends(key => 'token') ],
50  
51 =cut
52  
53 8 ▶sub depends ($self, %opts) {
Subroutine depends: called CC=1 SCAR=0.0
Pod documented
54 22 return Depends(
55 8 ▶ async sub ($c) { return $self->_run($c) },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 %opts,
57 );
58 }
59  
60 =head2 challenge_header
61  
62 my ($key, $value) = $security->challenge_header;
63 $c->set_header($security->challenge_header);
64  
65 Generates a key-value pair suitable for setting a C<WWW-Authenticate> HTTP header
66 on authentication failure responses in accordance with RFC 9110.
67  
68 Returns a two-element array C<('WWW-Authenticate', $challenge_string)>.
69  
70 =over 4
71  
72 =item * C<%args> - (Optional) Additional parameters passed to format scheme-specific challenges.
73  
74 =back
75  
76 By default, this method constructs a basic challenge string using the class scheme
77 and the configured C<realm> option:
78  
79 WWW-Authenticate: <Scheme> realm="<realm>"
80  
81 =cut
82  
83 0 ▶sub challenge_header ($self, %args) {
Subroutine challenge_header: not called CC=3 SCAR=24.8
Pod documented
84 0 ▶ my $scheme = $self->{scheme} // 'Bearer';
Condition0%0/2
$self->{"scheme"} // "Bearer"
l!l
00
MC/DC0%0/1
$self->{"scheme"} // "Bearer"
$self->{"scheme"}
Truth table0%0/2
A
A= $self->{"scheme"}
Aresult
11
00
85 0 ▶ my $realm = $self->{realm} // 'Restricted';
Condition0%0/2
$self->{"realm"} // "Restricted"
l!l
00
MC/DC0%0/1
$self->{"realm"} // "Restricted"
$self->{"realm"}
Truth table0%0/2
A
A= $self->{"realm"}
Aresult
11
00
86 0 $realm =~ s/"/\\"/g;
87  
88 0 return ('WWW-Authenticate' => qq{$scheme realm="$realm"});
89 }
90  
91 # Shared execution wrapper: calls the subclass's _extract(), and on
92 # failure applies the standard auto_error behaviour (set $c->status(...)
93 # to a value >= 400 and return a body hash), which is the actual failure
94 # contract PAGI::FastAPI's dispatcher checks for.
95 22 ▶sub _run ($self, $c) {
Subroutine _run: called CC=4 SCAR=13.9
96 22 my $value = $self->_extract($c);
97 22 ▶ return $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 7 15 22
98  
99 15 ▶ return undef unless $self->{auto_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auto_error"} 2 13 15
100  
101 13 my ($status, $detail, @headers) = $self->_failure;
102 13 $c->status($status);
103 13 $c->set_header(@$_) for @headers;
104 13 return { detail => $detail };
105 }
106  
107 # Default failure: 403 Forbidden, no challenge header. Bearer/Basic/OAuth2
108 # override this to send a 401 + WWW-Authenticate challenge instead, per
109 # RFC 7235 / RFC 6750.
110 3 ▶sub _failure ($self) {
Subroutine _failure: called CC=1 SCAR=0.0
111 3 return (403, 'Not authenticated');
112 }
113  
114 =head1 AUTHOR
115  
116 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
117  
118 =head1 REPOSITORY
119  
120 L<https://github.com/manwar/PAGI-FastAPI-Security>
121  
122 =head1 BUGS
123  
124 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
125 I will be notified and then you'll automatically be notified of progress on your
126 bug as I make changes.
127  
128 =head1 SUPPORT
129  
130 You can find documentation for this module with the perldoc command.
131  
132 perldoc PAGI::FastAPI::Security::Base
133  
134 You can also look for information at:
135  
136 =over 4
137  
138 =item * BUG Report
139  
140 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
141  
142 =item * Search MetaCPAN
143  
144 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
145  
146 =back
147  
148 =head1 LICENSE AND COPYRIGHT
149  
150 Copyright (C) 2026 Mohammad Sajid Anwar.
151  
152 This program is free software; you can redistribute it and/or modify it under
153 the terms of the Artistic License (2.0). You may obtain a copy of the full
154 license at:
155  
156 L<http://www.perlfoundation.org/artistic_license_2_0>
157  
158 =cut
159  
160 5 1; # End of PAGI::FastAPI::Security::Base
« Security/HTTPBearer.pm ↑ Summary Security/HTTPBasic.pm »