|
Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Webauthn; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
use Mojo::Base 'Mojolicious::Controller'; |
| 20 |
use Try::Tiny; |
| 21 |
use C4::Context; |
| 22 |
use Mojo::URL; |
| 23 |
use Koha::Patrons; |
| 24 |
use Koha::WebauthnCredential; |
| 25 |
use Koha::WebauthnCredentials; |
| 26 |
use Authen::WebAuthn; |
| 27 |
use MIME::Base64 qw(encode_base64 decode_base64); |
| 28 |
use JSON qw(decode_json encode_json); |
| 29 |
use DateTime; |
| 30 |
use Fcntl qw(O_RDONLY); |
| 31 |
|
| 32 |
=head1 NAME |
| 33 |
|
| 34 |
Koha::REST::V1::Webauthn |
| 35 |
|
| 36 |
=head1 API |
| 37 |
|
| 38 |
=head2 Methods |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
sub _resolve_patron { |
| 43 |
my ($c) = @_; |
| 44 |
my $body = $c->req->json; |
| 45 |
my $patron_id = $body->{patron_id}; |
| 46 |
my $userid = $body->{userid}; |
| 47 |
my $patron; |
| 48 |
if ($patron_id) { |
| 49 |
$patron = Koha::Patrons->find($patron_id); |
| 50 |
} elsif ($userid) { |
| 51 |
$patron = Koha::Patrons->find( { userid => $userid } ); |
| 52 |
} |
| 53 |
return $patron; |
| 54 |
} |
| 55 |
|
| 56 |
sub _webauthn_origin_and_rp_id { |
| 57 |
my ($c) = @_; |
| 58 |
my $pref_base = C4::Context->preference('StaffClientBaseURL'); |
| 59 |
my $req_url = $c->req->url->to_abs; |
| 60 |
|
| 61 |
# Respect reverse proxy headers if present |
| 62 |
my $headers = $c->req->headers; |
| 63 |
my $xf_proto = $headers->header('X-Forwarded-Proto'); |
| 64 |
my $xf_host_header = $headers->header('X-Forwarded-Host'); |
| 65 |
my $xf_port_header = $headers->header('X-Forwarded-Port'); |
| 66 |
|
| 67 |
# Effective request values |
| 68 |
my $req_scheme = $req_url->scheme // 'https'; |
| 69 |
if ( defined $xf_proto && length $xf_proto ) { |
| 70 |
$req_scheme = lc $xf_proto; |
| 71 |
} |
| 72 |
|
| 73 |
my $req_host = $req_url->host; |
| 74 |
my $req_port = $req_url->port; |
| 75 |
if ( defined $xf_host_header && length $xf_host_header ) { |
| 76 |
my ($first_host) = split /\s*,\s*/, $xf_host_header, 2; |
| 77 |
if ( $first_host ) { |
| 78 |
if ( $first_host =~ /^(.*?):(\d+)$/ ) { |
| 79 |
$req_host = $1; |
| 80 |
$req_port = $2; |
| 81 |
} else { |
| 82 |
$req_host = $first_host; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
if ( defined $xf_port_header && $xf_port_header =~ /^(\d+)$/ ) { |
| 87 |
$req_port = $1; |
| 88 |
} |
| 89 |
|
| 90 |
my ( $scheme, $host, $port ) = ( $req_scheme, $req_host, $req_port ); |
| 91 |
|
| 92 |
if ($pref_base) { |
| 93 |
my $pref = Mojo::URL->new($pref_base); |
| 94 |
my $pref_scheme = $pref->scheme // 'https'; |
| 95 |
my $pref_host = $pref->host; |
| 96 |
my $pref_port = $pref->port; |
| 97 |
|
| 98 |
# Always use the configured host for rp_id (domain requirement) |
| 99 |
$host = $pref_host if $pref_host; |
| 100 |
|
| 101 |
# If a password manager or proxy upgraded the scheme to https but host matches, |
| 102 |
# prefer the effective request scheme/port to avoid origin mismatches. |
| 103 |
if ( defined $req_scheme && defined $pref_scheme && $req_host && $pref_host && $req_host eq $pref_host ) { |
| 104 |
if ( $req_scheme ne $pref_scheme ) { |
| 105 |
# Only allow HTTPS upgrades (http -> https). Prevent downgrades. |
| 106 |
if ( $pref_scheme eq 'http' && $req_scheme eq 'https' ) { |
| 107 |
$scheme = 'https'; |
| 108 |
$port = $req_port; # honor request port on upgrade |
| 109 |
} else { |
| 110 |
# Keep configured secure scheme when request is http but pref is https |
| 111 |
$scheme = $pref_scheme; |
| 112 |
$port = defined $pref_port ? $pref_port : $req_port; |
| 113 |
} |
| 114 |
} else { |
| 115 |
# same scheme; prefer explicit configured port, otherwise request port |
| 116 |
$scheme = $pref_scheme; |
| 117 |
$port = defined $pref_port ? $pref_port : $req_port; |
| 118 |
} |
| 119 |
} else { |
| 120 |
# Different host or no request scheme: stick to configured scheme/port |
| 121 |
$scheme = $pref_scheme; |
| 122 |
$port = $pref_port; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
my $origin = $scheme . '://' . $host . ( $port && $port !~ /^(80|443)$/ ? ':' . $port : '' ); |
| 127 |
my $rp_id = $host; # rp_id must be a registrable domain, no scheme/port |
| 128 |
return ( $origin, $rp_id ); |
| 129 |
} |
| 130 |
|
| 131 |
sub _maybe_upgrade_origin_to_client { |
| 132 |
my ( $origin, $rp_id, $client_origin ) = @_; |
| 133 |
return $origin unless $client_origin; |
| 134 |
my $co = Mojo::URL->new($client_origin); |
| 135 |
my $o = Mojo::URL->new($origin); |
| 136 |
# Allow only http -> https upgrade for the same host (rp_id) |
| 137 |
if ( lc( $co->host // '' ) eq lc($rp_id) && ( $o->scheme // '' ) eq 'http' && ( $co->scheme // '' ) eq 'https' ) { |
| 138 |
my $port = $co->port; |
| 139 |
my $new = 'https://' . $rp_id . ( $port && $port !~ /^(80|443)$/ ? ':' . $port : '' ); |
| 140 |
return $new; |
| 141 |
} |
| 142 |
return $origin; |
| 143 |
} |
| 144 |
|
| 145 |
sub _generate_random_bytes { |
| 146 |
my ($length) = @_; |
| 147 |
$length ||= 32; |
| 148 |
my $bytes = ''; |
| 149 |
if ( sysopen( my $fh, '/dev/urandom', O_RDONLY ) ) { |
| 150 |
read( $fh, $bytes, $length ); |
| 151 |
close $fh; |
| 152 |
} |
| 153 |
return $bytes; |
| 154 |
} |
| 155 |
|
| 156 |
sub _to_base64url { |
| 157 |
my ($bytes) = @_; |
| 158 |
my $b64 = encode_base64( $bytes, '' ); |
| 159 |
$b64 =~ tr{+/}{-_}; |
| 160 |
$b64 =~ s/=+$//; |
| 161 |
return $b64; |
| 162 |
} |
| 163 |
|
| 164 |
sub _std_b64_to_b64url { |
| 165 |
my ($b64) = @_; |
| 166 |
$b64 =~ tr{+/}{-_}; |
| 167 |
$b64 =~ s/=+$//; |
| 168 |
return $b64; |
| 169 |
} |
| 170 |
|
| 171 |
sub _b64url_to_std { |
| 172 |
my ($b64u) = @_; |
| 173 |
return unless defined $b64u; |
| 174 |
my $b64 = $b64u; |
| 175 |
$b64 =~ tr{-_}{+/}; |
| 176 |
my $pad = ( 4 - ( length($b64) % 4 ) ) % 4; |
| 177 |
$b64 .= '=' x $pad; |
| 178 |
return $b64; |
| 179 |
} |
| 180 |
|
| 181 |
sub register_challenge { |
| 182 |
my $c = shift->openapi->valid_input or return; |
| 183 |
return try { |
| 184 |
my $patron = _resolve_patron($c); |
| 185 |
return $c->render( status => 404, openapi => { error => 'Patron not found' } ) unless $patron; |
| 186 |
my $patron_id = $patron->borrowernumber; |
| 187 |
|
| 188 |
my ( $origin, $rp_id ) = _webauthn_origin_and_rp_id($c); |
| 189 |
my $challenge_bytes = _generate_random_bytes(32); |
| 190 |
my $challenge_b64u = _to_base64url($challenge_bytes); |
| 191 |
|
| 192 |
$c->session( webauthn_challenge => $challenge_b64u ); |
| 193 |
$c->session( webauthn_patron_id => $patron_id ); |
| 194 |
|
| 195 |
$c->render( |
| 196 |
openapi => { |
| 197 |
challenge => $challenge_b64u, |
| 198 |
user => { |
| 199 |
id => $patron_id, |
| 200 |
name => $patron->userid, |
| 201 |
}, |
| 202 |
} |
| 203 |
); |
| 204 |
} catch { |
| 205 |
$c->unhandled_exception($_); |
| 206 |
}; |
| 207 |
} |
| 208 |
|
| 209 |
sub register { |
| 210 |
my $c = shift->openapi->valid_input or return; |
| 211 |
return try { |
| 212 |
my $body = $c->req->json; |
| 213 |
my $patron; |
| 214 |
my $patron_id; |
| 215 |
if ( $body->{patron_id} ) { |
| 216 |
$patron = Koha::Patrons->find( $body->{patron_id} ); |
| 217 |
$patron_id = $body->{patron_id}; |
| 218 |
} elsif ( $body->{userid} ) { |
| 219 |
$patron = Koha::Patrons->find( { userid => $body->{userid} } ); |
| 220 |
$patron_id = $patron ? $patron->borrowernumber : undef; |
| 221 |
} else { |
| 222 |
return $c->render( status => 400, openapi => { error => 'Missing patron_id or userid in request' } ); |
| 223 |
} |
| 224 |
my $att = $body->{attestation_response} // {}; |
| 225 |
return $c->render( status => 400, openapi => { error => 'Missing attestation_response' } ) unless %$att; |
| 226 |
|
| 227 |
my ( $origin, $rp_id ) = _webauthn_origin_and_rp_id($c); |
| 228 |
my $client_origin = eval { |
| 229 |
my $cdj_json = decode_base64( $att->{client_data_json} // '' ); |
| 230 |
my $cdj = decode_json($cdj_json); |
| 231 |
$cdj->{origin}; |
| 232 |
}; |
| 233 |
$origin = _maybe_upgrade_origin_to_client( $origin, $rp_id, $client_origin ); |
| 234 |
my $webauthn = Authen::WebAuthn->new( rp_id => $rp_id, origin => $origin ); |
| 235 |
my $challenge_b64u = $c->session('webauthn_challenge'); |
| 236 |
|
| 237 |
my $res = eval { |
| 238 |
$webauthn->validate_registration( |
| 239 |
challenge_b64 => $challenge_b64u, |
| 240 |
requested_uv => 'preferred', |
| 241 |
client_data_json_b64 => _std_b64_to_b64url( $att->{client_data_json} ), |
| 242 |
attestation_object_b64 => _std_b64_to_b64url( $att->{attestation_object} ), |
| 243 |
); |
| 244 |
}; |
| 245 |
if ( $@ || !$res ) { |
| 246 |
$c->app->log->warn( 'webauthn register failed: ' . ( $@ // 'undef' ) ); |
| 247 |
return $c->render( status => 401, openapi => { error => 'Attestation verification failed' } ); |
| 248 |
} |
| 249 |
|
| 250 |
Koha::WebauthnCredential->new( |
| 251 |
{ |
| 252 |
borrowernumber => $patron_id, |
| 253 |
credential_id => MIME::Base64::decode_base64url( $res->{credential_id} ), |
| 254 |
public_key => MIME::Base64::decode_base64url( $res->{credential_pubkey} ), |
| 255 |
sign_count => $res->{signature_count} // 0, |
| 256 |
transports => undef, |
| 257 |
created_on => DateTime->now->strftime('%F %T'), |
| 258 |
} |
| 259 |
)->store; |
| 260 |
|
| 261 |
$c->render( status => 201, openapi => { success => 1 } ); |
| 262 |
} catch { |
| 263 |
$c->unhandled_exception($_); |
| 264 |
}; |
| 265 |
} |
| 266 |
|
| 267 |
sub authenticate_challenge { |
| 268 |
my $c = shift->openapi->valid_input or return; |
| 269 |
return try { |
| 270 |
$c->app->log->debug( 'authenticate_challenge: incoming body: ' . encode_json( $c->req->json // {} ) ); |
| 271 |
my $patron = _resolve_patron($c); |
| 272 |
$c->app->log->debug( 'authenticate_challenge: resolved patron: ' |
| 273 |
. ( defined $patron ? $patron->borrowernumber . ' (' . ( $patron->userid // '' ) . ')' : 'undef' ) ); |
| 274 |
return $c->render( status => 404, openapi => { error => 'Patron not found' } ) unless $patron; |
| 275 |
my $patron_id = $patron->borrowernumber; |
| 276 |
$c->app->log->debug("authenticate_challenge: patron_id: $patron_id"); |
| 277 |
|
| 278 |
my $credentials = Koha::WebauthnCredentials->search( { borrowernumber => $patron_id } ); |
| 279 |
$c->app->log->debug( 'authenticate_challenge: credentials found: ' . $credentials->count ); |
| 280 |
return $c->render( status => 404, openapi => { error => 'No credentials registered' } ) |
| 281 |
unless $credentials->count; |
| 282 |
|
| 283 |
my ( $origin, $rp_id ) = _webauthn_origin_and_rp_id($c); |
| 284 |
my $challenge_bytes = _generate_random_bytes(32); |
| 285 |
my $challenge_b64u = _to_base64url($challenge_bytes); |
| 286 |
$c->app->log->debug( 'authenticate_challenge: generated challenge: ' . $challenge_b64u ); |
| 287 |
$c->session( webauthn_challenge => $challenge_b64u ); |
| 288 |
$c->session( webauthn_patron_id => $patron_id ); |
| 289 |
|
| 290 |
my @allow_credentials; |
| 291 |
while ( my $c = $credentials->next ) { |
| 292 |
push @allow_credentials, |
| 293 |
{ id => _std_b64_to_b64url( encode_base64( $c->credential_id, '' ) ), type => 'public-key' }; |
| 294 |
} |
| 295 |
$c->app->log->debug( 'authenticate_challenge: allow_credentials count: ' . scalar @allow_credentials ); |
| 296 |
|
| 297 |
$c->render( |
| 298 |
openapi => { |
| 299 |
challenge => $challenge_b64u, |
| 300 |
allowCredentials => \@allow_credentials, |
| 301 |
} |
| 302 |
); |
| 303 |
} catch { |
| 304 |
$c->unhandled_exception($_); |
| 305 |
}; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
sub authenticate { |
| 310 |
my $c = shift->openapi->valid_input or return; |
| 311 |
return try { |
| 312 |
my $body = $c->req->json; |
| 313 |
my $patron; |
| 314 |
my $patron_id; |
| 315 |
if ( $body->{patron_id} ) { |
| 316 |
$patron = Koha::Patrons->find( $body->{patron_id} ); |
| 317 |
$patron_id = $body->{patron_id}; |
| 318 |
} elsif ( $body->{userid} ) { |
| 319 |
$patron = Koha::Patrons->find( { userid => $body->{userid} } ); |
| 320 |
$patron_id = $patron ? $patron->borrowernumber : undef; |
| 321 |
} else { |
| 322 |
return $c->render( status => 400, openapi => { error => 'Missing patron_id or userid in request' } ); |
| 323 |
} |
| 324 |
my $assertion = $body->{assertion_response}; |
| 325 |
return $c->render( status => 400, openapi => { error => 'Missing assertion_response' } ) unless $assertion; |
| 326 |
|
| 327 |
my $credentials = Koha::WebauthnCredentials->search( { borrowernumber => $patron_id } ); |
| 328 |
my ( %cred_id_map, %pubkeys ); |
| 329 |
{ |
| 330 |
my $it = $credentials->search( {}, { columns => [qw/id credential_id public_key/] } ); |
| 331 |
while ( my $c = $it->next ) { |
| 332 |
my $id_b64u = _std_b64_to_b64url( encode_base64( $c->credential_id, '' ) ); |
| 333 |
$cred_id_map{$id_b64u} = $c->id; |
| 334 |
$pubkeys{$id_b64u} = _std_b64_to_b64url( encode_base64( $c->public_key, '' ) ); |
| 335 |
} |
| 336 |
} |
| 337 |
my $challenge_b64u = $c->session('webauthn_challenge'); |
| 338 |
my ( $origin, $rp_id ) = _webauthn_origin_and_rp_id($c); |
| 339 |
my $webauthn = Authen::WebAuthn->new( rp_id => $rp_id, origin => $origin ); |
| 340 |
|
| 341 |
# Browser sends base64 (we need base64url) |
| 342 |
my $client_data_b64u = |
| 343 |
_std_b64_to_b64url( $assertion->{clientDataJSON} // $assertion->{client_data_json} // '' ); |
| 344 |
my $client_origin = eval { |
| 345 |
my $cdj_b64 = $assertion->{clientDataJSON} // $assertion->{client_data_json} // ''; |
| 346 |
my $cdj_json = decode_base64($cdj_b64); |
| 347 |
my $cdj = decode_json($cdj_json); |
| 348 |
$cdj->{origin}; |
| 349 |
}; |
| 350 |
$origin = _maybe_upgrade_origin_to_client( $origin, $rp_id, $client_origin ); |
| 351 |
$webauthn = Authen::WebAuthn->new( rp_id => $rp_id, origin => $origin ); |
| 352 |
my $auth_data_b64u = |
| 353 |
_std_b64_to_b64url( $assertion->{authenticatorData} // $assertion->{authenticator_data} // '' ); |
| 354 |
my $signature_b64u = _std_b64_to_b64url( $assertion->{signature} // '' ); |
| 355 |
my $credential_id_b64u = _std_b64_to_b64url( $assertion->{id} // $assertion->{raw_id} // '' ); |
| 356 |
|
| 357 |
# pubkeys already built above |
| 358 |
|
| 359 |
my $stored_sign_count = 0; # optional enforcement; we track after success |
| 360 |
my $res = eval { |
| 361 |
$webauthn->validate_assertion( |
| 362 |
challenge_b64 => $challenge_b64u, |
| 363 |
credential_pubkey_b64 => $pubkeys{$credential_id_b64u}, |
| 364 |
stored_sign_count => $stored_sign_count, |
| 365 |
requested_uv => 'preferred', |
| 366 |
client_data_json_b64 => $client_data_b64u, |
| 367 |
authenticator_data_b64 => $auth_data_b64u, |
| 368 |
signature_b64 => $signature_b64u, |
| 369 |
); |
| 370 |
}; |
| 371 |
if ( $@ || !$res ) { |
| 372 |
$c->app->log->warn( 'webauthn authenticate failed: ' . ( $@ // 'undef' ) ); |
| 373 |
return $c->render( status => 401, openapi => { error => 'Authentication failed' } ); |
| 374 |
} |
| 375 |
if ( my $cred_pk = $cred_id_map{$credential_id_b64u} ) { |
| 376 |
if ( my $cred = Koha::WebauthnCredentials->find($cred_pk) ) { |
| 377 |
$cred->set( |
| 378 |
{ sign_count => $res->{signature_count} // 0, last_used => DateTime->now->strftime('%F %T') } ) |
| 379 |
->store; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
# Issue staff session and cookie for login |
| 384 |
my $patron_obj = Koha::Patrons->find($patron_id); |
| 385 |
my $session = C4::Auth::create_basic_session( { patron => $patron_obj, interface => 'intranet' } ); |
| 386 |
my ( $computed_origin ) = _webauthn_origin_and_rp_id($c); |
| 387 |
my $is_https = ( $computed_origin =~ m{^https://}i ) ? 1 : 0; |
| 388 |
$c->cookie( CGISESSID => $session->id, { path => '/', http_only => 1, same_site => 'Lax', secure => $is_https } ); |
| 389 |
C4::Context->interface('intranet'); |
| 390 |
my $lib = $patron_obj->library; |
| 391 |
C4::Context->set_userenv( |
| 392 |
$patron_obj->borrowernumber, |
| 393 |
$patron_obj->userid // '', |
| 394 |
$patron_obj->cardnumber // '', |
| 395 |
$patron_obj->firstname // '', |
| 396 |
$patron_obj->surname // '', |
| 397 |
( $lib ? $lib->branchcode : '' ), |
| 398 |
( $lib ? $lib->branchname : '' ), |
| 399 |
$patron_obj->flags, |
| 400 |
$patron_obj->email // '', |
| 401 |
); |
| 402 |
$c->render( status => 200, openapi => { success => 1 } ); |
| 403 |
} catch { |
| 404 |
$c->unhandled_exception($_); |
| 405 |
}; |
| 406 |
} |
| 407 |
|
| 408 |
1; |