| 1 | package PAGI::FastAPI::Security::OAuth2::PasswordBearer; | ||||||||||||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent 'PAGI::FastAPI::Security::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = qv('v0.0.6'); | |||||||||||||||||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | PAGI::FastAPI::Security::OAuth2::PasswordBearer - OAuth2 password-bearer scheme for PAGI::FastAPI | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Version v0.0.6 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | use PAGI::FastAPI::Security::OAuth2::PasswordBearer; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $oauth2 = PAGI::FastAPI::Security::OAuth2::PasswordBearer->new( | ||||||||||||||||||||||||||||
| 26 | token_url => '/token', | ||||||||||||||||||||||||||||
| 27 | scopes => { 'items:read' => 'Read items', 'items:write' => 'Write items' }, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | $app->get('/items', | ||||||||||||||||||||||||||||
| 31 | dependencies => [ $oauth2->depends(key => 'token') ], | ||||||||||||||||||||||||||||
| 32 | handler => async sub ($c) { | ||||||||||||||||||||||||||||
| 33 | my $token = $c->stash->{token}; | ||||||||||||||||||||||||||||
| 34 | # ... verify $token yourself (e.g. Crypt::JWT) and check scopes ... | ||||||||||||||||||||||||||||
| 35 | return { items => [] }; | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | One can find similarities in this with what C<OAuth2PasswordBearer> of the | ||||||||||||||||||||||||||||
| 42 | Python FastAPI does. It retrieves the bearer token from the C<Authorization> | ||||||||||||||||||||||||||||
| 43 | header, just like L<PAGI::FastAPI::Security::HTTPBearer> does, and extracts | ||||||||||||||||||||||||||||
| 44 | the OAuth2 C<token_url>/C<scopes> metadata. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The statement B<The specific task of this class does not involve implementing | ||||||||||||||||||||||||||||
| 47 | an OAuth2 token endpoint, authorization code flow, or token verification.> It's | ||||||||||||||||||||||||||||
| 48 | a data extraction process, just like C<fastapi.security.OAuth2PasswordBearer> | ||||||||||||||||||||||||||||
| 49 | does too. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | The actual token issuance and verification are left to you or a dedicated | ||||||||||||||||||||||||||||
| 52 | OAuth2 server module. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 CONSTRUCTOR OPTIONS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =over 4 | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item * C<token_url> - B<Required>. The path clients should POST | ||||||||||||||||||||||||||||
| 59 | credentials to in order to obtain a token (informational metadata only; | ||||||||||||||||||||||||||||
| 60 | not implemented by this class). | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =item * C<scopes> - (Optional) HashRef of C<< scope => description >> | ||||||||||||||||||||||||||||
| 63 | pairs, for future OpenAPI documentation. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item * C<auto_error> - (Optional) See L<PAGI::FastAPI::Security::Base>. | ||||||||||||||||||||||||||||
| 66 | Default: true. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =back | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub new ($class, %opts) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | die "PAGI::FastAPI::Security::OAuth2::PasswordBearer requires a 'token_url' option" | ||||||||||||||||||||||||||||
| 74 | 4 | ▶ | unless defined $opts{token_url} && length $opts{token_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $opts{"token_url"} and length $opts{"token_url"}
MC/DC50%1/2
defined $opts{"token_url"} and length $opts{"token_url"}
defined $opts{"token_url"}
length $opts{"token_url"}
Truth table66%2/3
A and B
A= defined $opts{"token_url"}B= length $opts{"token_url"}
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | $opts{scopes} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"scopes"} //= {}
MC/DC100%1/1
$opts{"scopes"} //= {}
$opts{"scopes"}
Truth table100%2/2
A
A= $opts{"scopes"}
| |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | $opts{realm} //= 'Restricted'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"realm"} //= "Restricted"
MC/DC100%1/1
$opts{"realm"} //= "Restricted"
$opts{"realm"}
Truth table100%2/2
A
A= $opts{"realm"}
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | return $class->SUPER::new(%opts); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 C<token_url()> | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Returns the configured token URL. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub token_url ($self) { $self->{token_url} } | ||||||||||||||||||||||||||
|
Subroutine
token_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 C<scopes()> | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Returns the configured scopes HashRef. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub scopes ($self) { $self->{scopes} } | ||||||||||||||||||||||||||
|
Subroutine
scopes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub _extract ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_extract: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | my $auth = $c->header('Authorization') // return undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$c->header("Authorization") // (return undef)
MC/DC100%1/1
$c->header("Authorization") // (return undef)
$c->header("Authorization")
Truth table100%2/2
A
A= $c->header("Authorization")
| |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | return undef unless $auth =~ /^Bearer\s+(\S+)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | return $1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | sub _failure ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_failure: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 1 | my $realm = $self->{realm}; | |||||||||||||||||||||||||||
| 106 | 1 | $realm =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 107 | 1 | return (401, 'Not authenticated', ['WWW-Authenticate' => qq{Bearer realm="$realm"}]); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | L<https://github.com/manwar/PAGI-FastAPI-Security> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 BUGS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI-Security/issues>. | ||||||||||||||||||||||||||||
| 121 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 122 | bug as I make changes. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | perldoc PAGI::FastAPI::Security::OAuth2::PasswordBearer | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | You can also look for information at: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item * BUG Report | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | L<https://github.com/manwar/PAGI-FastAPI-Security/issues> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | L<https://metacpan.org/dist/PAGI-FastAPI-Security/> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =back | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 149 | the terms of the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||||||||||
| 150 | license at: | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2 | 1; # End of PAGI::FastAPI::Security::OAuth2::PasswordBearer | |||||||||||||||||||||||||||