| 1 |
use warnings; |
| 2 |
|
| 3 |
# Before `make install' is performed this script should be runnable with |
| 4 |
# `make test'. After `make install' it should work as `perl Lemonldap-NG-Manager.t' |
| 5 |
|
| 6 |
######################### |
| 7 |
|
| 8 |
# change 'tests => 1' to 'tests => last_test_to_print'; |
| 9 |
|
| 10 |
use Test::More; |
| 11 |
use Test::Output; |
| 12 |
use File::Path; |
| 13 |
use JSON; |
| 14 |
|
| 15 |
BEGIN { |
| 16 |
use_ok('Lemonldap::NG::Common::Session'); |
| 17 |
use_ok('Lemonldap::NG::Common::CliSessions'); |
| 18 |
} |
| 19 |
|
| 20 |
######################### |
| 21 |
|
| 22 |
# Insert your test code below, the Test::More module is used here so read |
| 23 |
# its man page ( perldoc Test::More ) for help writing this test script. |
| 24 |
|
| 25 |
use File::Temp; |
| 26 |
my $dir = File::Temp::tempdir(); |
| 27 |
my $sessionsdir = "$dir/sessions"; |
| 28 |
my $psessionsdir = "$dir/psessions"; |
| 29 |
mkdir $sessionsdir; |
| 30 |
mkdir $psessionsdir; |
| 31 |
my $backupFile = "$dir/backup.json"; |
| 32 |
|
| 33 |
my $cli = Lemonldap::NG::Common::CliSessions->new( |
| 34 |
conf => { |
| 35 |
globalStorage => "Apache::Session::File", |
| 36 |
globalStorageOptions => { |
| 37 |
Directory => $sessionsdir, |
| 38 |
LockDirectory => $sessionsdir, |
| 39 |
}, |
| 40 |
persistentStorage => "Apache::Session::File", |
| 41 |
persistentStorageOptions => { |
| 42 |
Directory => $psessionsdir, |
| 43 |
LockDirectory => $psessionsdir, |
| 44 |
}, |
| 45 |
} |
| 46 |
); |
| 47 |
|
| 48 |
sub backup { |
| 49 |
my $file = shift; |
| 50 |
open F, '>', $file or die $!; |
| 51 |
Lemonldap::NG::Common::CliSessions->new( |
| 52 |
conf => { |
| 53 |
globalStorage => "Apache::Session::File", |
| 54 |
globalStorageOptions => { |
| 55 |
Directory => $sessionsdir, |
| 56 |
LockDirectory => $sessionsdir, |
| 57 |
}, |
| 58 |
persistentStorage => "Apache::Session::File", |
| 59 |
persistentStorageOptions => { |
| 60 |
Directory => $psessionsdir, |
| 61 |
LockDirectory => $psessionsdir, |
| 62 |
}, |
| 63 |
}, |
| 64 |
stdout => *F, |
| 65 |
)->run( "backup", {} ); |
| 66 |
close F; |
| 67 |
} |
| 68 |
|
| 69 |
# Provision test sessions |
| 70 |
my @sessionsOpts = ( |
| 71 |
storageModule => "Apache::Session::File", |
| 72 |
storageModuleOptions => { |
| 73 |
Directory => $sessionsdir, |
| 74 |
LockDirectory => $sessionsdir, |
| 75 |
}, |
| 76 |
kind => 'SSO', |
| 77 |
force => 1, |
| 78 |
); |
| 79 |
my @psessionsOpts = ( |
| 80 |
storageModule => "Apache::Session::File", |
| 81 |
storageModuleOptions => { |
| 82 |
Directory => $psessionsdir, |
| 83 |
LockDirectory => $psessionsdir, |
| 84 |
}, |
| 85 |
kind => 'Persistent', |
| 86 |
force => 1, |
| 87 |
); |
| 88 |
|
| 89 |
sub resetSessions { |
| 90 |
Lemonldap::NG::Common::Session->new( { |
| 91 |
@sessionsOpts, |
| 92 |
id => "1b3231655cebb7a1f783eddf27d254ca", |
| 93 |
info => { |
| 94 |
"uid" => "rtyler", |
| 95 |
} |
| 96 |
} |
| 97 |
); |
| 98 |
Lemonldap::NG::Common::Session->new( { |
| 99 |
@sessionsOpts, |
| 100 |
id => "9684dd2a6489bf2be2fbdd799a8028e3", |
| 101 |
info => { |
| 102 |
"uid" => "dwho", |
| 103 |
} |
| 104 |
} |
| 105 |
); |
| 106 |
Lemonldap::NG::Common::Session->new( { |
| 107 |
@sessionsOpts, |
| 108 |
id => "f90f597566f5cce47d9641377776c0c2", |
| 109 |
info => { |
| 110 |
"uid" => "dwho", |
| 111 |
"deleteme" => 1, |
| 112 |
} |
| 113 |
} |
| 114 |
); |
| 115 |
Lemonldap::NG::Common::Session->new( { |
| 116 |
@sessionsOpts, |
| 117 |
id => "1234", |
| 118 |
info => { |
| 119 |
"uid" => "foo", |
| 120 |
} |
| 121 |
} |
| 122 |
); |
| 123 |
Lemonldap::NG::Common::Session->new( { |
| 124 |
@sessionsOpts, |
| 125 |
id => "1235", |
| 126 |
info => { |
| 127 |
"uid" => "foo", |
| 128 |
} |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
Lemonldap::NG::Common::Session->new( { |
| 133 |
@psessionsOpts, |
| 134 |
id => "5efe8af397fc3577e05b483aca964f1b", |
| 135 |
force => 1, |
| 136 |
info => { |
| 137 |
"_2fDevices" => to_json( [ { |
| 138 |
'type' => 'UBK', |
| 139 |
'epoch' => 1588691690, |
| 140 |
'_yubikey' => 'cccccceijfnf', |
| 141 |
'name' => 'Imported automatically' |
| 142 |
}, |
| 143 |
{ |
| 144 |
'name' => 'MyU2F', |
| 145 |
'type' => 'U2F', |
| 146 |
'epoch' => 1588691728 |
| 147 |
}, |
| 148 |
{ |
| 149 |
'_secret' => 'mnxkiirpswuojr47kkrty7ax34fy2ix7', |
| 150 |
'name' => 'MyTOTP', |
| 151 |
'type' => 'TOTP', |
| 152 |
'epoch' => 1588691728 |
| 153 |
} |
| 154 |
] |
| 155 |
), |
| 156 |
"_oidcConsents" => to_json( [ { |
| 157 |
'scope' => 'openid email', |
| 158 |
'rp' => 'rp-example', |
| 159 |
'epoch' => 1589288341 |
| 160 |
}, |
| 161 |
{ |
| 162 |
'scope' => 'openid email', |
| 163 |
'epoch' => 1589291482, |
| 164 |
'rp' => 'rp-example2' |
| 165 |
} |
| 166 |
] |
| 167 |
), |
| 168 |
"_loginHistory" => { |
| 169 |
'successLogin' => [ { |
| 170 |
'_utime' => 1710239445, |
| 171 |
'ipAddr' => '192.168.1.1', |
| 172 |
}, |
| 173 |
{ |
| 174 |
'_utime' => 1710153045, |
| 175 |
'ipAddr' => '10.0.0.5', |
| 176 |
}, |
| 177 |
], |
| 178 |
'failedLogin' => [ { |
| 179 |
'_utime' => 1710235522, |
| 180 |
'ipAddr' => '192.168.1.100', |
| 181 |
'error' => 5, |
| 182 |
}, |
| 183 |
], |
| 184 |
}, |
| 185 |
"_session_uid" => "dwho", |
| 186 |
} |
| 187 |
} |
| 188 |
); |
| 189 |
Lemonldap::NG::Common::Session->new( { |
| 190 |
@psessionsOpts, |
| 191 |
id => "8d3bc3b0e14ea2a155f275aa7c07ebee", |
| 192 |
force => 1, |
| 193 |
info => { |
| 194 |
"_session_uid" => "rtyler", |
| 195 |
"_2fDevices" => to_json( [ { |
| 196 |
'type' => 'UBK', |
| 197 |
'epoch' => 1588691690, |
| 198 |
'_yubikey' => 'cccccceijfnf', |
| 199 |
'name' => 'Imported automatically' |
| 200 |
}, |
| 201 |
{ |
| 202 |
'_secret' => 'mnxkiirpswuojr47kkrty7ax34fy2ix7', |
| 203 |
'name' => 'MyTOTP', |
| 204 |
'type' => 'TOTP', |
| 205 |
'epoch' => 1588691728 |
| 206 |
} |
| 207 |
] |
| 208 |
), |
| 209 |
} |
| 210 |
} |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
resetSessions; |
| 215 |
|
| 216 |
sub getJson { |
| 217 |
my @args = @_; |
| 218 |
my ($str) = Test::Output::output_from( sub { $cli->run(@args); } ); |
| 219 |
return $str =~ /^([\w]+)$/ ? $1 : from_json($str); |
| 220 |
} |
| 221 |
|
| 222 |
sub getLines { |
| 223 |
my @args = @_; |
| 224 |
my ($str) = Test::Output::output_from( sub { $cli->run(@args); } ); |
| 225 |
return [ split /\n/, $str ]; |
| 226 |
} |
| 227 |
|
| 228 |
my $res; |
| 229 |
|
| 230 |
# Test get |
| 231 |
$res = getJson( "get", {}, "f90f597566f5cce47d9641377776c0c2" ); |
| 232 |
|
| 233 |
is( @{$res}, 1, "Found one session" ); |
| 234 |
is( |
| 235 |
$res->[0]->{_session_id}, |
| 236 |
"f90f597566f5cce47d9641377776c0c2", |
| 237 |
"Found correct session ID" |
| 238 |
); |
| 239 |
is( $res->[0]->{deleteme}, 1, "Found deleteme session key" ); |
| 240 |
|
| 241 |
# Change backend |
| 242 |
$res = getJson( |
| 243 |
"get", |
| 244 |
{ backend => 'persistent' }, |
| 245 |
"5efe8af397fc3577e05b483aca964f1b" |
| 246 |
); |
| 247 |
is( @{$res}, 1, "Found one session" ); |
| 248 |
is( $res->[0]->{_session_uid}, 'dwho', "Found correct session" ); |
| 249 |
|
| 250 |
# Persistent mode |
| 251 |
$res = getJson( "get", { persistent => 1 }, "dwho" ); |
| 252 |
is( @{$res}, 1, "Found one session" ); |
| 253 |
is( $res->[0]->{_session_uid}, 'dwho', "Found correct session" ); |
| 254 |
|
| 255 |
# Test output field selection |
| 256 |
$res = getJson( |
| 257 |
"get", |
| 258 |
{ select => [ "uid", "_session_id" ] }, |
| 259 |
"f90f597566f5cce47d9641377776c0c2" |
| 260 |
); |
| 261 |
|
| 262 |
is( keys %{ $res->[0] }, 2, "Only selected fields returned" ); |
| 263 |
is( $res->[0]->{uid}, "dwho", "Found correct UID" ); |
| 264 |
is( |
| 265 |
$res->[0]->{_session_id}, |
| 266 |
"f90f597566f5cce47d9641377776c0c2", |
| 267 |
"Found correct session ID" |
| 268 |
); |
| 269 |
|
| 270 |
# Test search |
| 271 |
$res = getJson( "search", {} ); |
| 272 |
is( @{$res}, 5, "Found 5 sessions" ); |
| 273 |
|
| 274 |
$res = getJson( "search", { count => 1 } ); |
| 275 |
is( $res, 5, "Count gives 5 sessions" ); |
| 276 |
|
| 277 |
sub searchTest { |
| 278 |
|
| 279 |
# Test search with different backend |
| 280 |
$res = getJson( "search", { backend => 'persistent' } ); |
| 281 |
is( @{$res}, 2, "Found 2 psessions" ); |
| 282 |
|
| 283 |
$res = getJson( "search", { backend => 'persistent', count => 1 } ); |
| 284 |
is( $res, 2, "Count gives 2 psessions" ); |
| 285 |
|
| 286 |
# Persistent mode |
| 287 |
$res = getJson( "search", { persistent => 1 } ); |
| 288 |
is( @{$res}, 2, "Found 2 psessions" ); |
| 289 |
|
| 290 |
# Test search with where |
| 291 |
$res = getJson( "search", { where => "uid=dwho" } ); |
| 292 |
is( @{$res}, 2, "Found 2 sessions" ); |
| 293 |
is( ( grep { $_->{uid} eq "dwho" } @{$res} ), 2, "Both sessions are dwho" ); |
| 294 |
|
| 295 |
# Test search with where and field selection |
| 296 |
$res = getJson( "search", |
| 297 |
{ where => "uid=dwho", select => [ "uid", "_session_id" ] } ); |
| 298 |
is( @{$res}, 2, "Found 2 sessions" ); |
| 299 |
is( keys %{ $res->[0] }, 2, "Only selected fields returned" ); |
| 300 |
|
| 301 |
# Test search with ID output |
| 302 |
$res = getLines( "search", { where => "uid=dwho", idonly => 1 } ); |
| 303 |
is( @{$res}, 2, "Got two lines" ); |
| 304 |
is( |
| 305 |
( join ':', sort @{$res} ), |
| 306 |
"9684dd2a6489bf2be2fbdd799a8028e3:f90f597566f5cce47d9641377776c0c2", |
| 307 |
"Correct session IDs" |
| 308 |
); |
| 309 |
} |
| 310 |
subtest 'Search', \&searchTest; |
| 311 |
|
| 312 |
# Backup sessions |
| 313 |
backup($backupFile); |
| 314 |
|
| 315 |
# Delete session |
| 316 |
$cli->run( 'delete', {}, "9684dd2a6489bf2be2fbdd799a8028e3" ); |
| 317 |
$cli->run( 'delete', { persistent => 1 }, "rtyler" ); |
| 318 |
|
| 319 |
$res = getJson( "get", {}, "9684dd2a6489bf2be2fbdd799a8028e3" ); |
| 320 |
is( @{$res}, 0, "Session was removed" ); |
| 321 |
$res = getJson( |
| 322 |
"get", |
| 323 |
{ backend => 'persistent' }, |
| 324 |
"8d3bc3b0e14ea2a155f275aa7c07ebee" |
| 325 |
); |
| 326 |
is( @{$res}, 0, "Session was removed" ); |
| 327 |
|
| 328 |
# We should have 2 foo sessions now |
| 329 |
$res = getJson( "search", { where => "uid=foo" } ); |
| 330 |
is( @{$res}, 2, "Found 2 foo sessions" ); |
| 331 |
|
| 332 |
# Test delete by filter, remove two foo sessions |
| 333 |
$cli->run( 'delete', { where => "uid=foo" } ); |
| 334 |
|
| 335 |
# We should have no foo sessions left |
| 336 |
$res = getJson( "search", { where => "uid=foo" } ); |
| 337 |
is( @{$res}, 0, "Found 0 foo sessions" ); |
| 338 |
|
| 339 |
# Set key |
| 340 |
|
| 341 |
$cli->run( "setKey", {}, "f90f597566f5cce47d9641377776c0c2", |
| 342 |
"key1", "value1", "deleteme", "newvalue" ); |
| 343 |
$res = getJson( "get", {}, "f90f597566f5cce47d9641377776c0c2" ); |
| 344 |
is( $res->[0]->{key1}, "value1", "New key was set" ); |
| 345 |
is( $res->[0]->{deleteme}, "newvalue", "Existing key was changed" ); |
| 346 |
|
| 347 |
# Delete key |
| 348 |
$cli->run( "delKey", {}, "f90f597566f5cce47d9641377776c0c2", |
| 349 |
"key1", "deleteme", "missing" ); |
| 350 |
$res = getJson( "get", {}, "f90f597566f5cce47d9641377776c0c2" ); |
| 351 |
is( $res->[0]->{key1}, undef, "Key was removed" ); |
| 352 |
is( $res->[0]->{deleteme}, undef, "Key was removed" ); |
| 353 |
|
| 354 |
# Show 2FA |
| 355 |
|
| 356 |
$res = getJson( "secondfactors", {}, "get", "dwho" ); |
| 357 |
is( ( keys %{$res} ), 3, "Found two second factors" ); |
| 358 |
is( ( grep { $_->{type} eq "UBK" } values %{$res} ), 1, "Found one Yubikey" ); |
| 359 |
is( ( grep { $_->{type} eq "TOTP" } values %{$res} ), 1, "Found one TOTP" ); |
| 360 |
is( ( grep { $_->{type} eq "U2F" } values %{$res} ), 1, "Found one U2F" ); |
| 361 |
|
| 362 |
# Delete 2FA |
| 363 |
$cli->run( "secondfactors", {}, "delete", "dwho", |
| 364 |
"MTU4ODY5MTY5MDo6VUJLOjpJbXBvcnRlZCBhdXRvbWF0aWNhbGx5" ); |
| 365 |
$res = getJson( "secondfactors", {}, "get", "dwho" ); |
| 366 |
is( ( keys %{$res} ), 2, "Found two second factors" ); |
| 367 |
is( ( grep { $_->{type} eq "UBK" } values %{$res} ), 0, "Yubikey was removed" ); |
| 368 |
|
| 369 |
# Delete 2FA by type |
| 370 |
$cli->run( "secondfactors", {}, "delType", "dwho", "U2F" ); |
| 371 |
$res = getJson( "secondfactors", {}, "get", "dwho" ); |
| 372 |
is( ( keys %{$res} ), 1, "Found one second factors" ); |
| 373 |
is( ( grep { $_->{type} eq "U2F" } values %{$res} ), 0, "U2F was removed" ); |
| 374 |
is( ( grep { $_->{type} eq "TOTP" } values %{$res} ), 1, "TOTP survived" ); |
| 375 |
|
| 376 |
$cli->run( "restore", {}, $backupFile ); |
| 377 |
subtest 'Search after restore', \&searchTest; |
| 378 |
|
| 379 |
# Delete 2FA by type (with search) |
| 380 |
resetSessions; |
| 381 |
$cli->run( "secondfactors", { where => "_session_uid=dwho" }, "delType", |
| 382 |
"U2F" ); |
| 383 |
$res = getJson( "secondfactors", {}, "get", "dwho" ); |
| 384 |
is( ( keys %{$res} ), 2, "Found one second factors" ); |
| 385 |
is( ( grep { $_->{type} eq "U2F" } values %{$res} ), 0, "U2F was removed" ); |
| 386 |
is( ( grep { $_->{type} eq "TOTP" } values %{$res} ), 1, "TOTP survived" ); |
| 387 |
|
| 388 |
# Delete 2FA by type (with all) |
| 389 |
resetSessions; |
| 390 |
$cli->run( "secondfactors", { all => 1 }, "delType", "TOTP" ); |
| 391 |
$res = getJson( "secondfactors", {}, "get", "dwho" ); |
| 392 |
is( ( keys %{$res} ), 2, "Found two second factors for dwho" ); |
| 393 |
is( ( grep { $_->{type} eq "TOTP" } values %{$res} ), 0, "TOTP was removed" ); |
| 394 |
is( ( grep { $_->{type} eq "UBK" } values %{$res} ), 1, "UBK survived" ); |
| 395 |
$res = getJson( "secondfactors", {}, "get", "rtyler" ); |
| 396 |
is( ( keys %{$res} ), 1, "Found one second factors for rtyler" ); |
| 397 |
is( ( grep { $_->{type} eq "TOTP" } values %{$res} ), 0, "TOTP was removed" ); |
| 398 |
is( ( grep { $_->{type} eq "UBK" } values %{$res} ), 1, "UBK survived" ); |
| 399 |
|
| 400 |
# Show consents |
| 401 |
$res = getJson( "consents", {}, "get", "dwho" ); |
| 402 |
is( ( keys %{$res} ), 2, "Found two consents" ); |
| 403 |
|
| 404 |
# Delete consents |
| 405 |
|
| 406 |
$cli->run( "consents", {}, "delete", "dwho", "rp-example" ); |
| 407 |
$res = getJson( "consents", {}, "get", "dwho" ); |
| 408 |
is( ( keys %{$res} ), 1, "Found one consent" ); |
| 409 |
is( $res->{'rp-example'}, undef, "Consent for test-rp removed" ); |
| 410 |
ok( $res->{'rp-example2'}, "Consent for test-rp2 still present" ); |
| 411 |
|
| 412 |
# History tests |
| 413 |
resetSessions; |
| 414 |
subtest 'History commands' => sub { |
| 415 |
|
| 416 |
# Test history lastsuccessdate |
| 417 |
my ( $stdout, $stderr ) = Test::Output::output_from( |
| 418 |
sub { $cli->run( "history", {}, "lastsuccessdate", "dwho" ); } ); |
| 419 |
like( $stdout, qr/Mar.*2024/, "lastsuccessdate shows date" ); |
| 420 |
is( $stderr, '', "No error on lastsuccessdate" ); |
| 421 |
|
| 422 |
# Test history lastfailuredate |
| 423 |
( $stdout, $stderr ) = Test::Output::output_from( |
| 424 |
sub { $cli->run( "history", {}, "lastfailuredate", "dwho" ); } ); |
| 425 |
like( $stdout, qr/Mar.*2024.*\(5\)/, |
| 426 |
"lastfailuredate shows date with error code" ); |
| 427 |
is( $stderr, '', "No error on lastfailuredate" ); |
| 428 |
|
| 429 |
# Test history get (JSON output) |
| 430 |
$res = getJson( "history", {}, "get", "dwho" ); |
| 431 |
ok( $res->{successLogin}, "history get has successLogin" ); |
| 432 |
ok( $res->{failedLogin}, "history get has failedLogin" ); |
| 433 |
is( @{ $res->{successLogin} }, 2, "Found 2 successful logins" ); |
| 434 |
is( @{ $res->{failedLogin} }, 1, "Found 1 failed login" ); |
| 435 |
|
| 436 |
# Check all fields are present |
| 437 |
ok( $res->{successLogin}[0]{_utime}, "successLogin has _utime" ); |
| 438 |
ok( $res->{successLogin}[0]{ipAddr}, "successLogin has ipAddr" ); |
| 439 |
ok( $res->{successLogin}[0]{date}, "successLogin has human-readable date" ); |
| 440 |
is( $res->{failedLogin}[0]{error}, 5, "failedLogin has error code" ); |
| 441 |
|
| 442 |
# Test error case: user without history |
| 443 |
( $stdout, $stderr ) = Test::Output::output_from( |
| 444 |
sub { $cli->run( "history", {}, "lastsuccessdate", "rtyler" ); } ); |
| 445 |
is( $stdout, '', "No stdout on missing history" ); |
| 446 |
like( $stderr, qr/No login history found/, "Error message on stderr" ); |
| 447 |
}; |
| 448 |
|
| 449 |
rmtree $dir; |
| 450 |
done_testing(); |