Security/HTTPBearer.pm

filter: Statementstmt 100% 15 / 15 Branchbran 100% 2 / 2 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 25 / 25 CC 3 SCAR 6.2
CC
3
Cov
100%
_extract
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.9
« Security/Base.pm ↑ Summary Security.pm »
1 package PAGI::FastAPI::Security::HTTPBearer;
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 parent 'PAGI::FastAPI::Security::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = qv('v0.0.4');
8 2 our $AUTHORITY = 'cpan:MANWAR';
9  
10 =encoding utf-8
11  
12 =head1 NAME
13  
14 PAGI::FastAPI::Security::HTTPBearer - HTTP Bearer token scheme for PAGI::FastAPI
15  
16 =head1 VERSION
17  
18 Version v0.0.4
19  
20 =head1 SYNOPSIS
21  
22 use PAGI::FastAPI::Security::HTTPBearer;
23  
24 my $bearer = PAGI::FastAPI::Security::HTTPBearer->new;
25  
26 $app->get('/items',
27 dependencies => [ $bearer->depends(key => 'token') ],
28 handler => async sub ($c) {
29 my $token = $c->stash->{token}; # the raw bearer token string
30 # ... verify $token yourself (JWT, opaque token lookup, etc.) ...
31 return { items => [] };
32 },
33 );
34  
35 =head1 DESCRIPTION
36  
37 Get the bearer token from the C<Authorization: Bearer E<lt>tokenE<gt>>> request
38 header. This class B<does not> verify the token, it only extracts it. You will
39 need to combine it with your verification method (for example, L<Crypt::JWT>
40 for signed JWTs, or a database or cache query for opaque tokens) in your route
41 handler, or you will need to manage verification with another C<Depends()>.
42  
43 =head1 CONSTRUCTOR OPTIONS
44  
45 =over 4
46  
47 =item * C<auto_error> - (Optional) If true (this is the default value), it
48 automatically issues a C<401 Unauthorized> response with a C<WWW-Authenticate: Bearer>
49 header and returns C<{ detail => 'Not authenticated' }> when there is no valid
50 bearer token available, which can completely bypass the route handler. If
51 this is set to false, the dependency resolves as C<undef> in case of failure,
52 allowing optional implementation of auth.
53  
54 =back
55  
56 =cut
57  
58 ▶sub _extract ($self, $c) {
Subroutine _extract: called CC=3 SCAR=11.0
59 7 ▶ my $auth = $c->header('Authorization') // return undef;
Condition100%2/2
$c->header("Authorization") // (return undef)
l!l
52
MC/DC100%1/1
$c->header("Authorization") // (return undef)
$c->header("Authorization")
Truth table100%2/2
A
A= $c->header("Authorization")
Aresult
11
00
60 5 ▶ return undef unless $auth =~ /^Bearer\s+(\S+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth =~ /^Bearer\s+(\S+)$/iu 4 1 5
61 1 return $1;
62 }
63  
64 ▶sub _failure ($self) {
Subroutine _failure: called CC=1 SCAR=0.0
65 5 return (401, 'Not authenticated', ['WWW-Authenticate' => 'Bearer']);
66 }
67  
68 =head1 AUTHOR
69  
70 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
71  
72 =head1 REPOSITORY
73  
74 L<https://github.com/manwar/PAGI-FastAPI-Security>
75  
76 =head1 BUGS
77  
78 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
79 I will be notified and then you'll automatically be notified of progress on your
80 bug as I make changes.
81  
82 =head1 SUPPORT
83  
84 You can find documentation for this module with the perldoc command.
85  
86 perldoc PAGI::FastAPI::Security::HTTPBearer
87  
88 You can also look for information at:
89  
90 =over 4
91  
92 =item * BUG Report
93  
94 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
95  
96 =item * CPAN Ratings
97  
98 L<http://cpanratings.perl.org/d/PAGI-FastAPI-Security>
99  
100 =item * Search MetaCPAN
101  
102 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
103  
104 =back
105  
106 =head1 LICENSE AND COPYRIGHT
107  
108 Copyright (C) 2026 Mohammad Sajid Anwar.
109  
110 This program is free software; you can redistribute it and/or modify it under
111 the terms of the Artistic License (2.0). You may obtain a copy of the full
112 license at:
113  
114 L<http://www.perlfoundation.org/artistic_license_2_0>
115  
116 =cut
117  
118 2 1; # End of PAGI::FastAPI::Security::HTTPBearer
« Security/Base.pm ↑ Summary Security.pm »