| 1 |
# Test 2F API |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Test::More; |
| 5 |
use strict; |
| 6 |
use JSON; |
| 7 |
use IO::String; |
| 8 |
use Lemonldap::NG::Common::Session; |
| 9 |
use Lemonldap::NG::Common::TOTP; |
| 10 |
|
| 11 |
require 't/test-lib.pm'; |
| 12 |
|
| 13 |
our $_json = JSON->new->allow_nonref; |
| 14 |
|
| 15 |
sub newSession { |
| 16 |
my ( $uid, $ip, $kind, $sfaDevices ) = splice @_; |
| 17 |
my $tmp; |
| 18 |
ok( |
| 19 |
$tmp = Lemonldap::NG::Common::Session->new( { |
| 20 |
storageModule => 'Apache::Session::File', |
| 21 |
storageModuleOptions => { |
| 22 |
Directory => "$main::tmpdir/sessions", |
| 23 |
LockDirectory => "$main::tmpdir/sessions", |
| 24 |
backend => 'Apache::Session::File', |
| 25 |
generateModule => |
| 26 |
'Lemonldap::NG::Common::Apache::Session::Generate::SHA256', |
| 27 |
}, |
| 28 |
} |
| 29 |
), |
| 30 |
'Sessions module' |
| 31 |
); |
| 32 |
count(1); |
| 33 |
ok( |
| 34 |
$tmp->update( { |
| 35 |
ipAddr => $ip, |
| 36 |
_whatToTrace => $uid, |
| 37 |
uid => $uid, |
| 38 |
_session_uid => $uid, |
| 39 |
_utime => time, |
| 40 |
_session_kind => $kind, |
| 41 |
( |
| 42 |
defined $sfaDevices |
| 43 |
? ( _2fDevices => to_json($sfaDevices) ) |
| 44 |
: () |
| 45 |
), |
| 46 |
} |
| 47 |
), |
| 48 |
"New $kind session for $uid" |
| 49 |
); |
| 50 |
count(1); |
| 51 |
return $tmp->id; |
| 52 |
} |
| 53 |
|
| 54 |
sub get2fDevices { |
| 55 |
my ($id) = @_; |
| 56 |
my $tmp; |
| 57 |
ok( |
| 58 |
$tmp = Lemonldap::NG::Common::Session->new( { |
| 59 |
storageModule => 'Apache::Session::File', |
| 60 |
storageModuleOptions => { |
| 61 |
Directory => "$main::tmpdir/sessions", |
| 62 |
LockDirectory => "$main::tmpdir/sessions", |
| 63 |
backend => 'Apache::Session::File', |
| 64 |
generateModule => |
| 65 |
'Lemonldap::NG::Common::Apache::Session::Generate::SHA256', |
| 66 |
}, |
| 67 |
id => $id, |
| 68 |
} |
| 69 |
), |
| 70 |
'Sessions module' |
| 71 |
); |
| 72 |
count(1); |
| 73 |
return from_json( $tmp->data->{_2fDevices}, { allow_nonref => 1 } ); |
| 74 |
} |
| 75 |
|
| 76 |
sub checkTotpData { |
| 77 |
my ( $totp, $key, %attr ) = @_; |
| 78 |
|
| 79 |
is( $totp->{type}, "TOTP", "Correct type" ); |
| 80 |
ok( $totp->{epoch}, "Epoch was set" ); |
| 81 |
while ( my ( $k, $v ) = each %attr ) { |
| 82 |
is( $totp->{$k}, $v, "Correct $k" ); |
| 83 |
} |
| 84 |
|
| 85 |
like( $totp->{_secret}, qr/llngcrypt/, "Secret was encrypted" ); |
| 86 |
|
| 87 |
my $totp_decrypt = Lemonldap::NG::Common::TOTP->new( |
| 88 |
key => "abc", |
| 89 |
encryptSecret => 1, |
| 90 |
); |
| 91 |
|
| 92 |
is( $totp_decrypt->get_cleartext_secret( $totp->{_secret} ), |
| 93 |
$key, "Correct normalized key" ); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
sub check200 { |
| 98 |
my ( $test, $res ) = splice @_; |
| 99 |
ok( $res->[0] == 200, "$test: Result code is 200" ); |
| 100 |
count(1); |
| 101 |
checkJson( $test, $res ); |
| 102 |
} |
| 103 |
|
| 104 |
sub check201 { |
| 105 |
my ( $test, $res ) = splice @_; |
| 106 |
|
| 107 |
is( $res->[0], "201", "$test: Result code is 201" ); |
| 108 |
count(1); |
| 109 |
checkJson( $test, $res ); |
| 110 |
} |
| 111 |
|
| 112 |
sub check204 { |
| 113 |
my ( $test, $res ) = splice @_; |
| 114 |
|
| 115 |
is( $res->[0], "204", "$test: Result code is 204" ); |
| 116 |
count(1); |
| 117 |
is( $res->[2]->[0], undef, "204 code returns no content" ); |
| 118 |
} |
| 119 |
|
| 120 |
sub check400 { |
| 121 |
my ( $test, $res, $regex ) = splice @_; |
| 122 |
ok( $res->[0] == 400, "$test: Result code is 400" ); |
| 123 |
count(1); |
| 124 |
checkJson( $test, $res, $regex ); |
| 125 |
} |
| 126 |
|
| 127 |
sub check404 { |
| 128 |
my ( $test, $res ) = splice @_; |
| 129 |
ok( $res->[0] == 404, "$test: Result code is 404" ); |
| 130 |
count(1); |
| 131 |
checkJson( $test, $res ); |
| 132 |
} |
| 133 |
|
| 134 |
sub check409 { |
| 135 |
my ( $test, $res ) = splice @_; |
| 136 |
is( $res->[0], "409", "$test: Result code is 409" ); |
| 137 |
count(1); |
| 138 |
checkJson( $test, $res ); |
| 139 |
} |
| 140 |
|
| 141 |
sub checkJson { |
| 142 |
my ( $test, $res, $regex ) = splice @_; |
| 143 |
my $key; |
| 144 |
|
| 145 |
#diag Dumper($res->[2]->[0]); |
| 146 |
ok( $key = from_json( $res->[2]->[0] ), "$test: Response is JSON" ); |
| 147 |
if ($regex) { |
| 148 |
like( $key->{error}, $regex, "Expected error message" ); |
| 149 |
count(1); |
| 150 |
} |
| 151 |
count(1); |
| 152 |
} |
| 153 |
|
| 154 |
sub checkSearchNotFound { |
| 155 |
my ($params) = @_; |
| 156 |
my $test = |
| 157 |
"Searching for " . ( $params || '[no params]' ) . " returns no results"; |
| 158 |
my $res = search( $test, $params ); |
| 159 |
check200( $test, $res ); |
| 160 |
my $result = from_json( $res->[2]->[0] ); |
| 161 |
is_deeply( $result, [], "Empty list was returned" ); |
| 162 |
} |
| 163 |
|
| 164 |
sub checkSearch { |
| 165 |
my ( $params, $expected_list ) = @_; |
| 166 |
$expected_list ||= []; |
| 167 |
my $test = |
| 168 |
"Searching for " |
| 169 |
. ( $params || '[no params]' ) |
| 170 |
. " returns " |
| 171 |
. join( ",", @$expected_list ); |
| 172 |
my $res = search( $test, $params ); |
| 173 |
check200( $test, $res ); |
| 174 |
my $result = from_json( $res->[2]->[0] ); |
| 175 |
my @names = map { $_->{uid} } @$result; |
| 176 |
is_deeply( [ sort @names ], $expected_list, "Expected results" ); |
| 177 |
return $result; |
| 178 |
} |
| 179 |
|
| 180 |
sub search { |
| 181 |
my ( $test, $params ) = @_; |
| 182 |
ok( my $res = &client->_get( "/api/v1/secondFactor", $params ), |
| 183 |
"$test: Request succeed" ); |
| 184 |
count(1); |
| 185 |
return $res; |
| 186 |
} |
| 187 |
|
| 188 |
sub get { |
| 189 |
my ( $test, $uid, $type, $id ) = splice @_; |
| 190 |
my ($res); |
| 191 |
ok( |
| 192 |
$res = &client->_get( |
| 193 |
"/api/v1/secondFactor/$uid" |
| 194 |
. ( |
| 195 |
defined $type ? "/type/$type" : ( defined $id ? "/id/$id" : "" ) |
| 196 |
) |
| 197 |
), |
| 198 |
"$test: Request succeed" |
| 199 |
); |
| 200 |
count(1); |
| 201 |
return $res; |
| 202 |
} |
| 203 |
|
| 204 |
sub checkGet { |
| 205 |
my ( $uid, $id ) = splice @_; |
| 206 |
my ( $test, $res, $ret ); |
| 207 |
$test = "$uid should have one 2F with id \"$id\""; |
| 208 |
$res = get( $test, $uid, undef, $id ); |
| 209 |
check200( $test, $res ); |
| 210 |
|
| 211 |
#diag Dumper($res); |
| 212 |
$ret = from_json( $res->[2]->[0] ); |
| 213 |
ok( ref $ret eq 'HASH' && $ret->{id} eq $id, |
| 214 |
"$test: check returned type is HASH and that ids match" ); |
| 215 |
count(1); |
| 216 |
return $ret; |
| 217 |
} |
| 218 |
|
| 219 |
sub checkGet404 { |
| 220 |
my ( $uid, $id ) = splice @_; |
| 221 |
my ( $test, $res, $ret ); |
| 222 |
$test = "$uid should not have any 2F with id \"$id\""; |
| 223 |
$res = get( $test, $uid, undef, $id ); |
| 224 |
check404( $test, $res ); |
| 225 |
} |
| 226 |
|
| 227 |
sub checkGetList { |
| 228 |
my ( $expect, $uid, $type ) = splice @_; |
| 229 |
my ( $test, $res, $ret ); |
| 230 |
$test = "$uid should have $expect 2F" |
| 231 |
. ( defined $type ? " of type \"$type\"" : "" ); |
| 232 |
$res = get( $test, $uid, $type ); |
| 233 |
check200( $test, $res ); |
| 234 |
|
| 235 |
#diag Dumper($res); |
| 236 |
$ret = from_json( $res->[2]->[0] ); |
| 237 |
ok( |
| 238 |
scalar @$ret eq $expect, |
| 239 |
"$test: check if nb of 2F found (" |
| 240 |
. scalar @$ret |
| 241 |
. ") equals expectation ($expect)" |
| 242 |
); |
| 243 |
count(1); |
| 244 |
return $ret; |
| 245 |
} |
| 246 |
|
| 247 |
sub checkDisplay { |
| 248 |
my ( $uid, $id ) = splice @_; |
| 249 |
my $ret = checkGet( $uid, $id ); |
| 250 |
is( $ret->{mydisplay}, 1, "Found display variable" ); |
| 251 |
} |
| 252 |
|
| 253 |
sub checkGetOnIds { |
| 254 |
my ( $uid, $ret ) = splice @_; |
| 255 |
foreach (@$ret) { |
| 256 |
checkGet( $uid, $_->{id} ); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
sub checkGetOnIdsNotFound { |
| 261 |
my ( $uid, $ret ) = splice @_; |
| 262 |
foreach (@$ret) { |
| 263 |
checkGet404( $uid, $_->{id} ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
sub del { |
| 268 |
my ( $test, $uid, $type, $id ) = splice @_; |
| 269 |
my ($res); |
| 270 |
ok( |
| 271 |
$res = &client->_del( |
| 272 |
"/api/v1/secondFactor/$uid" |
| 273 |
. ( |
| 274 |
defined $type ? "/type/$type" : ( defined $id ? "/id/$id" : "" ) |
| 275 |
) |
| 276 |
), |
| 277 |
"$test: Request succeed" |
| 278 |
); |
| 279 |
count(1); |
| 280 |
return $res; |
| 281 |
} |
| 282 |
|
| 283 |
sub checkDelete { |
| 284 |
my ( $uid, $id ) = splice @_; |
| 285 |
my ( $test, $res ); |
| 286 |
$test = "$uid should have a 2F with id \"$id\" to be deleted."; |
| 287 |
$res = del( $test, $uid, undef, $id ); |
| 288 |
check200( $test, $res ); |
| 289 |
} |
| 290 |
|
| 291 |
sub checkDelete404 { |
| 292 |
my ( $uid, $id ) = splice @_; |
| 293 |
my ( $test, $res ); |
| 294 |
$test = "$uid should not have a 2F with id \"$id\" to be deleted."; |
| 295 |
$res = del( $test, $uid, undef, $id ); |
| 296 |
check404( $test, $res ); |
| 297 |
} |
| 298 |
|
| 299 |
sub checkDeleteList { |
| 300 |
my ( $expect, $uid, $type ) = splice @_; |
| 301 |
my ( $test, $res, $ret, $countDel ); |
| 302 |
$test = |
| 303 |
"Delete all 2F from $uid" . ( defined $type ? " of type \"$type\"" : "" ); |
| 304 |
$res = del( $test, $uid, $type ); |
| 305 |
check200( $test, $res ); |
| 306 |
$ret = from_json( $res->[2]->[0] ); |
| 307 |
($countDel) = $ret->{message} =~ m/^Successful operation: ([\d]+) /i; |
| 308 |
$countDel = 0 unless ( defined $countDel ); |
| 309 |
ok( |
| 310 |
$countDel eq $expect, |
| 311 |
"$test: check nb of 2FA deleted ($countDel) matches expectation ($expect)" |
| 312 |
); |
| 313 |
count(1); |
| 314 |
} |
| 315 |
|
| 316 |
sub checkDeleteBadType { |
| 317 |
my ( $uid, $type ) = splice @_; |
| 318 |
my ( $test, $res ); |
| 319 |
$test = "Delete for uid $uid and type \"$type\" should get rejected."; |
| 320 |
$res = del( $test, $uid, $type ); |
| 321 |
check400( $test, $res ); |
| 322 |
} |
| 323 |
|
| 324 |
sub replace { |
| 325 |
my ( $test, $uid, $id, $obj ) = splice @_; |
| 326 |
my $j = $_json->encode($obj); |
| 327 |
my $res; |
| 328 |
ok( |
| 329 |
$res = &client->_put( |
| 330 |
"/api/v1/secondFactor/$uid/id/$id", '', |
| 331 |
IO::String->new($j), 'application/json', |
| 332 |
length($j) |
| 333 |
), |
| 334 |
"$test: Request succeed" |
| 335 |
); |
| 336 |
count(1); |
| 337 |
return $res; |
| 338 |
} |
| 339 |
|
| 340 |
sub checkReplace { |
| 341 |
my ( $test, $uid, $id, $replace ) = splice @_; |
| 342 |
check204( $test, replace( $test, $uid, $id, $replace ) ); |
| 343 |
} |
| 344 |
|
| 345 |
sub checkReplaceAlreadyThere { |
| 346 |
my ( $test, $uid, $id, $replace ) = splice @_; |
| 347 |
check400( $test, replace( $test, $uid, $id, $replace ) ); |
| 348 |
} |
| 349 |
|
| 350 |
sub checkReplaceNotFound { |
| 351 |
my ( $test, $uid, $id, $update ) = splice @_; |
| 352 |
check404( $test, replace( $test, $uid, $id, $update ) ); |
| 353 |
} |
| 354 |
|
| 355 |
sub checkReplaceWithInvalidAttribute { |
| 356 |
my ( $test, $uid, $id, $replace ) = splice @_; |
| 357 |
check400( $test, replace( $test, $uid, $id, $replace ) ); |
| 358 |
} |
| 359 |
|
| 360 |
sub add { |
| 361 |
my ( $test, $uid, $type, $obj, $query ) = splice @_; |
| 362 |
my $j = $_json->encode($obj); |
| 363 |
my $res; |
| 364 |
|
| 365 |
my $path = $type ? "/type/$type" : ""; |
| 366 |
ok( |
| 367 |
$res = &client->_post( |
| 368 |
"/api/v1/secondFactor/${uid}${path}", ( $query || '' ), |
| 369 |
IO::String->new($j), 'application/json', |
| 370 |
length($j) |
| 371 |
), |
| 372 |
"$test: Request succeed" |
| 373 |
); |
| 374 |
count(1); |
| 375 |
return $res; |
| 376 |
} |
| 377 |
|
| 378 |
sub checkAdd { |
| 379 |
my ( $test, $uid, $type, $add, $query ) = splice @_; |
| 380 |
check201( $test, add( $test, $uid, $type, $add, $query ) ); |
| 381 |
} |
| 382 |
|
| 383 |
sub checkAddFailsIfExists { |
| 384 |
my ( $test, $uid, $type, $add ) = splice @_; |
| 385 |
check409( $test, add( $test, $uid, $type, $add ) ); |
| 386 |
} |
| 387 |
|
| 388 |
sub checkAddWithBadAttributes { |
| 389 |
my ( $test, $uid, $type, $add, $regex ) = splice @_; |
| 390 |
$regex ||= qr/Invalid input/; |
| 391 |
check400( $test, add( $test, $uid, $type, $add ), $regex ); |
| 392 |
} |
| 393 |
|
| 394 |
sub checkAddWithUnknownUser { |
| 395 |
my ( $test, $uid, $type, $add, $query ) = splice @_; |
| 396 |
check404( $test, add( $test, $uid, $type, $add, $query ) ); |
| 397 |
} |
| 398 |
|
| 399 |
sub checkAddWithUnknownType { |
| 400 |
my ( $test, $uid, $type, $add ) = splice @_; |
| 401 |
check400( $test, add( $test, $uid, $type, $add ), qr/Invalid type: $type/ ); |
| 402 |
} |
| 403 |
|
| 404 |
my $sfaDevices = []; |
| 405 |
my $ret; |
| 406 |
|
| 407 |
## Sessions creation |
| 408 |
# msmith |
| 409 |
newSession( 'msmith', '127.10.0.1', 'SSO', $sfaDevices ); |
| 410 |
newSession( 'msmith', '127.10.0.1', 'Persistent', $sfaDevices ); |
| 411 |
|
| 412 |
# dwho |
| 413 |
$sfaDevices = [ { |
| 414 |
"name" => "MyU2FKey", |
| 415 |
"type" => "U2F", |
| 416 |
"_userKey" => "123456", |
| 417 |
"_keyHandle" => "654321", |
| 418 |
"epoch" => time |
| 419 |
}, |
| 420 |
{ |
| 421 |
"name" => "MyTOTP", |
| 422 |
"type" => "TOTP", |
| 423 |
"_secret" => "123456", |
| 424 |
"epoch" => time, |
| 425 |
"mydisplay" => 1, |
| 426 |
}, |
| 427 |
{ |
| 428 |
"name" => "MyYubikey", |
| 429 |
"type" => "UBK", |
| 430 |
"_secret" => "123456", |
| 431 |
"epoch" => time |
| 432 |
}, |
| 433 |
{ |
| 434 |
"_credentialId" => "abc", |
| 435 |
"_credentialPublicKey" => "abc", |
| 436 |
"_signCount" => "65", |
| 437 |
"epoch" => "1643201784", |
| 438 |
"name" => "MyFidoKey", |
| 439 |
"type" => "WebAuthn" |
| 440 |
}, |
| 441 |
]; |
| 442 |
newSession( 'dwho', '127.10.0.1', 'SSO', $sfaDevices ); |
| 443 |
newSession( 'dwho', '127.10.0.1', 'Persistent', $sfaDevices ); |
| 444 |
|
| 445 |
# rtyler |
| 446 |
$sfaDevices = [ { |
| 447 |
"name" => "MyU2FKey", |
| 448 |
"type" => "U2F", |
| 449 |
"_userKey" => "123456", |
| 450 |
"_keyHandle" => "654321", |
| 451 |
"epoch" => time |
| 452 |
}, |
| 453 |
{ |
| 454 |
"name" => "MyYubikey", |
| 455 |
"type" => "UBK", |
| 456 |
"_secret" => "123456", |
| 457 |
"epoch" => time |
| 458 |
}, |
| 459 |
{ |
| 460 |
"name" => "MyYubikey2", |
| 461 |
"type" => "UBK", |
| 462 |
"_secret" => "654321", |
| 463 |
"epoch" => time |
| 464 |
} |
| 465 |
]; |
| 466 |
newSession( 'rtyler', '127.10.0.1', 'SSO', $sfaDevices ); |
| 467 |
newSession( 'rtyler', '127.10.0.1', 'Persistent', $sfaDevices ); |
| 468 |
|
| 469 |
# davros |
| 470 |
$sfaDevices = [ { |
| 471 |
"name" => "MyU2FKey", |
| 472 |
"type" => "U2F", |
| 473 |
"_userKey" => "123456", |
| 474 |
"_keyHandle" => "654321", |
| 475 |
"epoch" => time |
| 476 |
}, |
| 477 |
{ |
| 478 |
"name" => "MyTOTP", |
| 479 |
"type" => "TOTP", |
| 480 |
"_secret" => "123456", |
| 481 |
"epoch" => time |
| 482 |
} |
| 483 |
]; |
| 484 |
newSession( 'davros', '127.10.0.1', 'SSO', $sfaDevices ); |
| 485 |
newSession( 'davros', '127.10.0.1', 'Persistent', $sfaDevices ); |
| 486 |
|
| 487 |
# tof |
| 488 |
$sfaDevices = [ { |
| 489 |
"name" => "MyU2FKey", |
| 490 |
"type" => "U2F", |
| 491 |
"_userKey" => "123456", |
| 492 |
"_keyHandle" => "654321", |
| 493 |
"epoch" => time |
| 494 |
} |
| 495 |
]; |
| 496 |
newSession( 'tof', '127.10.0.1', 'SSO', $sfaDevices ); |
| 497 |
newSession( 'tof', '127.10.0.1', 'Persistent', $sfaDevices ); |
| 498 |
|
| 499 |
# donna |
| 500 |
newSession( 'donna', '127.10.0.1', 'SSO', [] ); |
| 501 |
newSession( 'donna', '127.10.0.1', 'Persistent', [] ); |
| 502 |
|
| 503 |
# dwho |
| 504 |
|
| 505 |
$ret = checkGetList( 1, 'dwho', 'TOTP' ); |
| 506 |
checkDisplay( 'dwho', $ret->[0]->{id} ); |
| 507 |
|
| 508 |
checkGetList( 1, 'dwho', 'U2F' ); |
| 509 |
checkGetList( 1, 'dwho', 'UBK' ); |
| 510 |
checkGetList( 1, 'dwho', 'WebAuthn' ); |
| 511 |
checkGetList( 0, 'dwho', 'UBKIKI' ); |
| 512 |
$ret = checkGetList( 4, 'dwho' ); |
| 513 |
checkGetOnIds( 'dwho', $ret ); |
| 514 |
checkDelete( 'dwho', @$ret[0]->{id} ); |
| 515 |
checkDelete404( 'dwho', @$ret[0]->{id} ); |
| 516 |
|
| 517 |
checkGetList( 3, 'dwho' ); |
| 518 |
checkDeleteList( 1, 'dwho', 'WebAuthn' ); |
| 519 |
checkGetList( 0, 'dwho', 'WebAuthn' ); |
| 520 |
checkDeleteList( 2, 'dwho' ); |
| 521 |
checkGetList( 0, 'dwho' ); |
| 522 |
checkDeleteList( 0, 'dwho' ); |
| 523 |
|
| 524 |
# msmith |
| 525 |
$ret = checkGetList( 0, 'msmith' ); |
| 526 |
|
| 527 |
# rtyler |
| 528 |
checkGetList( 1, 'rtyler', 'U2F' ); |
| 529 |
checkGetList( 0, 'rtyler', 'TOTP' ); |
| 530 |
checkGetList( 2, 'rtyler', 'UBK' ); |
| 531 |
$ret = checkGetList( 3, 'rtyler' ); |
| 532 |
checkGetOnIds( 'rtyler', $ret ); |
| 533 |
checkDeleteList( 2, 'rtyler', 'UBK' ); |
| 534 |
$ret = checkGetList( 1, 'rtyler' ); |
| 535 |
checkDelete( 'rtyler', @$ret[0]->{id} ); |
| 536 |
checkDelete404( 'rtyler', @$ret[0]->{id} ); |
| 537 |
checkDeleteList( 0, 'rtyler' ); |
| 538 |
|
| 539 |
# davros |
| 540 |
checkGetList( 1, 'davros', 'U2F' ); |
| 541 |
checkGetList( 1, 'davros', 'TOTP' ); |
| 542 |
checkGetList( 0, 'davros', 'UBK' ); |
| 543 |
$ret = checkGetList( 2, 'davros' ); |
| 544 |
checkGetOnIds( 'davros', $ret ); |
| 545 |
checkDelete( 'davros', @$ret[0]->{id} ); |
| 546 |
checkDelete404( 'davros', @$ret[0]->{id} ); |
| 547 |
checkGetList( 1, 'davros' ); |
| 548 |
checkDeleteList( 1, 'davros', @$ret[1]->{type} ); |
| 549 |
checkGetList( 0, 'davros' ); |
| 550 |
checkDeleteList( 0, 'davros' ); |
| 551 |
|
| 552 |
# tof |
| 553 |
checkGetList( 1, 'tof', 'U2F' ); |
| 554 |
checkGetList( 0, 'tof', 'TOTP' ); |
| 555 |
checkGetList( 0, 'tof', 'UBK' ); |
| 556 |
$ret = checkGetList( 1, 'tof' ); |
| 557 |
checkGetOnIds( 'tof', $ret ); |
| 558 |
checkDelete( 'tof', @$ret[0]->{id} ); |
| 559 |
checkDelete404( 'tof', @$ret[0]->{id} ); |
| 560 |
checkGetList( 0, 'tof' ); |
| 561 |
checkDeleteList( 0, 'tof' ); |
| 562 |
|
| 563 |
# 2FA add (generic) |
| 564 |
checkAddWithBadAttributes( "Add/noattr ", "donna", undef, {}, |
| 565 |
qr/Invalid input/ ); |
| 566 |
checkAddWithBadAttributes( |
| 567 |
"Add/epoch", "donna", undef, |
| 568 |
{ name => "test", type => "test", epoch => 1 }, |
| 569 |
qr/Invalid input: epoch is forbidden/ |
| 570 |
); |
| 571 |
|
| 572 |
# Make epoch predictable |
| 573 |
no warnings 'redefine'; |
| 574 |
*Lemonldap::NG::Manager::Api::_get_epoch = sub { |
| 575 |
return 123; |
| 576 |
}; |
| 577 |
|
| 578 |
checkAdd( "Add second factor", |
| 579 |
"donna", undef, { type => "test", name => "test", mydisplay => 1 } ); |
| 580 |
$ret = checkGetList( 1, 'donna', 'test' ); |
| 581 |
checkDisplay( 'donna', $ret->[0]->{id} ); |
| 582 |
|
| 583 |
checkAddFailsIfExists( "Add second factor with same ID as previous", |
| 584 |
"donna", undef, { type => "test", name => "test" } ); |
| 585 |
|
| 586 |
checkAdd( "Add second factor with different ID", |
| 587 |
"donna", undef, { type => "test", name => "test2" } ); |
| 588 |
$ret = checkGetList( 2, 'donna', 'test' ); |
| 589 |
is_deeply( |
| 590 |
$ret, |
| 591 |
[ { |
| 592 |
'epoch' => '123', |
| 593 |
'id' => 'MTIzOjp0ZXN0Ojp0ZXN0', |
| 594 |
'mydisplay' => 1, |
| 595 |
'name' => 'test', |
| 596 |
'type' => 'test' |
| 597 |
}, |
| 598 |
{ |
| 599 |
'epoch' => '123', |
| 600 |
'id' => 'MTIzOjp0ZXN0Ojp0ZXN0Mg==', |
| 601 |
'name' => 'test2', |
| 602 |
'type' => 'test' |
| 603 |
} |
| 604 |
], |
| 605 |
"Expected second factors data" |
| 606 |
); |
| 607 |
|
| 608 |
# 2FA add (invalid type) |
| 609 |
checkAddWithUnknownType( "Add/noattr ", "amy", "xxx", {} ); |
| 610 |
|
| 611 |
# 2FA add (TOTP) |
| 612 |
newSession( 'amy', '127.10.0.1', 'SSO', [] ); |
| 613 |
my $amypsession = newSession( 'amy', '127.10.0.1', 'Persistent', [] ); |
| 614 |
|
| 615 |
checkAddWithBadAttributes( "Add/noattr ", "amy", "TOTP", {} ); |
| 616 |
checkAddWithBadAttributes( "Add/epoch", "amy", "TOTP", |
| 617 |
{ name => "test", type => "test", epoch => 1 } ); |
| 618 |
|
| 619 |
checkAddWithBadAttributes( "Add/nokey", "amy", "TOTP", { name => "test" } ); |
| 620 |
|
| 621 |
checkAddWithBadAttributes( |
| 622 |
"Add/badkey", "amy", "TOTP", |
| 623 |
{ name => "test", key => "123xxx" }, |
| 624 |
qr/Invalid secret/ |
| 625 |
); |
| 626 |
|
| 627 |
checkAdd( "Add/goodkey", "amy", "TOTP", |
| 628 |
{ name => "test", key => "GEZDGNBVGY3TQOJQ GEZDGNBVG Y3TQOJQ " } ); |
| 629 |
checkGetList( 1, 'amy', 'TOTP' ); |
| 630 |
|
| 631 |
checkTotpData( |
| 632 |
get2fDevices($amypsession)->[0], |
| 633 |
"gezdgnbvgy3tqojqgezdgnbvgy3tqojq", |
| 634 |
name => "test" |
| 635 |
); |
| 636 |
|
| 637 |
# 2FA add (TOTP) with undef 2fDevices |
| 638 |
newSession( 'rory', '127.10.0.1', 'SSO', undef ); |
| 639 |
my $rorypsession = newSession( 'rory', '127.10.0.1', 'Persistent', undef ); |
| 640 |
|
| 641 |
checkAdd( "Add/goodkey", "rory", "TOTP", |
| 642 |
{ name => "test", key => "GEZDGNBVGY3TQOJQ GEZDGNBVG Y3TQOJQ " } ); |
| 643 |
checkGetList( 1, 'rory', 'TOTP' ); |
| 644 |
|
| 645 |
checkTotpData( |
| 646 |
get2fDevices($rorypsession)->[0], |
| 647 |
"gezdgnbvgy3tqojqgezdgnbvgy3tqojq", |
| 648 |
name => "test" |
| 649 |
); |
| 650 |
|
| 651 |
# 2FA add with nonexisting session |
| 652 |
checkAddWithUnknownUser( "Add/missinguser", "unknowng", undef, |
| 653 |
{ type => "test", name => "test" } ); |
| 654 |
checkAddWithUnknownUser( "Add/missinguser", "unknownt", "TOTP", |
| 655 |
{ type => "test", key => "GEZDGNBVGY3TQOJQ GEZDGNBVG Y3TQOJQ " } ); |
| 656 |
checkAddWithUnknownUser( "Add/missinguser", "unknowng", undef, |
| 657 |
{ type => "test", name => "test" }, |
| 658 |
"create=false" ); |
| 659 |
checkAddWithUnknownUser( "Add/missinguser", "unknownt", "TOTP", |
| 660 |
{ type => "test", key => "GEZDGNBVGY3TQOJQ GEZDGNBVG Y3TQOJQ " }, |
| 661 |
"create=false" ); |
| 662 |
|
| 663 |
checkAdd( "Add/missinguser", "unknowng", undef, |
| 664 |
{ type => "test", name => "test" }, |
| 665 |
"create=true" ); |
| 666 |
checkGetList( 1, 'unknowng', 'test' ); |
| 667 |
|
| 668 |
checkAdd( "Add/missinguser", "unknownt", "TOTP", |
| 669 |
{ name => "test", key => "GEZDGNBVGY3TQOJQ GEZDGNBVG Y3TQOJQ " }, |
| 670 |
"create=true" ); |
| 671 |
checkGetList( 1, 'unknownt', 'TOTP' ); |
| 672 |
|
| 673 |
# 2FA search |
| 674 |
|
| 675 |
unlink glob $main::tmpdir . '/sessions/*'; |
| 676 |
|
| 677 |
checkSearchNotFound; |
| 678 |
|
| 679 |
# Populate some test factors |
| 680 |
newSession( |
| 681 |
'dwho', |
| 682 |
'127.10.0.1', |
| 683 |
'Persistent', |
| 684 |
[ { |
| 685 |
"name" => "MyTOTP", |
| 686 |
"type" => "TOTP", |
| 687 |
"epoch" => "1643201784", |
| 688 |
}, |
| 689 |
{ |
| 690 |
"epoch" => "1643201784", |
| 691 |
"name" => "MyFidoKey", |
| 692 |
"type" => "WebAuthn" |
| 693 |
}, |
| 694 |
] |
| 695 |
); |
| 696 |
newSession( |
| 697 |
'rtyler', |
| 698 |
'127.10.0.1', |
| 699 |
'Persistent', |
| 700 |
[ { |
| 701 |
"name" => "MyTOTP", |
| 702 |
"type" => "TOTP", |
| 703 |
"epoch" => "1643201784", |
| 704 |
} |
| 705 |
] |
| 706 |
); |
| 707 |
newSession( |
| 708 |
'mjones', |
| 709 |
'127.10.0.1', |
| 710 |
'Persistent', |
| 711 |
[ { |
| 712 |
"name" => "MyUBK", |
| 713 |
"type" => "Yubikey", |
| 714 |
"epoch" => "1643201784", |
| 715 |
} |
| 716 |
] |
| 717 |
); |
| 718 |
newSession( 'msmith', '127.10.0.1', 'Persistent', [] ); |
| 719 |
|
| 720 |
checkSearch( "", [qw/dwho mjones rtyler/] ); |
| 721 |
my $result = checkSearch( "type=TOTP", [qw/dwho rtyler/] ); |
| 722 |
is_deeply( |
| 723 |
$result, |
| 724 |
|
| 725 |
[ { |
| 726 |
'secondFactors' => [ { |
| 727 |
'id' => 'MTY0MzIwMTc4NDo6VE9UUDo6TXlUT1RQ', |
| 728 |
'name' => 'MyTOTP', |
| 729 |
'type' => 'TOTP' |
| 730 |
}, |
| 731 |
{ |
| 732 |
'id' => 'MTY0MzIwMTc4NDo6V2ViQXV0aG46Ok15Rmlkb0tleQ==', |
| 733 |
'name' => 'MyFidoKey', |
| 734 |
'type' => 'WebAuthn' |
| 735 |
} |
| 736 |
], |
| 737 |
'uid' => 'dwho' |
| 738 |
}, |
| 739 |
{ |
| 740 |
'secondFactors' => [ { |
| 741 |
'id' => 'MTY0MzIwMTc4NDo6VE9UUDo6TXlUT1RQ', |
| 742 |
'name' => 'MyTOTP', |
| 743 |
'type' => 'TOTP' |
| 744 |
} |
| 745 |
], |
| 746 |
'uid' => 'rtyler' |
| 747 |
} |
| 748 |
], |
| 749 |
"Expected API response" |
| 750 |
); |
| 751 |
|
| 752 |
checkSearch( "uid=m*", [qw/mjones/] ); |
| 753 |
checkSearch( "uid=m*&type=Yubikey", [qw/mjones/] ); |
| 754 |
checkSearchNotFound("uid=m*&type=TOTP"); |
| 755 |
checkSearch( "uid=dwho&type=TOTP", [qw/dwho/] ); |
| 756 |
checkSearchNotFound("uid=dwho&type=Yubikey"); |
| 757 |
checkSearch( "type=TOTP&type=WebAuthn", [qw/dwho/] ); |
| 758 |
|
| 759 |
done_testing(); |