Security/HTTPBearer.pm

filter: Statementstmt 65.5% 19 / 29 Branchbran 33.3% 2 / 6 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 85.7% 6 / 7 Podpod 50.0% 1 / 2 totaltotal 64.0% 32 / 50 CC 6 SCAR 14.9
CC
6
Cov
62%
challenge_header
12.0
_extract
3.0
new
2.1
CRAP 4.4
« Security/APIKey.pm ↑ Summary Security/Base.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.6');
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.6
19  
20 =head1 SYNOPSIS
21  
22 use PAGI::FastAPI::Security::HTTPBearer;
23  
24 my $bearer = PAGI::FastAPI::Security::HTTPBearer->new(realm => 'my-api');
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<realm> - (Optional) Realm string sent in the C<WWW-Authenticate>
48 challenge header on failure. Default: C<'Restricted'>.
49  
50 =item * C<auto_error> - (Optional) If true (this is the default value), it
51 automatically issues a C<401 Unauthorized> response with a C<WWW-Authenticate: Bearer>
52 header and returns C<{ detail => 'Not authenticated' }> when there is no valid
53 bearer token available, which can completely bypass the route handler. If
54 this is set to false, the dependency resolves as C<undef> in case of failure,
55 allowing optional implementation of auth.
56  
57 =back
58  
59 =head1 METHODS
60  
61 =head2 challenge_header
62  
63 my ($key, $value) = $bearer->challenge_header;
64 my ($key, $value) = $bearer->challenge_header(
65 error => 'invalid_token',
66 error_description => 'The access token has expired',
67 );
68  
69 # Usage inside route checks:
70 $c->set_header($bearer->challenge_header(error => 'invalid_token'));
71  
72 Overrides L<PAGI::FastAPI::Security::Base/challenge_header> to generate an RFC 6750
73 compliant C<WWW-Authenticate> challenge header for Bearer authentication schemes.
74  
75 Returns a two-element array C<('WWW-Authenticate', $challenge_string)>.
76  
77 =head3 OPTIONS
78  
79 =over 4
80  
81 =item * C<error> - (Optional) An ASCII error code as specified in RFC 6750 Section 3.1
82 (e.g., C<'invalid_request'>, C<'invalid_token'>, C<'insufficient_scope'>).
83  
84 =item * C<error_description> - (Optional) A human-readable text providing additional
85 information used to assist developer troubleshooting.
86  
87 =back
88  
89 =head3 EXAMPLES
90  
91 Without parameters:
92  
93 'WWW-Authenticate' => 'Bearer realm="Restricted"'
94  
95 With error parameters:
96  
97 'WWW-Authenticate' => 'Bearer realm="Restricted", error="invalid_token", error_description="The access token has expired"'
98  
99 =cut
100  
101 2 ▶sub new ($class, %opts) {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
102 2 ▶ $opts{realm} //= 'Restricted';
Condition50%1/2
$opts{"realm"} //= "Restricted"
l!l
02
MC/DC0%0/1
$opts{"realm"} //= "Restricted"
$opts{"realm"}
Truth table50%1/2
A
A= $opts{"realm"}
Aresult
11
00
103 2 return $class->SUPER::new(%opts);
104 }
105  
106 0 ▶sub challenge_header ($self, %args) {
Subroutine challenge_header: not called CC=3 SCAR=24.8
Pod documented
107 0 my $realm = $self->{realm};
108 0 $realm =~ s/"/\\"/g;
109 0 my $header = qq{Bearer realm="$realm"};
110  
111 0 ▶ if (my $error = $args{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $args{"error"}) 0 0 0
112 0 $error =~ s/"/\\"/g;
113 0 $header .= qq{, error="$error"};
114 }
115 0 ▶ if (my $desc = $args{error_description}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $desc = $args{"error_description"}) 0 0 0
116 0 $desc =~ s/"/\\"/g;
117 0 $header .= qq{, error_description="$desc"};
118 }
119  
120 0 return ('WWW-Authenticate' => $header);
121 }
122  
123 7 ▶sub _extract ($self, $c) {
Subroutine _extract: called CC=3 SCAR=11.0
124 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
125 5 ▶ return undef unless $auth =~ /^Bearer\s+(\S+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth =~ /^Bearer\s+(\S+)$/iu 4 1 5
126 1 return $1;
127 }
128  
129 5 ▶sub _failure ($self) {
Subroutine _failure: called CC=1 SCAR=0.0
130 5 my $realm = $self->{realm};
131 5 $realm =~ s/"/\\"/g;
132 5 return (401, 'Not authenticated', ['WWW-Authenticate' => qq{Bearer realm="$realm"}]);
133 }
134  
135 =head1 AUTHOR
136  
137 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
138  
139 =head1 REPOSITORY
140  
141 L<https://github.com/manwar/PAGI-FastAPI-Security>
142  
143 =head1 BUGS
144  
145 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
146 I will be notified and then you'll automatically be notified of progress on your
147 bug as I make changes.
148  
149 =head1 SUPPORT
150  
151 You can find documentation for this module with the perldoc command.
152  
153 perldoc PAGI::FastAPI::Security::HTTPBearer
154  
155 You can also look for information at:
156  
157 =over 4
158  
159 =item * BUG Report
160  
161 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
162  
163 =item * Search MetaCPAN
164  
165 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
166  
167 =back
168  
169 =head1 LICENSE AND COPYRIGHT
170  
171 Copyright (C) 2026 Mohammad Sajid Anwar.
172  
173 This program is free software; you can redistribute it and/or modify it under
174 the terms of the Artistic License (2.0). You may obtain a copy of the full
175 license at:
176  
177 L<http://www.perlfoundation.org/artistic_license_2_0>
178  
179 =cut
180  
181 2 1; # End of PAGI::FastAPI::Security::HTTPBearer
« Security/APIKey.pm ↑ Summary Security/Base.pm »