| 1 |
package Lemonldap::NG::Manager::Api::2F; |
| 2 |
|
| 3 |
our $VERSION = '2.21.0'; |
| 4 |
|
| 5 |
package Lemonldap::NG::Manager::Api; |
| 6 |
|
| 7 |
use strict; |
| 8 |
use utf8; |
| 9 |
use Mouse; |
| 10 |
use JSON; |
| 11 |
|
| 12 |
use Lemonldap::NG::Common::Session; |
| 13 |
use Lemonldap::NG::Common::Util qw/genId2F display2F filterKey2F/; |
| 14 |
|
| 15 |
sub getSecondFactors { |
| 16 |
my ( $self, $req ) = @_; |
| 17 |
my ( $uid, $res ); |
| 18 |
|
| 19 |
$uid = $req->params('_uid') |
| 20 |
or return $self->searchSecondFactors($req); |
| 21 |
|
| 22 |
$self->logger->debug("[API] 2F for $uid requested"); |
| 23 |
|
| 24 |
$res = $self->_get2F($uid); |
| 25 |
|
| 26 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 27 |
unless ( $res->{res} eq 'ok' ); |
| 28 |
|
| 29 |
return $self->sendJSONresponse( $req, $res->{secondFactors} ); |
| 30 |
} |
| 31 |
|
| 32 |
sub getSecondFactorsByType { |
| 33 |
my ( $self, $req ) = @_; |
| 34 |
my ( $uid, $type, $res ); |
| 35 |
|
| 36 |
$uid = $req->params('_uid') |
| 37 |
or return $self->sendError( $req, 'Uid is missing', 400 ); |
| 38 |
|
| 39 |
$type = $req->params('type') |
| 40 |
or return $self->sendError( $req, 'Type is missing', 400 ); |
| 41 |
|
| 42 |
$self->logger->debug("[API] 2F for $uid with type $type requested"); |
| 43 |
|
| 44 |
$res = $self->_get2F( $uid, uc $type ); |
| 45 |
|
| 46 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 47 |
unless ( $res->{res} eq 'ok' ); |
| 48 |
|
| 49 |
return $self->sendJSONresponse( $req, $res->{secondFactors} ); |
| 50 |
} |
| 51 |
|
| 52 |
sub getSecondFactorsById { |
| 53 |
my ( $self, $req ) = @_; |
| 54 |
my ( $uid, $id, $res ); |
| 55 |
|
| 56 |
$uid = $req->params('_uid') |
| 57 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 58 |
|
| 59 |
$id = $req->params('id') |
| 60 |
or return $self->sendError( $req, 'id is missing', 400 ); |
| 61 |
|
| 62 |
$self->logger->debug("[API] 2F for $uid with id $id requested"); |
| 63 |
|
| 64 |
$res = $self->_get2F( $uid, undef, $id ); |
| 65 |
|
| 66 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 67 |
unless ( $res->{res} eq 'ok' ); |
| 68 |
|
| 69 |
return $self->sendError( $req, "2F id '$id' not found for user '$uid'", |
| 70 |
404 ) |
| 71 |
unless ( scalar @{ $res->{secondFactors} } > 0 ); |
| 72 |
|
| 73 |
return $self->sendJSONresponse( $req, @{ $res->{secondFactors} }[0] ); |
| 74 |
} |
| 75 |
|
| 76 |
sub deleteSecondFactors { |
| 77 |
my ( $self, $req, @path ) = @_; |
| 78 |
my ( $uid, $res ); |
| 79 |
|
| 80 |
# Return an error on |
| 81 |
@path |
| 82 |
and return $self->sendError( $req, 'Invalid URI, provide /id/ or /type/', |
| 83 |
400 ); |
| 84 |
|
| 85 |
$uid = $req->params('uid') |
| 86 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 87 |
|
| 88 |
$self->logger->debug("[API] Delete all 2F for $uid requested"); |
| 89 |
|
| 90 |
$res = $self->_delete2F( $req, $uid ); |
| 91 |
|
| 92 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 93 |
unless ( $res->{res} eq 'ok' ); |
| 94 |
|
| 95 |
return $self->sendJSONresponse( $req, { message => $res->{msg} } ); |
| 96 |
} |
| 97 |
|
| 98 |
sub searchSecondFactors { |
| 99 |
my ( $self, $req ) = @_; |
| 100 |
|
| 101 |
my $uid = $req->param('uid') || '*'; |
| 102 |
my @types = $req->parameters->get_all('type'); |
| 103 |
use Hash::MultiValue; |
| 104 |
my $search_params = |
| 105 |
Hash::MultiValue->from_mixed( { _session_uid => $uid, type => \@types } ); |
| 106 |
|
| 107 |
my $res = $self->search2F( $req, $self->_getPersistentMod, "Persistent", |
| 108 |
$search_params, 1 ); |
| 109 |
|
| 110 |
if ( $res->{result} ) { |
| 111 |
my $api_result = []; |
| 112 |
|
| 113 |
my @sorted_results = |
| 114 |
sort { $a->{userId} cmp $b->{userId} } @{ $res->{values} || [] }; |
| 115 |
for my $result (@sorted_results) { |
| 116 |
push @$api_result, { |
| 117 |
uid => $result->{userId}, |
| 118 |
secondFactors => [ |
| 119 |
map { |
| 120 |
{ |
| 121 |
id => genId2F($_), |
| 122 |
type => $_->{type}, |
| 123 |
name => $_->{name} |
| 124 |
} |
| 125 |
} @{ $result->{_2fDevices} || [] } |
| 126 |
] |
| 127 |
|
| 128 |
}; |
| 129 |
} |
| 130 |
return $self->sendJSONresponse( $req, $api_result ); |
| 131 |
} |
| 132 |
else { |
| 133 |
return $self->sendError( $req, $res->{msg}, $res->{code} ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
sub deleteSecondFactorsById { |
| 138 |
my ( $self, $req ) = @_; |
| 139 |
my ( $uid, $id, $res ); |
| 140 |
|
| 141 |
$uid = $req->params('uid') |
| 142 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 143 |
|
| 144 |
$id = $req->params('id') |
| 145 |
or return $self->sendError( $req, 'id is missing', 400 ); |
| 146 |
|
| 147 |
$self->logger->debug("[API] Delete 2F for $uid with id $id requested"); |
| 148 |
|
| 149 |
$res = $self->_delete2F( $req, $uid, undef, $id ); |
| 150 |
|
| 151 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 152 |
unless ( $res->{res} eq 'ok' ); |
| 153 |
|
| 154 |
return $self->sendError( $req, "2F id '$id' not found for user '$uid'", |
| 155 |
404 ) |
| 156 |
unless ( $res->{removed} > 0 ); |
| 157 |
|
| 158 |
return $self->sendJSONresponse( $req, { message => $res->{msg} } ); |
| 159 |
} |
| 160 |
|
| 161 |
sub deleteSecondFactorsByType { |
| 162 |
my ( $self, $req ) = @_; |
| 163 |
my ( $uid, $type, $res ); |
| 164 |
|
| 165 |
$uid = $req->params('uid') |
| 166 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 167 |
|
| 168 |
$type = $req->params('type') |
| 169 |
or return $self->sendError( $req, 'type is missing', 400 ); |
| 170 |
|
| 171 |
$self->logger->debug( |
| 172 |
"[API] Delete all 2F for $uid with type $type requested"); |
| 173 |
|
| 174 |
$res = $self->_delete2F( $req, $uid, uc $type ); |
| 175 |
|
| 176 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 177 |
unless ( $res->{res} eq 'ok' ); |
| 178 |
|
| 179 |
return $self->sendJSONresponse( $req, { message => $res->{msg} } ); |
| 180 |
} |
| 181 |
|
| 182 |
sub _get2F { |
| 183 |
my ( $self, $uid, $type, $id ) = @_; |
| 184 |
my ( $res, $psessions, @secondFactors ); |
| 185 |
|
| 186 |
$psessions = $self->_getSessions2F( $self->_getPersistentMod, 'Persistent', |
| 187 |
'_session_uid', $uid ); |
| 188 |
|
| 189 |
foreach ( keys %$psessions ) { |
| 190 |
my $devices = $self->_getDevicesFromSessionData( $psessions->{$_} ); |
| 191 |
foreach my $device ( @{$devices} ) { |
| 192 |
$self->logger->debug( |
| 193 |
"Check device [epoch=$device->{epoch}, type=$device->{type}, name=$device->{name}]" |
| 194 |
); |
| 195 |
if ( ( !defined $type or uc($type) eq uc( $device->{type} ) ) |
| 196 |
and ( !defined $id or $id eq genId2F($device) ) ) |
| 197 |
{ |
| 198 |
push @secondFactors, |
| 199 |
{ %{ filterKey2F($device) }, id => genId2F($device), }; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
$self->logger->debug( |
| 204 |
"Found " . scalar @secondFactors . " 2F devices for uid $uid." ); |
| 205 |
return { res => 'ok', secondFactors => [@secondFactors] }; |
| 206 |
} |
| 207 |
|
| 208 |
sub _getSessions2F { |
| 209 |
my ( $self, $mod, $kind, $key, $uid ) = @_; |
| 210 |
$self->logger->debug("Looking for sessions for uid $uid ..."); |
| 211 |
my $sessions = |
| 212 |
Lemonldap::NG::Common::Apache::Session->searchOn( $mod->{options}, $key, |
| 213 |
$uid, |
| 214 |
( '_session_kind', '_session_uid', '_session_id', '_2fDevices' ) ); |
| 215 |
foreach ( keys %$sessions ) { |
| 216 |
delete $sessions->{$_} |
| 217 |
unless ( $sessions->{$_}->{_session_kind} eq $kind ); |
| 218 |
} |
| 219 |
$self->logger->debug( "Found " |
| 220 |
. scalar( keys %$sessions ) |
| 221 |
. " $kind sessions for uid $uid." ); |
| 222 |
|
| 223 |
return $sessions; |
| 224 |
} |
| 225 |
|
| 226 |
sub _getSession2F { |
| 227 |
my ( $self, $sessionId, $mod ) = @_; |
| 228 |
$self->logger->debug("Looking for session with sessionId $sessionId ..."); |
| 229 |
my $session = $self->getApacheSession( $mod, $sessionId ); |
| 230 |
$self->logger->debug( |
| 231 |
defined $session |
| 232 |
? "Session $sessionId found." |
| 233 |
: " No session found for sessionId $sessionId" |
| 234 |
); |
| 235 |
|
| 236 |
return $session; |
| 237 |
} |
| 238 |
|
| 239 |
sub _delete2FFromSessions { |
| 240 |
my ( $self, $uid, $type, $id, $mod, $kind, $key ) = @_; |
| 241 |
my ( $sessions, $session, $devices, @keep, $removed, |
| 242 |
$total, $module, $localStorage ); |
| 243 |
$sessions = $self->_getSessions2F( $mod, $kind, $key, $uid ); |
| 244 |
foreach ( keys %$sessions ) { |
| 245 |
|
| 246 |
$session = $self->_getSession2F( $_, $mod ) |
| 247 |
or return { res => 'ko', code => 500, msg => $@ }; |
| 248 |
|
| 249 |
$self->logger->debug( |
| 250 |
"Looking for 2F Device(s) attached to sessionId $_"); |
| 251 |
|
| 252 |
if ( $session->data->{_2fDevices} ) { |
| 253 |
$devices = $self->_getDevicesFromSessionData( $session->data ); |
| 254 |
$total = scalar @$devices; |
| 255 |
|
| 256 |
$self->logger->debug( |
| 257 |
"Found $total 2F devices attached to sessionId $_"); |
| 258 |
|
| 259 |
@keep = (); |
| 260 |
while (@$devices) { |
| 261 |
my $element = shift @$devices; |
| 262 |
if ( |
| 263 |
( defined $type or defined $id ) |
| 264 |
and ( ( |
| 265 |
defined $type |
| 266 |
and uc($type) ne uc( $element->{type} ) |
| 267 |
) |
| 268 |
or ( defined $id and $id ne genId2F($element) ) |
| 269 |
) |
| 270 |
) |
| 271 |
{ |
| 272 |
push @keep, $element; |
| 273 |
} |
| 274 |
else { |
| 275 |
$removed->{ genId2F($element) } = $element; |
| 276 |
} |
| 277 |
} |
| 278 |
if ( ( $total - scalar @keep ) > 0 ) { |
| 279 |
|
| 280 |
# Update session |
| 281 |
$self->logger->debug( "Removing " |
| 282 |
. ( $total - scalar @keep ) |
| 283 |
. " 2F device(s) attached to sessionId $_ ..." ); |
| 284 |
$session->data->{_2fDevices} = to_json( \@keep ); |
| 285 |
$session->update( $session->data ); |
| 286 |
|
| 287 |
# Delete from local cache |
| 288 |
if ( $session->{options}->{localStorage} ) { |
| 289 |
$module = $session->{options}->{localStorage}; |
| 290 |
eval "use $module;"; |
| 291 |
$localStorage = |
| 292 |
$module->new( |
| 293 |
$session->{options}->{localStorageOptions} ); |
| 294 |
if ( $localStorage->get($_) ) { |
| 295 |
$self->logger->debug( |
| 296 |
"Delete local cache for session $_"); |
| 297 |
$localStorage->remove($_); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
else { |
| 302 |
$self->logger->debug( |
| 303 |
"No matching 2F devices attached to sessionId $_ were selected for removal." |
| 304 |
); |
| 305 |
} |
| 306 |
} |
| 307 |
else { |
| 308 |
$self->logger->debug( |
| 309 |
"No 2F devices attached to sessionId $_ were found."); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
return { res => 'ok', removed => $removed }; |
| 314 |
} |
| 315 |
|
| 316 |
sub _delete2F { |
| 317 |
my ( $self, $req, $uid, $type, $id ) = @_; |
| 318 |
my ( $res, $removed, $count ); |
| 319 |
|
| 320 |
$res = |
| 321 |
$self->_delete2FFromSessions( $uid, $type, $id, $self->_getPersistentMod, |
| 322 |
'Persistent', '_session_uid' ); |
| 323 |
return $res if ( $res->{res} ne 'ok' ); |
| 324 |
$removed = $res->{removed} || {}; |
| 325 |
|
| 326 |
my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace}; |
| 327 |
$res = |
| 328 |
$self->_delete2FFromSessions( $uid, $type, $id, $self->_getSSOMod, 'SSO', |
| 329 |
$whatToTrace ); |
| 330 |
return $res if ( $res->{res} ne 'ok' ); |
| 331 |
$res->{removed} ||= {}; |
| 332 |
|
| 333 |
# merge results |
| 334 |
$removed = { %$removed, %{ $res->{removed} } }; |
| 335 |
$count = scalar( keys %$removed ); |
| 336 |
if ($count) { |
| 337 |
my @list_log = map { display2F( $removed->{$_} ) } keys %$removed; |
| 338 |
my $list_log = join( ",", @list_log ); |
| 339 |
$self->auditLog( |
| 340 |
$req, |
| 341 |
code => "API_2FA_DELETED", |
| 342 |
message => ("[API] 2FA deletion for $uid: $list_log"), |
| 343 |
user => $uid, |
| 344 |
removed_2fa => \@list_log, |
| 345 |
); |
| 346 |
} |
| 347 |
|
| 348 |
return { |
| 349 |
res => 'ok', |
| 350 |
removed => $count, |
| 351 |
msg => $count > 0 |
| 352 |
? "Successful operation: " . $count . " 2F were removed" |
| 353 |
: "No operation performed" |
| 354 |
}; |
| 355 |
} |
| 356 |
|
| 357 |
sub _getDevicesFromSessionData { |
| 358 |
my ( $self, $sessiondata ) = @_; |
| 359 |
if ( $sessiondata->{_2fDevices} ) { |
| 360 |
my $devices; |
| 361 |
eval { $devices = from_json( $sessiondata->{_2fDevices} ); }; |
| 362 |
if ($@) { |
| 363 |
$self->logger->warn("Error deserializing _2fDevices: $@"); |
| 364 |
} |
| 365 |
else { |
| 366 |
if ( ref($devices) eq "ARRAY" ) { |
| 367 |
return $devices; |
| 368 |
} |
| 369 |
else { |
| 370 |
$self->logger->warn( |
| 371 |
"Error deserializing _2fDevices: not a JSON Array"); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
return []; |
| 376 |
} |
| 377 |
|
| 378 |
sub addSecondFactor { |
| 379 |
my ( $self, $req ) = @_; |
| 380 |
|
| 381 |
my $uid = $req->params('uid') |
| 382 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 383 |
|
| 384 |
$self->logger->debug("[API] Add 2F for $uid"); |
| 385 |
|
| 386 |
my $add = $req->jsonBodyToObj; |
| 387 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 388 |
unless ( $add and ref($add) eq "HASH" ); |
| 389 |
|
| 390 |
return $self->sendError( $req, 'Invalid input: type is missing', 400 ) |
| 391 |
unless ( defined $add->{type} ); |
| 392 |
|
| 393 |
return $self->sendError( $req, 'Invalid input: epoch is forbidden', 400 ) |
| 394 |
if ( defined $add->{epoch} ); |
| 395 |
|
| 396 |
my $allow_create = |
| 397 |
( $req->params('create') |
| 398 |
&& $req->params('create') ne "false" |
| 399 |
&& $req->params('create') ne "0" ); |
| 400 |
|
| 401 |
my $res = $self->_add2F( $uid, undef, $add, $allow_create ); |
| 402 |
|
| 403 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 404 |
if ( $res->{res} ne 'ok' ); |
| 405 |
return $self->sendJSONresponse( |
| 406 |
$req, |
| 407 |
{ message => "Successful operation" }, |
| 408 |
code => 201 |
| 409 |
); |
| 410 |
} |
| 411 |
|
| 412 |
sub addSecondFactorByType { |
| 413 |
my ( $self, $req ) = @_; |
| 414 |
|
| 415 |
my $uid = $req->params('uid') |
| 416 |
or return $self->sendError( $req, 'uid is missing', 400 ); |
| 417 |
|
| 418 |
my $type = $req->params('type') |
| 419 |
or return $self->sendError( $req, 'type is missing', 400 ); |
| 420 |
|
| 421 |
my $add = $req->jsonBodyToObj; |
| 422 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 423 |
unless ( $add and ref($add) eq "HASH" ); |
| 424 |
|
| 425 |
$self->logger->debug("[API] Add $type 2F for $uid"); |
| 426 |
|
| 427 |
my $allow_create = |
| 428 |
( $req->params('create') |
| 429 |
&& $req->params('create') ne "false" |
| 430 |
&& $req->params('create') ne "0" ); |
| 431 |
|
| 432 |
my $res = $self->_add2F( $uid, $type, $add, $allow_create ); |
| 433 |
|
| 434 |
return $self->sendError( $req, $res->{msg}, $res->{code} ) |
| 435 |
unless ( $res->{res} eq 'ok' ); |
| 436 |
|
| 437 |
return $self->sendJSONresponse( |
| 438 |
$req, |
| 439 |
{ message => "Successful operation" }, |
| 440 |
code => 201 |
| 441 |
); |
| 442 |
} |
| 443 |
|
| 444 |
sub _transformNew2f { |
| 445 |
my ( $self, $type, $add ) = @_; |
| 446 |
|
| 447 |
# Generic API, no transformation is done |
| 448 |
if ( !defined($type) ) { |
| 449 |
return { |
| 450 |
res => "ok", |
| 451 |
device => $add, |
| 452 |
}; |
| 453 |
} |
| 454 |
|
| 455 |
if ( uc($type) eq "TOTP" ) { |
| 456 |
return $self->_transformNew2fTotp( $type, $add ); |
| 457 |
} |
| 458 |
|
| 459 |
else { |
| 460 |
return { |
| 461 |
res => "ko", |
| 462 |
code => 400, |
| 463 |
msg => "Invalid type: $type", |
| 464 |
}; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
sub _transformNew2fTotp { |
| 469 |
my ( $self, $type, $add ) = @_; |
| 470 |
|
| 471 |
if ( !$add->{key} ) { |
| 472 |
return { |
| 473 |
res => "ko", |
| 474 |
code => 400, |
| 475 |
msg => "Invalid input: missing \"key\" parameter", |
| 476 |
}; |
| 477 |
} |
| 478 |
|
| 479 |
my $secret = $self->_totpKeyToSecret( $add->{key} ); |
| 480 |
if ( !$secret ) { |
| 481 |
return { |
| 482 |
res => "ko", |
| 483 |
code => 400, |
| 484 |
msg => "Invalid secret: you must provide a base32-encoded key", |
| 485 |
}; |
| 486 |
} |
| 487 |
|
| 488 |
my $newdevice = { |
| 489 |
type => "TOTP", |
| 490 |
( $add->{name} ? ( name => $add->{name} ) : () ), |
| 491 |
_secret => $secret, |
| 492 |
}; |
| 493 |
|
| 494 |
return { |
| 495 |
res => "ok", |
| 496 |
device => $newdevice, |
| 497 |
}; |
| 498 |
} |
| 499 |
|
| 500 |
sub _totpKeyToSecret { |
| 501 |
my ( $self, $key ) = @_; |
| 502 |
|
| 503 |
return unless $key; |
| 504 |
|
| 505 |
# Make sure only BASE32 characters are present |
| 506 |
if ( uc($key) =~ /^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\s]*$/ ) { |
| 507 |
|
| 508 |
# Trim spaces and normalize to lowercase |
| 509 |
my $newKey = $key =~ s/\s//gr; |
| 510 |
return $self->totp_encrypt->get_storable_secret( lc($newKey) ); |
| 511 |
} |
| 512 |
|
| 513 |
return undef; |
| 514 |
} |
| 515 |
|
| 516 |
sub _get_epoch { |
| 517 |
my ($self) = @_; |
| 518 |
return time(); |
| 519 |
} |
| 520 |
|
| 521 |
sub _add2F { |
| 522 |
my ( $self, $uid, $type, $add, $allow_create ) = @_; |
| 523 |
|
| 524 |
my $res = $self->_transformNew2f( $type, $add ); |
| 525 |
return $res if ( $res->{res} ne 'ok' ); |
| 526 |
|
| 527 |
my $device = $res->{device}; |
| 528 |
|
| 529 |
$device->{epoch} = $self->_get_epoch(); |
| 530 |
|
| 531 |
$res = $self->_check_duplicates( $uid, $device ); |
| 532 |
return $res if ( $res->{res} ne 'ok' ); |
| 533 |
|
| 534 |
$res = |
| 535 |
$self->_add2FToSessions( $uid, $device, $self->_getPersistentMod, |
| 536 |
'Persistent', '_session_uid', $allow_create ); |
| 537 |
return $res if ( $res->{res} ne 'ok' ); |
| 538 |
|
| 539 |
# Add to existing sessions |
| 540 |
my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace}; |
| 541 |
$res = |
| 542 |
$self->_add2FToSessions( $uid, $device, $self->_getSSOMod, 'SSO', |
| 543 |
$whatToTrace ); |
| 544 |
|
| 545 |
return { res => "ok" }; |
| 546 |
} |
| 547 |
|
| 548 |
sub _check_duplicates { |
| 549 |
my ( $self, $uid, $device ) = @_; |
| 550 |
my $type = $device->{type}; |
| 551 |
my $new_id = genId2F($device); |
| 552 |
|
| 553 |
my $res = $self->_get2F( $uid, uc $type ); |
| 554 |
if ( $res->{res} eq 'ok' ) { |
| 555 |
my @devices = @{ $res->{secondFactors} // [] }; |
| 556 |
if ( grep { $_->{id} and $_->{id} eq $new_id } @devices ) { |
| 557 |
return { |
| 558 |
res => 'ko', |
| 559 |
code => 409, |
| 560 |
msg => "ID $new_id is already used by an existing device" |
| 561 |
}; |
| 562 |
} |
| 563 |
else { |
| 564 |
return { res => 'ok' }; |
| 565 |
} |
| 566 |
} |
| 567 |
else { |
| 568 |
return { res => 'ok' }; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
sub _add2FToSessions { |
| 573 |
my ( $self, $uid, $add, $mod, $kind, $key, $allow_create ) = @_; |
| 574 |
|
| 575 |
my ( $sessions, $session, $devices, @keep, $removed, |
| 576 |
$total, $module, $localStorage ); |
| 577 |
$sessions = $self->_getSessions2F( $mod, $kind, $key, $uid ); |
| 578 |
my $found = keys %$sessions; |
| 579 |
|
| 580 |
if ( !$found and $allow_create ) { |
| 581 |
my $ps = $self->getPersistentSession( $mod, $uid ); |
| 582 |
$sessions = { $ps->id => { _2fDevices => undef } }; |
| 583 |
$found = 1; |
| 584 |
} |
| 585 |
|
| 586 |
foreach ( keys %$sessions ) { |
| 587 |
$session = $self->_getSession2F( $_, $mod ) |
| 588 |
or return { res => 'ko', code => 500, msg => $@ }; |
| 589 |
|
| 590 |
$devices = $self->_getDevicesFromSessionData( $session->data ); |
| 591 |
push @$devices, $add; |
| 592 |
|
| 593 |
# Update session |
| 594 |
$self->logger->debug( |
| 595 |
"Adding " . display2F($add) . " to sessionId $_" ); |
| 596 |
$session->data->{_2fDevices} = to_json($devices); |
| 597 |
$session->update( $session->data ); |
| 598 |
} |
| 599 |
return { |
| 600 |
res => "notfound", |
| 601 |
message => "User $uid was not found", |
| 602 |
code => 404 |
| 603 |
} |
| 604 |
if !$found; |
| 605 |
|
| 606 |
return { res => 'ok' }; |
| 607 |
} |
| 608 |
|
| 609 |
1; |