| 1 |
package Lemonldap::NG::Common::CliSessions; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Mouse; |
| 5 |
use JSON; |
| 6 |
use Lemonldap::NG::Common::Conf; |
| 7 |
use Lemonldap::NG::Common::Logger::Std; |
| 8 |
use Lemonldap::NG::Common::Apache::Session; |
| 9 |
use Lemonldap::NG::Common::Session; |
| 10 |
use Lemonldap::NG::Common::Util qw/getPSessionID genId2F/; |
| 11 |
|
| 12 |
use constant BACKENDS => qw(global persistent oidc saml cas); |
| 13 |
use constant ISREFRESHTOKEN => '_type=refresh_token'; |
| 14 |
|
| 15 |
our $VERSION = '2.23.0'; |
| 16 |
|
| 17 |
has opts => ( is => 'rw' ); |
| 18 |
|
| 19 |
has stdout => ( |
| 20 |
is => 'ro', |
| 21 |
default => *STDOUT, |
| 22 |
); |
| 23 |
|
| 24 |
has stderr => ( |
| 25 |
is => 'ro', |
| 26 |
default => *STDERR, |
| 27 |
); |
| 28 |
|
| 29 |
has conf => ( |
| 30 |
is => 'ro', |
| 31 |
default => sub { |
| 32 |
my $ca = Lemonldap::NG::Common::Conf->new( { ( |
| 33 |
ref $_[0] && $_[0]->{iniFile} |
| 34 |
? ( confFile => $_[0]->{iniFile} ) |
| 35 |
: () |
| 36 |
) |
| 37 |
} |
| 38 |
); |
| 39 |
die $Lemonldap::NG::Common::Conf::msg unless ($ca); |
| 40 |
my $res = $ca->getConf('all'); |
| 41 |
my $localConf = $ca->getLocalConf( 'all', $_[0]->{iniFile} ); |
| 42 |
return { %$res, %$localConf }; |
| 43 |
}, |
| 44 |
); |
| 45 |
|
| 46 |
sub _to_json { |
| 47 |
my $self = shift; |
| 48 |
my $obj = shift; |
| 49 |
return to_json( $obj, { pretty => 1, canonical => 1 } ); |
| 50 |
} |
| 51 |
|
| 52 |
sub _search { |
| 53 |
my ($self) = shift; |
| 54 |
my $backendStorage = |
| 55 |
( lc( $self->opts->{backend} || 'global' ) ) . "Storage"; |
| 56 |
|
| 57 |
# Handle --persistent |
| 58 |
if ( $self->opts->{persistent} ) { |
| 59 |
$backendStorage = "persistentStorage"; |
| 60 |
} |
| 61 |
elsif ( $self->opts->{refreshTokens} ) { |
| 62 |
$backendStorage = "oidcStorage"; |
| 63 |
$self->opts->{where} ||= ISREFRESHTOKEN; |
| 64 |
} |
| 65 |
|
| 66 |
unless ( $self->conf->{$backendStorage} ) { |
| 67 |
print STDERR "Backend " |
| 68 |
. $self->opts->{backend} |
| 69 |
. " was not found, falling back to global\n"; |
| 70 |
$backendStorage = "globalStorage"; |
| 71 |
} |
| 72 |
|
| 73 |
my $args = $self->conf->{"${backendStorage}Options"}; |
| 74 |
$args->{backend} = $self->conf->{$backendStorage}; |
| 75 |
|
| 76 |
my @fields = @{ $self->opts->{select} || [] }; |
| 77 |
if ( $self->opts->{idonly} ) { |
| 78 |
@fields = ('_session_id'); |
| 79 |
} |
| 80 |
|
| 81 |
my $res; |
| 82 |
if ( $self->opts->{where} ) { |
| 83 |
|
| 84 |
# TODO fix regexp? |
| 85 |
if ( $self->opts->{where} =~ /^(\w+)\s*=\s*(.*)/ ) { |
| 86 |
my ( $selectField, $value ) = ( $1, $2 ); |
| 87 |
$res = Lemonldap::NG::Common::Apache::Session->searchOn( $args, |
| 88 |
$selectField, $value, @fields ); |
| 89 |
} |
| 90 |
elsif ( $self->opts->{where} =~ /^(\w+)\s*<\s*(.*)/ ) { |
| 91 |
$res = |
| 92 |
Lemonldap::NG::Common::Apache::Session->searchLt( $args, $1, $2, |
| 93 |
@fields ); |
| 94 |
} |
| 95 |
elsif ( $self->opts->{where} =~ /^(\w+)\s*>\s*(.*)/ ) { |
| 96 |
$res = |
| 97 |
Lemonldap::NG::Common::Apache::Session->searchGt( $args, $1, $2, |
| 98 |
@fields ); |
| 99 |
} |
| 100 |
else { |
| 101 |
die "Invalid --where option : " . $self->opts->{where}; |
| 102 |
} |
| 103 |
} |
| 104 |
else { |
| 105 |
$res = |
| 106 |
Lemonldap::NG::Common::Apache::Session->get_key_from_all_sessions( |
| 107 |
$args, ( @fields ? [@fields] : () ) ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( $self->opts->{refreshTokens} |
| 111 |
and $self->opts->{where} ne ISREFRESHTOKEN ) |
| 112 |
{ |
| 113 |
foreach my $key (%$res) { |
| 114 |
delete $res->{$key} |
| 115 |
unless $res->{$key}->{_type} |
| 116 |
and $res->{$key}->{_type} eq 'refresh_token'; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $res; |
| 121 |
} |
| 122 |
|
| 123 |
sub search { |
| 124 |
my ($self) = shift; |
| 125 |
my $res = $self->_search(); |
| 126 |
my $o = $self->stdout; |
| 127 |
if ( $self->opts->{count} ) { |
| 128 |
my @tmp = keys %$res; |
| 129 |
print $o scalar(@tmp) . "\n"; |
| 130 |
} |
| 131 |
else { |
| 132 |
if ( $self->opts->{idonly} ) { |
| 133 |
print $o map { $res->{$_}->{_session_id} . "\n" } keys %{$res}; |
| 134 |
} |
| 135 |
else { |
| 136 |
print $o $self->_to_json( [ values %{$res} ] ); |
| 137 |
} |
| 138 |
} |
| 139 |
return 0; |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
sub backup { |
| 144 |
my $self = shift; |
| 145 |
my $res = {}; |
| 146 |
if ( $self->opts->{persistent} ) { |
| 147 |
$res->{persistent} = $self->_search(); |
| 148 |
} |
| 149 |
elsif ( $self->opts->{refreshTokens} ) { |
| 150 |
$res->{oidc} = $self->_search(); |
| 151 |
} |
| 152 |
else { |
| 153 |
foreach my $backend (BACKENDS) { |
| 154 |
next unless $self->conf->{"${backend}Storage"}; |
| 155 |
$self->opts->{backend} = $backend; |
| 156 |
my $_res = $self->_search(); |
| 157 |
$res->{$backend} = $_res; |
| 158 |
} |
| 159 |
} |
| 160 |
my $o = $self->stdout; |
| 161 |
print $o to_json($res); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
sub restore { |
| 166 |
my $self = shift; |
| 167 |
my $obj; |
| 168 |
{ |
| 169 |
local $/ = undef; |
| 170 |
my $i = *STDIN; |
| 171 |
if (@_) { |
| 172 |
open( $i, '<', "$_[0]" ) or die $!; |
| 173 |
} |
| 174 |
while (<$i>) { |
| 175 |
$obj .= $_; |
| 176 |
} |
| 177 |
} |
| 178 |
eval { |
| 179 |
$obj = from_json($obj); |
| 180 |
no warnings; |
| 181 |
%$obj; |
| 182 |
}; |
| 183 |
if ($@) { |
| 184 |
print STDERR "Given backup file looks bad: $@\n"; |
| 185 |
return 1; |
| 186 |
} |
| 187 |
foreach my $backend ( keys %$obj ) { |
| 188 |
unless ( grep { $backend eq $_ } BACKENDS ) { |
| 189 |
print STDERR |
| 190 |
"Given backup file looks bad: unknpwn backend $backend\n"; |
| 191 |
} |
| 192 |
$self->opts->{backend} = $backend; |
| 193 |
foreach my $_session_id ( keys %{ $obj->{$backend} } ) { |
| 194 |
$self->_insert( $_session_id, $obj->{$backend}->{$_session_id} ); |
| 195 |
} |
| 196 |
} |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
sub _insert { |
| 201 |
my ( $self, $id, $content ) = @_; |
| 202 |
$id = id2storage($id) if $id and $self->opts->{hash}; |
| 203 |
|
| 204 |
# Lookup backend storage from CLI options |
| 205 |
my $backendStorage = |
| 206 |
( lc( $self->opts->{backend} || 'global' ) ) . "Storage"; |
| 207 |
|
| 208 |
my $as = Lemonldap::NG::Common::Session->new( { |
| 209 |
hashStore => 0, |
| 210 |
storageModule => $self->conf->{$backendStorage}, |
| 211 |
storageModuleOptions => $self->conf->{"${backendStorage}Options"}, |
| 212 |
id => $id, |
| 213 |
force => 1, |
| 214 |
info => $content, |
| 215 |
} |
| 216 |
); |
| 217 |
print STDERR "Unable to restore session $id in backend " |
| 218 |
. $self->opts->{backend} . "\n" |
| 219 |
unless $as; |
| 220 |
return $as; |
| 221 |
} |
| 222 |
|
| 223 |
# Returns the session object, so we can modify it |
| 224 |
sub _get_one_session { |
| 225 |
my ( $self, $id, $backend ) = @_; |
| 226 |
|
| 227 |
$id = id2storage($id) if $id and $self->opts->{hash}; |
| 228 |
|
| 229 |
# Lookup backend storage from CLI options |
| 230 |
my $backendStorage = |
| 231 |
( lc( $self->opts->{backend} || 'global' ) ) . "Storage"; |
| 232 |
|
| 233 |
# allow argument to overwrite the backend |
| 234 |
if ($backend) { |
| 235 |
$backendStorage = $backend . "Storage"; |
| 236 |
} |
| 237 |
|
| 238 |
# Handle --persistent |
| 239 |
elsif ( $self->opts->{persistent} ) { |
| 240 |
$backendStorage = "persistentStorage"; |
| 241 |
$id = getPSessionID($id); |
| 242 |
} |
| 243 |
elsif ( $self->opts->{refreshTokens} ) { |
| 244 |
$backendStorage = "oidcStorage"; |
| 245 |
} |
| 246 |
|
| 247 |
# In any case, fall back to global storage if we couldn't find the backend |
| 248 |
unless ( $self->conf->{$backendStorage} ) { |
| 249 |
print STDERR "Backend " |
| 250 |
. $self->opts->{backend} |
| 251 |
. " was not found, falling back to global\n"; |
| 252 |
$backendStorage = "globalStorage"; |
| 253 |
} |
| 254 |
|
| 255 |
my $as = Lemonldap::NG::Common::Session->new( { |
| 256 |
hashStore => 0, |
| 257 |
storageModule => $self->conf->{$backendStorage}, |
| 258 |
storageModuleOptions => $self->conf->{"${backendStorage}Options"}, |
| 259 |
id => $id, |
| 260 |
} |
| 261 |
); |
| 262 |
|
| 263 |
if ( $as->error ) { |
| 264 |
my $e = $self->stderr; |
| 265 |
print $e $as->error; |
| 266 |
return undef; |
| 267 |
} |
| 268 |
else { |
| 269 |
return $as; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
# Returns only session data |
| 274 |
sub _get_one_data { |
| 275 |
my ( $self, $id ) = @_; |
| 276 |
my $as = $self->_get_one_session($id); |
| 277 |
if ($as) { |
| 278 |
my $new; |
| 279 |
|
| 280 |
# Filter |
| 281 |
if ( ( ref( $self->opts->{select} ) eq 'ARRAY' ) |
| 282 |
and ( scalar @{ $self->opts->{select} } > 0 ) ) |
| 283 |
{ |
| 284 |
for ( @{ $self->opts->{select} } ) { |
| 285 |
$new->{$_} = $as->data->{$_} if defined $as->data->{$_}; |
| 286 |
} |
| 287 |
} |
| 288 |
else { |
| 289 |
$new = $as->data; |
| 290 |
} |
| 291 |
return $new; |
| 292 |
} |
| 293 |
return undef; |
| 294 |
} |
| 295 |
|
| 296 |
sub delete { |
| 297 |
my ($self) = shift; |
| 298 |
my $result = 0; |
| 299 |
my @sessions; |
| 300 |
|
| 301 |
# Run search if a where option was provided |
| 302 |
if ( $self->opts->{where} ) { |
| 303 |
my $res = $self->_search(); |
| 304 |
@sessions = keys %{$res}; |
| 305 |
} |
| 306 |
else { |
| 307 |
@sessions = @_; |
| 308 |
} |
| 309 |
|
| 310 |
my @result; |
| 311 |
for my $id (@sessions) { |
| 312 |
my $as = $self->_get_one_session($id); |
| 313 |
if ($as) { |
| 314 |
unless ( $as->remove ) { |
| 315 |
my $e = $self->stderr; |
| 316 |
print $e $as->error; |
| 317 |
$result = 1; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
return $result; |
| 322 |
} |
| 323 |
|
| 324 |
sub get { |
| 325 |
my $self = shift; |
| 326 |
my $o = $self->stdout; |
| 327 |
|
| 328 |
my @result; |
| 329 |
for my $id (@_) { |
| 330 |
my $new = $self->_get_one_data($id); |
| 331 |
push @result, $new if $new; |
| 332 |
} |
| 333 |
print $o $self->_to_json( \@result ); |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
sub _get_psession { |
| 338 |
my ( $self, $uid ) = @_; |
| 339 |
my $psession_id = getPSessionID($uid); |
| 340 |
my $res = $self->_get_one_session( $psession_id, 'persistent' ); |
| 341 |
die "Could not get psession for user $uid" unless $res; |
| 342 |
return $res; |
| 343 |
} |
| 344 |
|
| 345 |
sub _get_psession_data { |
| 346 |
my ( $self, $uid ) = @_; |
| 347 |
my $ps = $self->_get_psession($uid); |
| 348 |
return $ps->data; |
| 349 |
} |
| 350 |
|
| 351 |
# This method takes a special psession key (oidcConsents, 2fDevices..) |
| 352 |
# and returns the expected JSON object |
| 353 |
# idBuilder is a sub that gets applied to every object in the array, yielding the key of |
| 354 |
# this object in the resulting hash |
| 355 |
sub _get_psession_special { |
| 356 |
my ( $self, $target, $keyName, $idBuilder ) = @_; |
| 357 |
my $psession = $self->_get_psession_data($target); |
| 358 |
my $res = {}; |
| 359 |
my $special = $psession->{$keyName} || "[]"; |
| 360 |
$special = from_json($special); |
| 361 |
die "Expecting JSON array in $keyName" unless ref($special) eq "ARRAY"; |
| 362 |
for my $item ( @{$special} ) { |
| 363 |
my $id = $idBuilder->($item); |
| 364 |
$res->{$id} = $item; |
| 365 |
} |
| 366 |
return $res; |
| 367 |
} |
| 368 |
|
| 369 |
# This method deletes all matching items from an array psession key (oidcConsents, 2fDevices..) |
| 370 |
# keyBuilder is a sub that gets applied to every object in the array, yielding the value to be |
| 371 |
# compared against |
| 372 |
sub _del_psession_special { |
| 373 |
my ( $self, $target, $specialKeyName, $itemKeyBuilder, @todelete ) = @_; |
| 374 |
my $psession = $self->_get_psession($target); |
| 375 |
my $data = $psession->data; |
| 376 |
my $deleted = 0; |
| 377 |
|
| 378 |
my $special = $data->{$specialKeyName} || "[]"; |
| 379 |
$special = from_json($special); |
| 380 |
|
| 381 |
die "Expecting JSON array in $specialKeyName" |
| 382 |
unless ref($special) eq "ARRAY"; |
| 383 |
|
| 384 |
my @new; |
| 385 |
for my $item ( @{$special} ) { |
| 386 |
my $id = $itemKeyBuilder->($item); |
| 387 |
if ( $id and grep { $_ eq $id } @todelete ) { |
| 388 |
$deleted = $deleted + 1; |
| 389 |
} |
| 390 |
else { |
| 391 |
push @new, $item; |
| 392 |
} |
| 393 |
} |
| 394 |
if ($deleted) { |
| 395 |
$data->{$specialKeyName} = to_json( [@new] ); |
| 396 |
} |
| 397 |
|
| 398 |
# TODO should this be in the if??? |
| 399 |
$psession->update($data); |
| 400 |
} |
| 401 |
|
| 402 |
sub _migrateu2f_device { |
| 403 |
my ( $self, $device ) = @_; |
| 404 |
|
| 405 |
my $credential_id = $device->{_keyHandle}; |
| 406 |
my $_userKey = $device->{_userKey}; |
| 407 |
|
| 408 |
eval { require Authen::WebAuthn }; |
| 409 |
if ($@) { |
| 410 |
die "Missing Authen::WebAuthn dependency: $@"; |
| 411 |
} |
| 412 |
|
| 413 |
my $credential_pubkey = |
| 414 |
Authen::WebAuthn::convert_raw_ecc_to_cose($_userKey); |
| 415 |
|
| 416 |
return { |
| 417 |
type => "WebAuthn", |
| 418 |
name => "$device->{name}", |
| 419 |
_credentialId => "$credential_id", |
| 420 |
_credentialPublicKey => "$credential_pubkey", |
| 421 |
_signCount => 0, |
| 422 |
epoch => "$device->{epoch}", |
| 423 |
}; |
| 424 |
} |
| 425 |
|
| 426 |
sub _migrateu2f { |
| 427 |
my $self = shift; |
| 428 |
my $target = shift; |
| 429 |
|
| 430 |
my $psession = $self->_get_psession($target); |
| 431 |
my $data = $psession->data; |
| 432 |
my $migrated = 0; |
| 433 |
|
| 434 |
my $_2fDevices = $data->{_2fDevices} || "[]"; |
| 435 |
$_2fDevices = from_json($_2fDevices); |
| 436 |
|
| 437 |
die "Expecting JSON array in _2fDevices" |
| 438 |
unless ref($_2fDevices) eq "ARRAY"; |
| 439 |
|
| 440 |
my @new_2fDevices = @{$_2fDevices}; |
| 441 |
my @u2f_devices = grep { $_->{type} eq "U2F" } @{$_2fDevices}; |
| 442 |
|
| 443 |
my %migrated_devices; |
| 444 |
for my $u2f_device (@u2f_devices) { |
| 445 |
my $migrated_device = $self->_migrateu2f_device($u2f_device); |
| 446 |
$migrated_devices{ $migrated_device->{_credentialId} } = |
| 447 |
$migrated_device; |
| 448 |
} |
| 449 |
|
| 450 |
for my $migrated_device ( keys %migrated_devices ) { |
| 451 |
|
| 452 |
# If credentialId is not already present |
| 453 |
unless ( |
| 454 |
grep { |
| 455 |
$_->{type} eq "WebAuthn" |
| 456 |
and $_->{_credentialId} eq $migrated_device |
| 457 |
} @new_2fDevices |
| 458 |
) |
| 459 |
{ |
| 460 |
push @new_2fDevices, $migrated_devices{$migrated_device}; |
| 461 |
$migrated = 1; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if ($migrated) { |
| 466 |
$data->{_2fDevices} = to_json( [@new_2fDevices] ); |
| 467 |
$psession->update($data); |
| 468 |
} |
| 469 |
|
| 470 |
} |
| 471 |
|
| 472 |
sub consents_get { |
| 473 |
my $self = shift; |
| 474 |
my $target = shift; |
| 475 |
return 0 unless $target; |
| 476 |
|
| 477 |
my $o = $self->stdout; |
| 478 |
my $consents = $self->_get_psession_special( $target, '_oidcConsents', |
| 479 |
sub { $_[0]->{rp} } ); |
| 480 |
print $o $self->_to_json($consents); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
sub secondfactors_get { |
| 485 |
my $self = shift; |
| 486 |
my $target = shift; |
| 487 |
return 0 unless $target; |
| 488 |
|
| 489 |
my $o = $self->stdout; |
| 490 |
my $consents = $self->_get_psession_special( $target, '_2fDevices', |
| 491 |
sub { genId2F( $_[0] ) } ); |
| 492 |
print $o $self->_to_json($consents); |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
sub consents_delete { |
| 497 |
my $self = shift; |
| 498 |
my $target = shift; |
| 499 |
return 0 unless $target; |
| 500 |
|
| 501 |
my @ids = @_; |
| 502 |
return 0 unless @ids; |
| 503 |
|
| 504 |
$self->_del_psession_special( $target, '_oidcConsents', |
| 505 |
sub { $_[0]->{rp} }, @ids ); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
sub secondfactors_delete { |
| 510 |
my $self = shift; |
| 511 |
my $target = shift; |
| 512 |
return 0 unless $target; |
| 513 |
|
| 514 |
my @ids = @_; |
| 515 |
return 0 unless @ids; |
| 516 |
$self->_del_psession_special( $target, '_2fDevices', |
| 517 |
sub { genId2F( $_[0] ) }, @ids ); |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
sub _get_psession_targets { |
| 522 |
my ( $self, @args ) = @_; |
| 523 |
|
| 524 |
if ( $self->opts->{where} or $self->opts->{all} ) { |
| 525 |
$self->opts->{persistent} = 1; |
| 526 |
|
| 527 |
if ( $self->opts->{all} ) { |
| 528 |
delete $self->opts->{where}; |
| 529 |
} |
| 530 |
|
| 531 |
my $res = $self->_search(); |
| 532 |
return ( map { $res->{$_}->{_session_uid} } keys %{$res} ); |
| 533 |
} |
| 534 |
else { |
| 535 |
return @args; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
sub secondfactors_delType { |
| 540 |
my $self = shift; |
| 541 |
|
| 542 |
my $target; |
| 543 |
unless ( $self->opts->{where} or $self->opts->{all} ) { |
| 544 |
$target = shift; |
| 545 |
} |
| 546 |
|
| 547 |
my @types = @_; |
| 548 |
return 0 unless @types; |
| 549 |
|
| 550 |
my @targets = $self->_get_psession_targets($target); |
| 551 |
for my $target (@targets) { |
| 552 |
$self->_del_psession_special( $target, '_2fDevices', |
| 553 |
sub { $_[0]->{type} }, @types ); |
| 554 |
} |
| 555 |
|
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
sub secondfactors_migrateu2f { |
| 560 |
my ( $self, @ids ) = @_; |
| 561 |
my $result = 0; |
| 562 |
my @sessions = $self->_get_psession_targets(@ids); |
| 563 |
|
| 564 |
for my $id (@sessions) { |
| 565 |
if ( !$self->_migrateu2f($id) ) { |
| 566 |
$result = 1; |
| 567 |
} |
| 568 |
} |
| 569 |
return $result; |
| 570 |
} |
| 571 |
|
| 572 |
sub setKey { |
| 573 |
my $self = shift; |
| 574 |
my $id = shift; |
| 575 |
die "Unever number of arguments" unless ( @_ % 2 ) == 0; |
| 576 |
my %newvalues = (@_); |
| 577 |
die "Session ID and new values not provided" unless $id and %newvalues; |
| 578 |
|
| 579 |
my $as = $self->_get_one_session($id); |
| 580 |
die unless $as; |
| 581 |
|
| 582 |
my $data = $as->data; |
| 583 |
|
| 584 |
for ( keys %newvalues ) { |
| 585 |
$data->{$_} = $newvalues{$_}; |
| 586 |
} |
| 587 |
|
| 588 |
$as->update($data); |
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
sub delKey { |
| 593 |
my $self = shift; |
| 594 |
my $id = shift; |
| 595 |
my @todelete = @_; |
| 596 |
die "Session ID and key names not provided" unless $id and @todelete; |
| 597 |
|
| 598 |
my $as = $self->_get_one_session($id); |
| 599 |
die unless $as; |
| 600 |
|
| 601 |
my $data = $as->data; |
| 602 |
|
| 603 |
for (@todelete) { |
| 604 |
|
| 605 |
# Weird, I know. But that's how |
| 606 |
# Lemonldap::NG::Common::Session::update works |
| 607 |
$data->{$_} = undef; |
| 608 |
} |
| 609 |
|
| 610 |
$as->update($data); |
| 611 |
return 0; |
| 612 |
} |
| 613 |
|
| 614 |
sub _get_login_history { |
| 615 |
my ( $self, $uid ) = @_; |
| 616 |
|
| 617 |
my $psession_id = getPSessionID($uid); |
| 618 |
my $as = $self->_get_one_session( $psession_id, 'persistent' ); |
| 619 |
return undef unless $as; |
| 620 |
|
| 621 |
my $data = $as->data; |
| 622 |
my $login_history = $data->{_loginHistory}; |
| 623 |
return $login_history |
| 624 |
if $login_history && ref($login_history) eq 'HASH'; |
| 625 |
return undef; |
| 626 |
} |
| 627 |
|
| 628 |
sub _last_login_event { |
| 629 |
my ( $self, $uid, $key, $label ) = @_; |
| 630 |
die "User ID required" unless $uid; |
| 631 |
|
| 632 |
my $o = $self->stdout; |
| 633 |
my $e = $self->stderr; |
| 634 |
my $login_history = $self->_get_login_history($uid); |
| 635 |
|
| 636 |
unless ($login_history) { |
| 637 |
print $e "No login history found for user $uid\n"; |
| 638 |
return 1; |
| 639 |
} |
| 640 |
|
| 641 |
my $events = $login_history->{$key}; |
| 642 |
unless ( $events && ref($events) eq 'ARRAY' && @$events ) { |
| 643 |
print $e "No $label found for user $uid\n"; |
| 644 |
return 1; |
| 645 |
} |
| 646 |
|
| 647 |
my $last = $events->[0]; |
| 648 |
my $utime = $last->{_utime}; |
| 649 |
|
| 650 |
unless ($utime) { |
| 651 |
print $e "No timestamp found in last $label\n"; |
| 652 |
return 1; |
| 653 |
} |
| 654 |
|
| 655 |
my $msg = scalar( localtime($utime) ); |
| 656 |
$msg .= " ($last->{error})" if $last->{error}; |
| 657 |
print $o "$msg\n"; |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
sub history_lastsuccessdate { |
| 662 |
my ( $self, $uid ) = @_; |
| 663 |
return $self->_last_login_event( $uid, 'successLogin', 'successful login' ); |
| 664 |
} |
| 665 |
|
| 666 |
sub history_lastfailuredate { |
| 667 |
my ( $self, $uid ) = @_; |
| 668 |
return $self->_last_login_event( $uid, 'failedLogin', 'failed login' ); |
| 669 |
} |
| 670 |
|
| 671 |
sub history_get { |
| 672 |
my ( $self, $uid ) = @_; |
| 673 |
die "User ID required" unless $uid; |
| 674 |
|
| 675 |
my $o = $self->stdout; |
| 676 |
my $e = $self->stderr; |
| 677 |
my $login_history = $self->_get_login_history($uid); |
| 678 |
|
| 679 |
unless ($login_history) { |
| 680 |
print $e "No login history found for user $uid\n"; |
| 681 |
return 1; |
| 682 |
} |
| 683 |
|
| 684 |
my $result = {}; |
| 685 |
|
| 686 |
for my $key (qw(successLogin failedLogin)) { |
| 687 |
next unless my $events = $login_history->{$key}; |
| 688 |
$result->{$key} = [ |
| 689 |
map { |
| 690 |
{ |
| 691 |
%$_, |
| 692 |
date => scalar( localtime( $_->{_utime} ) ), |
| 693 |
} |
| 694 |
} @$events |
| 695 |
]; |
| 696 |
} |
| 697 |
|
| 698 |
print $o $self->_to_json($result); |
| 699 |
return 0; |
| 700 |
} |
| 701 |
|
| 702 |
sub run { |
| 703 |
my $self = shift; |
| 704 |
my $action = shift; |
| 705 |
my $opts = shift; |
| 706 |
|
| 707 |
unless ( ref $self ) { |
| 708 |
$self = $self->new; |
| 709 |
} |
| 710 |
unless ($action) { |
| 711 |
die 'nothing to do, aborting'; |
| 712 |
} |
| 713 |
$self->opts($opts); |
| 714 |
|
| 715 |
# Simple commands |
| 716 |
if ( $action =~ /^(?:get|search|delete|setKey|delKey|backup|restore)$/ ) { |
| 717 |
return $self->$action(@_); |
| 718 |
} |
| 719 |
|
| 720 |
# Subcommands and target |
| 721 |
elsif ( $action =~ /^(?:secondfactors|consents|history)$/ ) { |
| 722 |
my $subcommand = shift; |
| 723 |
unless ($subcommand) { |
| 724 |
die "Missing subcommand $action"; |
| 725 |
} |
| 726 |
my $func = "${action}_${subcommand}"; |
| 727 |
if ( $self->can($func) ) { |
| 728 |
return $self->$func(@_); |
| 729 |
} |
| 730 |
else { |
| 731 |
die "Unknown subcommand $subcommand for action $action"; |
| 732 |
} |
| 733 |
} |
| 734 |
else { |
| 735 |
die "unknown action $action. Only get or search are allowed"; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
1; |