blib/lib/PAGI/Middleware/Auth/Bearer.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 214 CC 26 SCAR 42.6
CC
26
Cov
0%
_validate_jwt
182.0
wrap
42.0
_requires_auth
30.0
CRAP 70.8
1 package PAGI::Middleware::Auth::Bearer;
2 $PAGI::Middleware::Auth::Bearer::VERSION = '0.002000';
3 use strict;
4 use warnings;
5 use parent 'PAGI::Middleware';
6 use Future::AsyncAwait;
7 use JSON::MaybeXS ();
8 use MIME::Base64 qw(decode_base64url);
9 use Digest::SHA qw(hmac_sha256);
10  
11 =head1 NAME
12  
13 PAGI::Middleware::Auth::Bearer - Bearer token authentication middleware
14  
15 =head1 SYNOPSIS
16  
17 use PAGI::Middleware::Builder;
18  
19 my $app = builder {
20 enable 'Auth::Bearer',
21 secret => 'your-jwt-secret',
22 algorithms => ['HS256'];
23 $my_app;
24 };
25  
26 # In your app:
27 async sub app {
28 my ($scope, $receive, $send) = @_;
29  
30 my $auth = $scope->{'pagi.auth'};
31 my $user_id = $auth->{claims}{sub};
32 }
33  
34 =head1 DESCRIPTION
35  
36 PAGI::Middleware::Auth::Bearer validates Bearer tokens in the Authorization
37 header. It supports JWT (JSON Web Tokens) with HMAC-SHA256 signatures.
38  
39 =head1 CONFIGURATION
40  
41 =over 4
42  
43 =item * secret (required for JWT)
44  
45 Secret key for JWT signature verification.
46  
47 =item * algorithms (default: ['HS256'])
48  
49 Allowed JWT algorithms.
50  
51 =item * validator (optional)
52  
53 Custom token validator coderef. Receives ($token) and returns claims hashref or undef.
54 If provided, bypasses built-in JWT validation.
55  
56 =item * realm (default: 'Bearer')
57  
58 The authentication realm for WWW-Authenticate header.
59  
60 =item * paths (optional)
61  
62 Arrayref of path patterns to protect.
63  
64 =back
65  
66 =cut
67  
68 sub _init {
69 my ($self, $config) = @_;
70  
71 $self->{secret} = $config->{secret};
72 $self->{algorithms} = $config->{algorithms} // ['HS256'];
73 $self->{validator} = $config->{validator};
74 $self->{realm} = $config->{realm} // 'Bearer';
75 $self->{paths} = $config->{paths};
76  
77 die "Auth::Bearer requires 'secret' or 'validator' option"
78 unless $self->{secret} || $self->{validator};
79 }
80  
81 sub wrap {
82 my ($self, $app) = @_;
83  
84 return async sub {
85 my ($scope, $receive, $send) = @_;
86 if ($scope->{type} ne 'http') {
87 await $app->($scope, $receive, $send);
88 return;
89 }
90  
91 # Check if path requires authentication
92 unless ($self->_requires_auth($scope->{path})) {
93 await $app->($scope, $receive, $send);
94 return;
95 }
96  
97 # Get Authorization header
98 my $auth_header = $self->_get_header($scope, 'authorization');
99  
100 unless ($auth_header) {
101 await $self->_send_unauthorized($send, 'Token required');
102 return;
103 }
104  
105 # Parse Bearer token
106 my $token = $self->_parse_bearer_token($auth_header);
107  
108 unless ($token) {
109 await $self->_send_unauthorized($send, 'Invalid authorization header');
110 return;
111 }
112  
113 # Validate token
114 my $claims = $self->_validate_token($token);
115  
116 unless ($claims) {
117 await $self->_send_unauthorized($send, 'Invalid token');
118 return;
119 }
120  
121 # Add auth info to scope
122 my $new_scope = {
123 %$scope,
124 'pagi.auth' => {
125 type => 'bearer',
126 token => $token,
127 claims => $claims,
128 },
129 };
130  
131 await $app->($new_scope, $receive, $send);
132 };
133 }
134  
135 sub _requires_auth {
136 my ($self, $path) = @_;
137  
138 return 1 unless $self->{paths};
139  
140 for my $pattern (@{$self->{paths}}) {
141 if (ref $pattern eq 'Regexp') {
142 return 1 if $path =~ $pattern;
143 } else {
144 return 1 if index($path, $pattern) == 0;
145 }
146 }
147 return 0;
148 }
149  
150 sub _parse_bearer_token {
151 my ($self, $header) = @_;
152  
153 return unless $header =~ /^Bearer\s+(.+)$/i;
154 return $1;
155 }
156  
157 sub _validate_token {
158 my ($self, $token) = @_;
159  
160 # Use custom validator if provided
161 if ($self->{validator}) {
162 return $self->{validator}->($token);
163 }
164  
165 # Validate as JWT
166 return $self->_validate_jwt($token);
167 }
168  
169 sub _validate_jwt {
170 my ($self, $token) = @_;
171  
172 my @parts = split /\./, $token;
173 return unless @parts == 3;
174  
175 my ($header_b64, $payload_b64, $signature_b64) = @parts;
176  
177 # Decode and parse header
178 my $header_json = eval { decode_base64url($header_b64) };
179 return unless $header_json;
180 my $header = eval { JSON::MaybeXS::decode_json($header_json) };
181 return unless $header;
182  
183 # Check algorithm
184 my $alg = $header->{alg} // '';
185 return unless grep { $_ eq $alg } @{$self->{algorithms}};
186  
187 # Verify signature
188 my $signature_input = "$header_b64.$payload_b64";
189 my $expected_signature;
190  
191 if ($alg eq 'HS256') {
192 $expected_signature = $self->_base64url_encode(
193 hmac_sha256($signature_input, $self->{secret})
194 );
195 } else {
196 return; # Unsupported algorithm
197 }
198  
199 return unless $self->_secure_compare($signature_b64, $expected_signature);
200  
201 # Decode payload
202 my $payload_json = eval { decode_base64url($payload_b64) };
203 return unless $payload_json;
204 my $claims = eval { JSON::MaybeXS::decode_json($payload_json) };
205 return unless $claims;
206  
207 # Check expiration
208 if (exists $claims->{exp}) {
209 return if time() > $claims->{exp};
210 }
211  
212 # Check not-before
213 if (exists $claims->{nbf}) {
214 return if time() < $claims->{nbf};
215 }
216  
217 return $claims;
218 }
219  
220 sub _base64url_encode {
221 my ($self, $data) = @_;
222  
223 my $encoded = MIME::Base64::encode_base64($data, '');
224 $encoded =~ tr{+/}{-_};
225 $encoded =~ s/=+$//;
226 return $encoded;
227 }
228  
229 sub _secure_compare {
230 my ($self, $a, $b) = @_;
231  
232 return 0 unless length($a) == length($b);
233 my $result = 0;
234 for my $i (0 .. length($a) - 1) {
235 $result |= ord(substr($a, $i, 1)) ^ ord(substr($b, $i, 1));
236 }
237 return $result == 0;
238 }
239  
240 async sub _send_unauthorized {
241 my ($self, $send, $error) = @_;
242  
243 my $body = $error;
244  
245 await $send->({
246 type => 'http.response.start',
247 status => 401,
248 headers => [
249 ['Content-Type', 'text/plain'],
250 ['Content-Length', length($body)],
251 ['WWW-Authenticate', qq{Bearer realm="$self->{realm}"}],
252 ],
253 });
254 await $send->({
255 type => 'http.response.body',
256 body => $body,
257 more => 0,
258 });
259 }
260  
261 sub _get_header {
262 my ($self, $scope, $name) = @_;
263  
264 $name = lc($name);
265 for my $h (@{$scope->{headers} // []}) {
266 return $h->[1] if lc($h->[0]) eq $name;
267 }
268 return;
269 }
270  
271 1;
272  
273 __END__