| 1 | package PAGI::FastAPI::Security::HTTPBasic; | ||||||||||||||||||||||||||||
| 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 MIME::Base64 qw(decode_base64); | ||||||||||||||||||||||||||
|
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::HTTPBasic - HTTP Basic authentication scheme for PAGI::FastAPI | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Version v0.0.6 | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | use PAGI::FastAPI::Security::HTTPBasic; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $basic = PAGI::FastAPI::Security::HTTPBasic->new(realm => 'my-api'); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $app->get('/admin', | ||||||||||||||||||||||||||||
| 28 | dependencies => [ $basic->depends(key => 'creds') ], | ||||||||||||||||||||||||||||
| 29 | handler => async sub ($c) { | ||||||||||||||||||||||||||||
| 30 | my ($user, $pass) = @{ $c->stash->{creds} }{qw(username password)}; | ||||||||||||||||||||||||||||
| 31 | # ... verify $user/$pass yourself ... | ||||||||||||||||||||||||||||
| 32 | return { ok => 1 }; | ||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Grabs C<username>/C<password> information from an C<Authorization: Basic E<lt>base64(user:pass)E<gt>> | ||||||||||||||||||||||||||||
| 39 | request header. This class just extracts the information, verification of the | ||||||||||||||||||||||||||||
| 40 | information is the user’s responsibility (user should verify the information | ||||||||||||||||||||||||||||
| 41 | against the database comparing it to password hashed with some hashing algorithm, | ||||||||||||||||||||||||||||
| 42 | an example could be L<Crypt::Bcrypt>/L<Crypt::Argon2>. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Upon success, a dependency resolves to a HashRef: | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | C<< { username => $user, password => $pass } >>. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 CONSTRUCTOR OPTIONS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item * C<realm> - (Optional) Realm string sent in the C<WWW-Authenticate> | ||||||||||||||||||||||||||||
| 53 | challenge header on failure. Default: C<'Restricted'>. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item * C<auto_error> - (Optional) See L<PAGI::FastAPI::Security::Base>. | ||||||||||||||||||||||||||||
| 56 | Default: true. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =back | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | sub new ($class, %opts) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | $opts{realm} //= 'Restricted'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"realm"} //= "Restricted"
MC/DC0%0/1
$opts{"realm"} //= "Restricted"
$opts{"realm"}
Truth table50%1/2
A
A= $opts{"realm"}
| |||||||||||||||||||||||||||||
| 64 | 1 | return $class->SUPER::new(%opts); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub _extract ($self, $c) { | ||||||||||||||||||||||||||
|
Subroutine
_extract: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | return undef unless $auth =~ /^Basic\s+(\S+)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | my $decoded = eval { decode_base64($1) }; | |||||||||||||||||||||||||||
| 72 | 3 | ▶ | return undef unless defined $decoded && $decoded =~ /^([^:]*):(.*)$/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $decoded and $decoded =~ /^([^:]*):(.*)$/su
MC/DC50%1/2
defined $decoded and $decoded =~ /^([^:]*):(.*)$/su
defined $decoded
$decoded =~ /^([^:]*):(.*)$/su
Truth table66%2/3
A and B
A= defined $decodedB= $decoded =~ /^([^:]*):(.*)$/su
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | return { username => $1, password => $2 }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | sub _failure ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_failure: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 4 | my $realm = $self->{realm}; | |||||||||||||||||||||||||||
| 79 | 4 | $realm =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 80 | 4 | return (401, 'Not authenticated', ['WWW-Authenticate' => qq{Basic realm="$realm"}]); | |||||||||||||||||||||||||||
| 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::HTTPBasic | ||||||||||||||||||||||||||||
| 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 * Search MetaCPAN | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | L<https://metacpan.org/dist/PAGI-FastAPI-Security/> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =back | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 122 | the terms of the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||||||||||
| 123 | license at: | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | 1; # End of PAGI::FastAPI::Security::HTTPBasic | |||||||||||||||||||||||||||