From c5c6c4ab0c16f8b6087bd3a4ed2222e25688db54 Mon Sep 17 00:00:00 2001 From: Maxime Besson Date: Mon, 17 Aug 2020 18:18:52 +0200 Subject: [PATCH] Factor genId2F method into Common --- .../lib/Lemonldap/NG/Common/CliSessions.pm | 14 +++----------- .../lib/Lemonldap/NG/Common/Util.pm | 15 +++++++++++++-- .../lib/Lemonldap/NG/Manager/Api/2F.pm | 17 +++++------------ 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/CliSessions.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/CliSessions.pm index 629140fbf..d99d801ed 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/CliSessions.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/CliSessions.pm @@ -3,12 +3,11 @@ package Lemonldap::NG::Common::CliSessions; use strict; use Mouse; use JSON; -use MIME::Base64; use Lemonldap::NG::Common::Conf; use Lemonldap::NG::Common::Logger::Std; use Lemonldap::NG::Common::Apache::Session; use Lemonldap::NG::Common::Session; -use Lemonldap::NG::Common::Util qw/getPSessionID/; +use Lemonldap::NG::Common::Util qw/getPSessionID genId2F/; our $VERSION = '2.0.8'; @@ -195,13 +194,6 @@ sub get { return 0; } -# TODO factor with manager API function -sub _genId2F { - my ( $self, $device ) = @_; - return encode_base64( "$device->{epoch}::$device->{type}::$device->{name}", - "" ); -} - sub _get_psession { my ( $self, $uid ) = @_; my $psession_id = getPSessionID($uid); @@ -280,7 +272,7 @@ sub secondfactors_get { my $target = shift; my $o = $self->stdout; my $consents = $self->_get_psession_special( $target, '_2fDevices', - sub { $self->_genId2F( $_[0] ) } ); + sub { genId2F( $_[0] ) } ); print $o $self->_to_json($consents); return 0; } @@ -301,7 +293,7 @@ sub secondfactors_delete { my @ids = @_; return unless @ids; $self->_del_psession_special( $target, '_2fDevices', - sub { $self->_genId2F( $_[0] ) }, @ids ); + sub { genId2F( $_[0] ) }, @ids ); return 0; } diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Util.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Util.pm index b305a8035..ab501ff6f 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Util.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Util.pm @@ -2,19 +2,26 @@ package Lemonldap::NG::Common::Util; require Exporter; use Digest::MD5; +use MIME::Base64 qw/encode_base64/; use 5.10.0; our $VERSION = '2.0.9'; our @ISA = qw(Exporter); -our @EXPORT_OK = qw(getSameSite getPSessionID); +our @EXPORT_OK = qw(getSameSite getPSessionID genId2F); -# Return stable psession ID from username sub getPSessionID { my ($uid) = @_; return substr( Digest::MD5::md5_hex($uid), 0, 32 ); } +sub genId2F { + my ( $device ) = @_; + return encode_base64( "$device->{epoch}::$device->{type}::$device->{name}", + "" ); +} + + sub getSameSite { my ($conf) = @_; @@ -61,6 +68,10 @@ modules. This method computes the psession ID from the user login +=head3 genId2F($device) + +This method computes the unique ID of each 2F device, for use with the API and CLI + =head3 getSameSite($conf) Try to find a sensible value for the SameSite cookie attribute. diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/2F.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/2F.pm index deeb16eee..d72a724cd 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/2F.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/2F.pm @@ -8,9 +8,9 @@ use 5.10.0; use utf8; use Mouse; use JSON; -use MIME::Base64; use Lemonldap::NG::Common::Session; +use Lemonldap::NG::Common::Util qw/genId2F/; sub getSecondFactors { my ( $self, $req ) = @_; @@ -155,12 +155,12 @@ sub _get2F { ); push @secondFactors, { - id => $self->_genId2F($device), + id => genId2F($device), type => $device->{type}, name => $device->{name} } unless ( ( defined $type and $type ne $device->{type} ) - or ( defined $id and $id ne $self->_genId2F($device) ) ); + or ( defined $id and $id ne genId2F($device) ) ); } } $self->logger->debug( @@ -168,12 +168,6 @@ sub _get2F { return { res => 'ok', secondFactors => [@secondFactors] }; } -sub _genId2F { - my ( $self, $device ) = @_; - return encode_base64( "$device->{epoch}::$device->{type}::$device->{name}", - "" ); -} - sub _getPersistentMod { my ($self) = @_; my $mod = $self->sessionTypes->{persistent}; @@ -245,14 +239,13 @@ sub _delete2FFromSessions { if ( ( defined $type or defined $id ) and ( ( defined $type and $type ne $element->{type} ) - or - ( defined $id and $id ne $self->_genId2F($element) ) ) + or ( defined $id and $id ne genId2F($element) ) ) ) { push @keep, $element; } else { - $removed->{ $self->_genId2F($element) } = "removed"; + $removed->{ genId2F($element) } = "removed"; } } if ( ( $total - scalar @keep ) > 0 ) {