requires
qw(p conf logger)
;
our
$VERSION
=
'2.21.0'
;
sub
update2fDevice {
my
(
$self
,
$req
,
$info
,
$type
,
$key
,
$value
,
$update_key
,
$update_value
)
=
@_
;
my
$user
=
$info
->{
$self
->conf->{whatToTrace} };
my
$_2fDevices
=
$self
->get2fDevices(
$req
,
$info
);
return
0
unless
$_2fDevices
;
my
@found
=
grep
{
$_
->{type} eq
$type
and
$_
->{
$key
} eq
$value
} @{
$_2fDevices
};
for
my
$device
(
@found
) {
$device
->{
$update_key
} =
$update_value
;
}
if
(
@found
) {
$self
->p->updatePersistentSession(
$req
,
{
_2fDevices
=> to_json(
$_2fDevices
) },
$user
);
return
1;
}
return
0;
}
sub
add2fDevice {
my
(
$self
,
$req
,
$info
,
$device
) =
@_
;
my
$_2fDevices
=
$self
->get2fDevices(
$req
,
$info
);
push
@{
$_2fDevices
},
$device
;
my
$uid
=
$info
->{
$self
->conf->{whatToTrace} };
$self
->auditLog(
$req
,
message
=> (
"User "
.
$uid
.
" registered 2F device: "
. display2F(
$device
)
),
code
=>
"2FA_DEVICE_REGISTERED"
,
user
=>
$uid
,
device
=> display2F(
$device
),
);
$self
->p->updatePersistentSession(
$req
,
{
_2fDevices
=> to_json(
$_2fDevices
) } );
return
1;
}
sub
del2fDevices {
my
(
$self
,
$req
,
$info
,
$devices
) =
@_
;
return
0
unless
(
ref
(
$devices
) eq
'ARRAY'
);
my
$_2fDevices
=
$self
->get2fDevices(
$req
,
$info
);
return
0
unless
$_2fDevices
;
my
@updated_2fDevices
= @{
$_2fDevices
};
my
$need_update
= 0;
for
my
$device_spec
(
@$devices
) {
next
unless
(
ref
(
$device_spec
) eq
'HASH'
);
my
$type
=
$device_spec
->{type};
my
$epoch
=
$device_spec
->{epoch};
next
unless
(
$type
and
$epoch
);
my
$size_before
=
@updated_2fDevices
;
@updated_2fDevices
=
grep
{ not(
$_
->{type} eq
$type
and
$_
->{epoch} eq
$epoch
) }
@updated_2fDevices
;
if
(
@updated_2fDevices
<
$size_before
) {
$need_update
= 1;
my
$uid
=
$info
->{
$self
->conf->{whatToTrace} };
$self
->auditLog(
$req
,
message
=> (
"User $uid deleted 2F device: "
. display2F(
$device_spec
)
),
code
=>
"2FA_DEVICE_DELETED"
,
user
=>
$uid
,
device
=> display2F(
$device_spec
),
);
}
}
$self
->p->updatePersistentSession(
$req
,
{
_2fDevices
=> to_json( [
@updated_2fDevices
] ) } )
if
$need_update
;
return
1;
}
sub
del2fDevice {
my
(
$self
,
$req
,
$info
,
$type
,
$epoch
) =
@_
;
return
$self
->del2fDevices(
$req
,
$info
,
[ {
type
=>
$type
,
epoch
=>
$epoch
} ] );
}
sub
find2fDevicesByKey {
my
(
$self
,
$req
,
$info
,
$type
,
$key
,
$value
) =
@_
;
my
$_2fDevices
=
$self
->get2fDevices(
$req
,
$info
);
return
unless
$_2fDevices
;
my
@found
=
grep
{
$_
->{type} eq
$type
and
$_
->{
$key
} eq
$value
} @{
$_2fDevices
};
return
@found
;
}
sub
get2fDevices {
my
(
$self
,
$req
,
$info
) =
@_
;
my
$_2fDevices
;
$self
->logger->debug(
"Looking for 2F devices..."
);
if
(
$info
->{_2fDevices} ) {
$_2fDevices
=
eval
{ from_json(
$info
->{_2fDevices}, {
allow_nonref
=> 1 } ); };
if
($@) {
$self
->logger->error(
"Corrupted session (_2fDevices): $@"
);
return
;
}
}
else
{
return
[];
}
return
ref
(
$_2fDevices
) eq
'ARRAY'
?
$_2fDevices
:
undef
;
}
sub
find2fDevicesByType {
my
(
$self
,
$req
,
$info
,
$type
) =
@_
;
my
$_2fDevices
=
$self
->get2fDevices(
$req
,
$info
);
return
unless
$_2fDevices
;
return
@{
$_2fDevices
}
unless
$type
;
my
@found
=
grep
{
$_
->{type} eq
$type
} @{
$_2fDevices
};
$self
->logger->debug(
"Return $type"
);
return
@found
;
}
1;
=back