| 1 |
package Lemonldap::NG::Common::Util; |
| 2 |
require Exporter; |
| 3 |
|
| 4 |
use strict; |
| 5 |
use Digest::MD5; |
| 6 |
use MIME::Base64 qw(encode_base64); |
| 7 |
|
| 8 |
our $VERSION = '2.23.0'; |
| 9 |
our @ISA = qw(Exporter); |
| 10 |
our @EXPORT_OK = |
| 11 |
qw(getSameSite getPSessionID genId2F display2F filterKey2F isHiddenAttr); |
| 12 |
|
| 13 |
sub getPSessionID { |
| 14 |
my ($uid) = @_; |
| 15 |
return substr( Digest::MD5::md5_hex($uid), 0, 32 ); |
| 16 |
} |
| 17 |
|
| 18 |
sub genId2F { |
| 19 |
my ($device) = @_; |
| 20 |
return encode_base64( "$device->{epoch}::$device->{type}::$device->{name}", |
| 21 |
"" ); |
| 22 |
} |
| 23 |
|
| 24 |
sub display2F { |
| 25 |
my ($device) = @_; |
| 26 |
return sprintf( "[%s]%s", $device->{type}, $device->{epoch} ); |
| 27 |
} |
| 28 |
|
| 29 |
# Return the 2F device without private _* keys |
| 30 |
sub filterKey2F { |
| 31 |
my ( $device, @filterKeys ) = @_; |
| 32 |
my $filtered_2f = |
| 33 |
{ map { $_ => $device->{$_} } grep( !/^_/, keys %$device ) }; |
| 34 |
|
| 35 |
delete $filtered_2f->{$_} for @filterKeys; |
| 36 |
return $filtered_2f; |
| 37 |
} |
| 38 |
|
| 39 |
sub isHiddenAttr { |
| 40 |
my ( $conf, $attr, @extra_hidden_attributes ) = @_; |
| 41 |
my ( @regexps, $match ); |
| 42 |
|
| 43 |
my %hiddenAttributes = map { $_ => 1 } grep { |
| 44 |
if (m#^/(.+)?/$#) { |
| 45 |
push @regexps, qr/$1/; |
| 46 |
0; |
| 47 |
} |
| 48 |
else { |
| 49 |
1; |
| 50 |
} |
| 51 |
} ( split( /[,\s]+/, $conf->{hiddenAttributes} ), |
| 52 |
@extra_hidden_attributes ); |
| 53 |
|
| 54 |
my $regex = |
| 55 |
keys %hiddenAttributes |
| 56 |
? '\b(?:' . join( '|', keys %hiddenAttributes ) . ')\b' |
| 57 |
: ''; |
| 58 |
|
| 59 |
foreach (@regexps) { |
| 60 |
$match++ if $attr =~ $_; |
| 61 |
} |
| 62 |
|
| 63 |
return |
| 64 |
$match |
| 65 |
|| $hiddenAttributes{$attr} |
| 66 |
|| ( $regex && $attr =~ m#$regex# ); |
| 67 |
} |
| 68 |
|
| 69 |
sub getSameSite { |
| 70 |
my ($conf) = @_; |
| 71 |
|
| 72 |
# Initialize cookie SameSite value |
| 73 |
return $conf->{sameSite} if $conf->{sameSite}; |
| 74 |
|
| 75 |
# SAML requires SameSite=None for POST bindings |
| 76 |
return ( |
| 77 |
$conf->{issuerDBSAMLActivation} |
| 78 |
or keys %{ $conf->{samlIDPMetaDataXML} } |
| 79 |
) ? 'None' : 'Lax'; |
| 80 |
|
| 81 |
# if CDA, OIDC, CAS: Lax |
| 82 |
# TODO: try to detect when we can use 'Strict'? |
| 83 |
# Any scenario that uses pdata to save state during login, |
| 84 |
# Issuers, and CDA all require at least Lax |
| 85 |
} |
| 86 |
|
| 87 |
1; |
| 88 |
__END__ |
| 89 |
|
| 90 |
=head1 NAME |
| 91 |
|
| 92 |
=encoding utf8 |
| 93 |
|
| 94 |
Lemonldap::NG::Common::Util - Utility class for LemonLDAP::NG |
| 95 |
|
| 96 |
=head1 DESCRIPTION |
| 97 |
|
| 98 |
This package contains various functions that need to be shared between |
| 99 |
modules. |
| 100 |
|
| 101 |
=head1 METHODS |
| 102 |
|
| 103 |
=head3 getPSessionID($uid) |
| 104 |
|
| 105 |
This method computes the psession ID from the user login |
| 106 |
|
| 107 |
=head3 genId2F($device) |
| 108 |
|
| 109 |
This method computes the unique ID of each 2F device, for use with the API and CLI |
| 110 |
|
| 111 |
=head3 display2F($device) |
| 112 |
|
| 113 |
This method formats device name for logging purpose |
| 114 |
|
| 115 |
=head3 isHiddenAttr( $conf, $attr, @extra_hidden_attributes ) |
| 116 |
|
| 117 |
This method tests if the attribute is hidden. |
| 118 |
@extra_hidden_attributes is an array of additional attributes to hide. |
| 119 |
|
| 120 |
=head3 getSameSite($conf) |
| 121 |
|
| 122 |
Try to find a sensible value for the SameSite cookie attribute. |
| 123 |
If the user has overridden it, return the forced value |
| 124 |
|
| 125 |
=head1 AUTHORS |
| 126 |
|
| 127 |
=over |
| 128 |
|
| 129 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 130 |
|
| 131 |
=back |
| 132 |
|
| 133 |
=head1 BUG REPORT |
| 134 |
|
| 135 |
Use OW2 system to report bug or ask for features: |
| 136 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 137 |
|
| 138 |
=head1 DOWNLOAD |
| 139 |
|
| 140 |
Lemonldap::NG is available at |
| 141 |
L<https://lemonldap-ng.org/download> |
| 142 |
|
| 143 |
=head1 COPYRIGHT AND LICENSE |
| 144 |
|
| 145 |
See COPYING file for details. |
| 146 |
|
| 147 |
This library is free software; you can redistribute it and/or modify |
| 148 |
it under the terms of the GNU General Public License as published by |
| 149 |
the Free Software Foundation; either version 2, or (at your option) |
| 150 |
any later version. |
| 151 |
|
| 152 |
This program is distributed in the hope that it will be useful, |
| 153 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 154 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 155 |
GNU General Public License for more details. |
| 156 |
|
| 157 |
You should have received a copy of the GNU General Public License |
| 158 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 159 |
|
| 160 |
=cut |