Security/Base.pm

filter: Statementstmt 100% 31 / 31 Branchbran 83.3% 5 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 96.1% 50 / 52 CC 6 SCAR 7.9
CC
6
Cov
97%
_run
4.0
new
3.0
BEGIN
1.0
CRAP 2.2
« Security/OAuth2/PasswordBearer.pm ↑ Summary Security/HTTPBearer.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.4');
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.4
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 ▶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 ▶sub depends ($self, %opts) {
Subroutine depends: called CC=1 SCAR=0.0
Pod documented
54 22 ▶ return Depends(
Subroutine __ANON__: called CC=1 SCAR=0.0
55 8 async sub ($c) { return $self->_run($c) },
56 %opts,
57 );
58 }
59  
60 # Shared execution wrapper: calls the subclass's _extract(), and on
61 # failure applies the standard auto_error behaviour (set $c->status(...)
62 # to a value >= 400 and return a body hash), which is the actual failure
63 # contract PAGI::FastAPI's dispatcher checks for.
64 ▶sub _run ($self, $c) {
Subroutine _run: called CC=4 SCAR=13.9
65 22 my $value = $self->_extract($c);
66 22 ▶ return $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 7 15 22
67  
68 15 ▶ return undef unless $self->{auto_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auto_error"} 2 13 15
69  
70 13 my ($status, $detail, @headers) = $self->_failure;
71 13 $c->status($status);
72 13 $c->set_header(@$_) for @headers;
73 13 return { detail => $detail };
74 }
75  
76 # Default failure: 403 Forbidden, no challenge header. Bearer/Basic/OAuth2
77 # override this to send a 401 + WWW-Authenticate challenge instead, per
78 # RFC 7235 / RFC 6750.
79 ▶sub _failure ($self) {
Subroutine _failure: called CC=1 SCAR=0.0
80 3 return (403, 'Not authenticated');
81 }
82  
83 =head1 AUTHOR
84  
85 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
86  
87 =head1 REPOSITORY
88  
89 L<https://github.com/manwar/PAGI-FastAPI-Security>
90  
91 =head1 BUGS
92  
93 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
94 I will be notified and then you'll automatically be notified of progress on your
95 bug as I make changes.
96  
97 =head1 SUPPORT
98  
99 You can find documentation for this module with the perldoc command.
100  
101 perldoc PAGI::FastAPI::Security::Base
102  
103 You can also look for information at:
104  
105 =over 4
106  
107 =item * BUG Report
108  
109 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
110  
111 =item * CPAN Ratings
112  
113 L<http://cpanratings.perl.org/d/PAGI-FastAPI-Security>
114  
115 =item * Search MetaCPAN
116  
117 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
118  
119 =back
120  
121 =head1 LICENSE AND COPYRIGHT
122  
123 Copyright (C) 2026 Mohammad Sajid Anwar.
124  
125 This program is free software; you can redistribute it and/or modify it under
126 the terms of the Artistic License (2.0). You may obtain a copy of the full
127 license at:
128  
129 L<http://www.perlfoundation.org/artistic_license_2_0>
130  
131 =cut
132  
133 5 1; # End of PAGI::FastAPI::Security::Base
« Security/OAuth2/PasswordBearer.pm ↑ Summary Security/HTTPBearer.pm »