|
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 <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
use Mojo::Base 'Mojolicious::Controller'; |
| 20 |
use Try::Tiny; |
| 21 |
use Authen::WebAuthn; |
| 22 |
use C4::Auth qw(create_basic_session); |
| 23 |
use C4::Context; |
| 24 |
use JSON qw(encode_json); |
| 25 |
use Koha::Auth::WebAuthn; |
| 26 |
use Koha::DateUtils qw(dt_from_string); |
| 27 |
use Koha::Exceptions::WebAuthn; |
| 28 |
use Koha::Patrons; |
| 29 |
use Koha::WebauthnCredential; |
| 30 |
use Koha::WebauthnCredentials; |
| 31 |
use MIME::Base64 qw(encode_base64 decode_base64); |
| 32 |
|
| 33 |
=head1 NAME |
| 34 |
|
| 35 |
Koha::REST::V1::Webauthn |
| 36 |
|
| 37 |
=head1 API |
| 38 |
|
| 39 |
=head2 Methods |
| 40 |
|
| 41 |
=head3 register_challenge |
| 42 |
|
| 43 |
Controller for POST /api/v1/webauthn/register/challenge |
| 44 |
|
| 45 |
=cut |
| 46 |
|
| 47 |
sub register_challenge { |
| 48 |
my $c = shift->openapi->valid_input or return; |
| 49 |
return try { |
| 50 |
my $body = $c->req->json; |
| 51 |
my ( $patron, $patron_id ) = Koha::Auth::WebAuthn->resolve_patron($body); |
| 52 |
|
| 53 |
my ( $origin, $rp_id ) = Koha::Auth::WebAuthn->get_origin_and_rp_id($c); |
| 54 |
my $challenge_b64u = Koha::Auth::WebAuthn->generate_challenge(); |
| 55 |
|
| 56 |
Koha::Auth::WebAuthn->store_challenge( $c, $challenge_b64u, $patron_id ); |
| 57 |
|
| 58 |
$c->render( |
| 59 |
openapi => { |
| 60 |
challenge => $challenge_b64u, |
| 61 |
rp_id => $rp_id, |
| 62 |
user => { |
| 63 |
id => $patron_id, |
| 64 |
name => $patron->userid, |
| 65 |
}, |
| 66 |
} |
| 67 |
); |
| 68 |
} catch { |
| 69 |
return _handle_exception( $c, $_ ); |
| 70 |
}; |
| 71 |
} |
| 72 |
|
| 73 |
=head3 register |
| 74 |
|
| 75 |
Controller for POST /api/v1/webauthn/register |
| 76 |
|
| 77 |
=cut |
| 78 |
|
| 79 |
sub register { |
| 80 |
my $c = shift->openapi->valid_input or return; |
| 81 |
return try { |
| 82 |
my $body = $c->req->json; |
| 83 |
my ( $patron, $patron_id ) = Koha::Auth::WebAuthn->resolve_patron($body); |
| 84 |
|
| 85 |
my $att = $body->{attestation_response} // {}; |
| 86 |
return $c->render( status => 400, openapi => { error => 'Missing attestation_response' } ) |
| 87 |
unless $att->{client_data_json} && $att->{attestation_object}; |
| 88 |
|
| 89 |
my ( $origin, $rp_id ) = Koha::Auth::WebAuthn->get_origin_and_rp_id($c); |
| 90 |
|
| 91 |
# Allow http->https upgrade based on client-reported origin |
| 92 |
my $client_origin = Koha::Auth::WebAuthn->extract_client_origin( $att->{client_data_json} ); |
| 93 |
$origin = Koha::Auth::WebAuthn->maybe_upgrade_origin( $origin, $rp_id, $client_origin ); |
| 94 |
|
| 95 |
my $webauthn = Authen::WebAuthn->new( rp_id => $rp_id, origin => $origin ); |
| 96 |
my $challenge_b64u = Koha::Auth::WebAuthn->validate_and_consume_challenge( $c, $patron_id ); |
| 97 |
|
| 98 |
my $res; |
| 99 |
try { |
| 100 |
$res = $webauthn->validate_registration( |
| 101 |
challenge_b64 => $challenge_b64u, |
| 102 |
requested_uv => 'preferred', |
| 103 |
client_data_json_b64 => Koha::Auth::WebAuthn->std_b64_to_b64url( $att->{client_data_json} ), |
| 104 |
attestation_object_b64 => Koha::Auth::WebAuthn->std_b64_to_b64url( $att->{attestation_object} ), |
| 105 |
); |
| 106 |
} catch { |
| 107 |
$c->app->log->warn( 'WebAuthn register failed for patron ' . $patron_id . ': ' . $_ ); |
| 108 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Attestation verification failed' ); |
| 109 |
}; |
| 110 |
unless ($res) { |
| 111 |
$c->app->log->warn( 'WebAuthn register failed for patron ' . $patron_id . ': verification returned false' ); |
| 112 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Attestation verification failed' ); |
| 113 |
} |
| 114 |
|
| 115 |
Koha::WebauthnCredential->new( |
| 116 |
{ |
| 117 |
borrowernumber => $patron_id, |
| 118 |
credential_id => MIME::Base64::decode_base64url( $res->{credential_id} ), |
| 119 |
public_key => MIME::Base64::decode_base64url( $res->{credential_pubkey} ), |
| 120 |
sign_count => $res->{signature_count} // 0, |
| 121 |
transports => undef, |
| 122 |
created_date => dt_from_string()->strftime('%F %T'), |
| 123 |
} |
| 124 |
)->store; |
| 125 |
|
| 126 |
$c->app->log->info( 'WebAuthn credential registered for patron ' . $patron_id ); |
| 127 |
$c->render( status => 201, openapi => { success => 1 } ); |
| 128 |
} catch { |
| 129 |
return _handle_exception( $c, $_ ); |
| 130 |
}; |
| 131 |
} |
| 132 |
|
| 133 |
=head3 authenticate_challenge |
| 134 |
|
| 135 |
Controller for POST /api/v1/webauthn/authenticate/challenge |
| 136 |
|
| 137 |
=cut |
| 138 |
|
| 139 |
sub authenticate_challenge { |
| 140 |
my $c = shift->openapi->valid_input or return; |
| 141 |
return try { |
| 142 |
my $body = $c->req->json; |
| 143 |
my ( $patron, $patron_id ) = Koha::Auth::WebAuthn->resolve_patron($body); |
| 144 |
|
| 145 |
my $credentials = Koha::WebauthnCredentials->search( { borrowernumber => $patron_id } ); |
| 146 |
Koha::Exceptions::WebAuthn::NoCredentials->throw() unless $credentials->count; |
| 147 |
|
| 148 |
my ( $origin, $rp_id ) = Koha::Auth::WebAuthn->get_origin_and_rp_id($c); |
| 149 |
my $challenge_b64u = Koha::Auth::WebAuthn->generate_challenge(); |
| 150 |
|
| 151 |
Koha::Auth::WebAuthn->store_challenge( $c, $challenge_b64u, $patron_id ); |
| 152 |
|
| 153 |
my @allow_credentials; |
| 154 |
while ( my $cred = $credentials->next ) { |
| 155 |
push @allow_credentials, |
| 156 |
{ |
| 157 |
id => Koha::Auth::WebAuthn->std_b64_to_b64url( encode_base64( $cred->credential_id, '' ) ), |
| 158 |
type => 'public-key', |
| 159 |
}; |
| 160 |
} |
| 161 |
|
| 162 |
$c->render( |
| 163 |
openapi => { |
| 164 |
challenge => $challenge_b64u, |
| 165 |
rpId => $rp_id, |
| 166 |
allowCredentials => \@allow_credentials, |
| 167 |
} |
| 168 |
); |
| 169 |
} catch { |
| 170 |
return _handle_exception( $c, $_ ); |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
=head3 authenticate |
| 175 |
|
| 176 |
Controller for POST /api/v1/webauthn/authenticate |
| 177 |
|
| 178 |
=cut |
| 179 |
|
| 180 |
sub authenticate { |
| 181 |
my $c = shift->openapi->valid_input or return; |
| 182 |
return try { |
| 183 |
my $body = $c->req->json; |
| 184 |
my ( $patron, $patron_id ) = Koha::Auth::WebAuthn->resolve_patron($body); |
| 185 |
|
| 186 |
my $assertion = $body->{assertion_response}; |
| 187 |
return $c->render( status => 400, openapi => { error => 'Missing assertion_response' } ) |
| 188 |
unless $assertion; |
| 189 |
|
| 190 |
# Reject locked or expired accounts |
| 191 |
if ( $patron->account_locked ) { |
| 192 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Account is locked' ); |
| 193 |
} |
| 194 |
|
| 195 |
# Build credential lookup maps |
| 196 |
my $credentials = Koha::WebauthnCredentials->search( { borrowernumber => $patron_id } ); |
| 197 |
my ( %cred_id_map, %pubkeys, %sign_counts ); |
| 198 |
while ( my $cred = $credentials->next ) { |
| 199 |
my $id_b64u = Koha::Auth::WebAuthn->std_b64_to_b64url( encode_base64( $cred->credential_id, '' ) ); |
| 200 |
$cred_id_map{$id_b64u} = $cred->webauthn_credential_id; |
| 201 |
$pubkeys{$id_b64u} = Koha::Auth::WebAuthn->std_b64_to_b64url( encode_base64( $cred->public_key, '' ) ); |
| 202 |
$sign_counts{$id_b64u} = $cred->sign_count // 0; |
| 203 |
} |
| 204 |
|
| 205 |
my $challenge_b64u = Koha::Auth::WebAuthn->validate_and_consume_challenge( $c, $patron_id ); |
| 206 |
my ( $origin, $rp_id ) = Koha::Auth::WebAuthn->get_origin_and_rp_id($c); |
| 207 |
|
| 208 |
# Browser sends base64 (we need base64url) |
| 209 |
my $client_data_b64u = |
| 210 |
Koha::Auth::WebAuthn->std_b64_to_b64url( $assertion->{clientDataJSON} // $assertion->{client_data_json} |
| 211 |
// '' ); |
| 212 |
|
| 213 |
# Allow http->https upgrade based on client-reported origin |
| 214 |
my $client_origin = |
| 215 |
Koha::Auth::WebAuthn->extract_client_origin( $assertion->{clientDataJSON} |
| 216 |
// $assertion->{client_data_json} ); |
| 217 |
$origin = Koha::Auth::WebAuthn->maybe_upgrade_origin( $origin, $rp_id, $client_origin ); |
| 218 |
|
| 219 |
my $webauthn = Authen::WebAuthn->new( rp_id => $rp_id, origin => $origin ); |
| 220 |
my $auth_data_b64u = |
| 221 |
Koha::Auth::WebAuthn->std_b64_to_b64url( $assertion->{authenticatorData} |
| 222 |
// $assertion->{authenticator_data} // '' ); |
| 223 |
my $signature_b64u = Koha::Auth::WebAuthn->std_b64_to_b64url( $assertion->{signature} // '' ); |
| 224 |
my $credential_id_b64u = |
| 225 |
Koha::Auth::WebAuthn->std_b64_to_b64url( $assertion->{id} // $assertion->{raw_id} // '' ); |
| 226 |
|
| 227 |
# Verify credential exists for this patron |
| 228 |
my $credential_pubkey = $pubkeys{$credential_id_b64u}; |
| 229 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Unknown credential' ) |
| 230 |
unless $credential_pubkey; |
| 231 |
|
| 232 |
my $stored_sign_count = $sign_counts{$credential_id_b64u} // 0; |
| 233 |
my $res; |
| 234 |
try { |
| 235 |
$res = $webauthn->validate_assertion( |
| 236 |
challenge_b64 => $challenge_b64u, |
| 237 |
credential_pubkey_b64 => $credential_pubkey, |
| 238 |
stored_sign_count => $stored_sign_count, |
| 239 |
requested_uv => 'preferred', |
| 240 |
client_data_json_b64 => $client_data_b64u, |
| 241 |
authenticator_data_b64 => $auth_data_b64u, |
| 242 |
signature_b64 => $signature_b64u, |
| 243 |
); |
| 244 |
} catch { |
| 245 |
$c->app->log->warn( 'WebAuthn authenticate failed for patron ' . $patron_id . ': ' . $_ ); |
| 246 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Authentication failed' ); |
| 247 |
}; |
| 248 |
unless ($res) { |
| 249 |
$c->app->log->warn( |
| 250 |
'WebAuthn authenticate failed for patron ' . $patron_id . ': verification returned false' ); |
| 251 |
Koha::Exceptions::WebAuthn::VerificationFailed->throw( error => 'Authentication failed' ); |
| 252 |
} |
| 253 |
|
| 254 |
# Update sign count |
| 255 |
if ( my $cred_pk = $cred_id_map{$credential_id_b64u} ) { |
| 256 |
if ( my $cred = Koha::WebauthnCredentials->find($cred_pk) ) { |
| 257 |
$cred->set( |
| 258 |
{ |
| 259 |
sign_count => $res->{signature_count} // 0, |
| 260 |
last_used_date => dt_from_string()->strftime('%F %T'), |
| 261 |
} |
| 262 |
)->store; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
# Issue staff session and cookie for login |
| 267 |
my $session = create_basic_session( { patron => $patron, interface => 'intranet' } ); |
| 268 |
my $is_https = ( $origin =~ m{^https://}i ) ? 1 : 0; |
| 269 |
$c->cookie( |
| 270 |
CGISESSID => $session->id, |
| 271 |
{ path => '/', http_only => 1, same_site => 'Lax', secure => $is_https } |
| 272 |
); |
| 273 |
C4::Context->interface('intranet'); |
| 274 |
my $lib = $patron->library; |
| 275 |
C4::Context->set_userenv( |
| 276 |
$patron->borrowernumber, |
| 277 |
$patron->userid // '', |
| 278 |
$patron->cardnumber // '', |
| 279 |
$patron->firstname // '', |
| 280 |
$patron->surname // '', |
| 281 |
( $lib ? $lib->branchcode : '' ), |
| 282 |
( $lib ? $lib->branchname : '' ), |
| 283 |
$patron->flags, |
| 284 |
$patron->email // '', |
| 285 |
); |
| 286 |
|
| 287 |
$c->app->log->info( 'WebAuthn authentication successful for patron ' . $patron_id ); |
| 288 |
$c->render( status => 200, openapi => { success => 1 } ); |
| 289 |
} catch { |
| 290 |
return _handle_exception( $c, $_ ); |
| 291 |
}; |
| 292 |
} |
| 293 |
|
| 294 |
=head3 _handle_exception |
| 295 |
|
| 296 |
Maps WebAuthn exceptions to appropriate HTTP responses. |
| 297 |
|
| 298 |
=cut |
| 299 |
|
| 300 |
sub _handle_exception { |
| 301 |
my ( $c, $err ) = @_; |
| 302 |
|
| 303 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::ConfigMissing') ) { |
| 304 |
return $c->render( |
| 305 |
status => 500, |
| 306 |
openapi => { error => $err->error // $err->description } |
| 307 |
); |
| 308 |
} |
| 309 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::PatronNotFound') ) { |
| 310 |
return $c->render( |
| 311 |
status => 404, |
| 312 |
openapi => { error => 'Patron not found' } |
| 313 |
); |
| 314 |
} |
| 315 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::NoCredentials') ) { |
| 316 |
return $c->render( |
| 317 |
status => 404, |
| 318 |
openapi => { error => 'No credentials registered' } |
| 319 |
); |
| 320 |
} |
| 321 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::InvalidChallenge') ) { |
| 322 |
return $c->render( |
| 323 |
status => 401, |
| 324 |
openapi => { error => $err->error // 'Invalid or expired challenge' } |
| 325 |
); |
| 326 |
} |
| 327 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::ChallengeMismatch') ) { |
| 328 |
return $c->render( |
| 329 |
status => 401, |
| 330 |
openapi => { error => 'Challenge patron mismatch' } |
| 331 |
); |
| 332 |
} |
| 333 |
if ( ref $err && $err->isa('Koha::Exceptions::WebAuthn::VerificationFailed') ) { |
| 334 |
return $c->render( |
| 335 |
status => 401, |
| 336 |
openapi => { error => $err->error // 'Verification failed' } |
| 337 |
); |
| 338 |
} |
| 339 |
|
| 340 |
return $c->unhandled_exception($err); |
| 341 |
} |
| 342 |
|
| 343 |
1; |