Security.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 10 / 10 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Security/OAuth2/PasswordBearer.pm ↑ Summary
1 package PAGI::FastAPI::Security;
2  
3 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = qv('v0.0.6');
7 1 our $AUTHORITY = 'cpan:MANWAR';
8  
9 =encoding utf-8
10  
11 =head1 NAME
12  
13 PAGI::FastAPI::Security - Authentication scheme building blocks for PAGI::FastAPI
14  
15 =head1 VERSION
16  
17 Version v0.0.6
18  
19 =head1 SYNOPSIS
20  
21 use PAGI::FastAPI;
22 use PAGI::FastAPI::Security::HTTPBearer;
23  
24 my $bearer = PAGI::FastAPI::Security::HTTPBearer->new;
25 my $app = PAGI::FastAPI->new(title => 'PAGI::FastAPI app');
26  
27 $app->get('/items',
28 dependencies => [ $bearer->depends(key => 'token') ],
29 handler => async sub ($c) {
30 my $token = $c->stash->{token};
31 # verify $token yourself, however you like
32 return { items => [] };
33 },
34 );
35  
36 =head1 DESCRIPTION
37  
38 The C<PAGI::FastAPI::Security> comprises a compact selection of classes for
39 the authentication scheme offered as part of the L<PAGI::FastAPI::Depends>
40 for the framework of L<PAGI::FastAPI>, taking the inspiration from the
41 C<fastapi.security> module regarding the C<Python FastAPI>.
42  
43 In general, every class used for the authentication scheme has been
44 B<extracting> the credentials from the request. The classes do provide
45 the specification of the error response where necessary (401 + C<WWW-Authenticate>
46 for the challenge-based types and 403 for the API keys). Nevertheless, they
47 do not really verify the credentials, and thus token signature and password/hash
48 verification are left to the user.
49  
50 =head1 AVAILABLE SCHEMES
51  
52 =over 4
53  
54 =item * L<PAGI::FastAPI::Security::HTTPBearer> - C<Authorization: Bearer <token>>
55  
56 =item * L<PAGI::FastAPI::Security::HTTPBasic> - C<Authorization: Basic <base64>>
57  
58 =item * L<PAGI::FastAPI::Security::APIKey> - header, query, or cookie API key
59  
60 =item * L<PAGI::FastAPI::Security::OAuth2::PasswordBearer> - OAuth2 bearer-token extraction + metadata
61  
62 =back
63  
64 =head1 DESIGN NOTES
65  
66 =head2 Why these don't verify tokens
67  
68 Actual token validation is quite diverse: the verification of a JWT signature
69 alone can refer to any of L<Crypt::JWT>, L<Mojo::JWT>, or a JWKS-fetching
70 client, while non-transparent tokens almost always require a database or
71 cache lookup. Putting together any of those is impractical because everyone
72 who would download this package would need to install and configure something,
73 and hence, following the boundaries of C exactly, all of the classes indeed
74 go only halfway through and leave the verification to some other external
75 library or the route handler.
76  
77 $app->get('/items',
78 dependencies => [
79 $bearer->depends(key => 'token'),
80 Depends(async sub ($c) {
81 my $claims = eval { verify_jwt($c->stash->{token}) };
82 unless ($claims) { $c->status(401); return { detail => 'Invalid token' } }
83 return $claims;
84 }, key => 'claims'),
85 ],
86 handler => async sub ($c) { ... $c->stash->{claims} ... },
87 );
88  
89 =head2 Failure Contract
90  
91 These classes indicate any failure in the same manner that the dependency
92 dispatcher of L<PAGI::FastAPI> anticipates by invoking C<< $c->status($code) >>
93 using a code E<gt>= 400 and by producing a body HashRef, delegating the
94 responsibility of the route handler generation.
95  
96 =head1 AUTHOR
97  
98 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
99  
100 =head1 REPOSITORY
101  
102 L<https://github.com/manwar/PAGI-FastAPI-Security>
103  
104 =head1 BUGS
105  
106 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>.
107 I will be notified and then you'll automatically be notified of progress on your
108 bug as I make changes.
109  
110 =head1 SUPPORT
111  
112 You can find documentation for this module with the perldoc command.
113  
114 perldoc PAGI::FastAPI::Security
115  
116 You can also look for information at:
117  
118 =over 4
119  
120 =item * BUG Report
121  
122 L<https://github.com/manwar/PAGI-FastAPI-Security/issues>
123  
124 =item * Search MetaCPAN
125  
126 L<https://metacpan.org/dist/PAGI-FastAPI-Security/>
127  
128 =back
129  
130 =head1 LICENSE AND COPYRIGHT
131  
132 Copyright (C) 2026 Mohammad Sajid Anwar.
133  
134 This program is free software; you can redistribute it and/or modify it under
135 the terms of the Artistic License (2.0). You may obtain a copy of the full
136 license at:
137  
138 L<http://www.perlfoundation.org/artistic_license_2_0>
139  
140 =cut
141  
142 1 1; # End of PAGI::FastAPI::Security
« Security/OAuth2/PasswordBearer.pm ↑ Summary