2019-12-17 09:56:59 +01:00
|
|
|
package Lemonldap::NG::Manager::Api::Common;
|
2020-01-10 21:29:11 +01:00
|
|
|
|
2020-11-21 18:19:10 +01:00
|
|
|
our $VERSION = '2.0.10';
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
package Lemonldap::NG::Manager::Api;
|
|
|
|
|
2020-11-21 18:19:10 +01:00
|
|
|
use strict;
|
2019-12-17 09:56:59 +01:00
|
|
|
use Lemonldap::NG::Manager::Build::Attributes;
|
|
|
|
use Lemonldap::NG::Manager::Build::CTrees;
|
2019-12-17 20:58:55 +01:00
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
# use Scalar::Util 'weaken'; ?
|
|
|
|
|
|
|
|
sub _isSimpleKeyValueHash {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $hash ) = @_;
|
2020-01-10 21:29:11 +01:00
|
|
|
return 0 if ( ref($hash) ne "HASH" );
|
|
|
|
|
|
|
|
foreach ( keys %$hash ) {
|
|
|
|
return 0 if ( ref( $hash->{$_} ) ne '' || ref($_) ne '' );
|
2019-12-17 09:56:59 +01:00
|
|
|
}
|
2020-01-10 21:29:11 +01:00
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:52:44 +02:00
|
|
|
sub _getDefaultValues {
|
|
|
|
my ( $self, $rootNode ) = @_;
|
2019-12-17 20:58:55 +01:00
|
|
|
my @allAttrs = $self->_listAttributes($rootNode);
|
2019-12-17 09:56:59 +01:00
|
|
|
my $defaultAttrs = Lemonldap::NG::Manager::Build::Attributes::attributes();
|
2020-04-06 22:52:44 +02:00
|
|
|
my $attrs = {};
|
2019-12-17 09:56:59 +01:00
|
|
|
|
2020-11-21 18:19:10 +01:00
|
|
|
foreach my $attr (@allAttrs) {
|
2020-04-06 22:52:44 +02:00
|
|
|
$attrs->{$attr} = $defaultAttrs->{$attr}->{default}
|
|
|
|
if ( defined $defaultAttrs->{$attr}
|
|
|
|
&& defined $defaultAttrs->{$attr}->{default} );
|
2019-12-17 09:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _hasAllowedAttributes {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $attributes, $rootNode ) = @_;
|
2019-12-17 09:56:59 +01:00
|
|
|
my @allowedAttributes = $self->_listAttributes($rootNode);
|
|
|
|
|
2020-11-21 18:19:10 +01:00
|
|
|
foreach my $attribute ( keys %{$attributes} ) {
|
2019-12-17 20:58:55 +01:00
|
|
|
if ( length( ref($attribute) ) ) {
|
2019-12-17 09:56:59 +01:00
|
|
|
return {
|
|
|
|
res => "ko",
|
|
|
|
msg => "Invalid input: Attribute $attribute is not a string."
|
|
|
|
};
|
|
|
|
}
|
2020-04-06 22:52:44 +02:00
|
|
|
unless ( grep { $_ eq $attribute } @allowedAttributes ) {
|
2019-12-17 09:56:59 +01:00
|
|
|
return {
|
|
|
|
res => "ko",
|
|
|
|
msg => "Invalid input: Attribute $attribute does not exist."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { res => "ok" };
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _listAttributes {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $rootNode ) = @_;
|
2020-02-20 23:34:02 +01:00
|
|
|
my $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
|
|
|
|
my $rootNodes = [ grep { ref($_) eq "HASH" } @{ $mainTree->{$rootNode} } ];
|
2020-01-10 21:29:11 +01:00
|
|
|
my @attributes = map { $self->_listNodeAttributes($_) } @$rootNodes;
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
return @attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _listNodeAttributes {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $node ) = @_;
|
2020-01-10 21:29:11 +01:00
|
|
|
my @attributes =
|
|
|
|
map { ref($_) eq "HASH" ? $self->_listNodeAttributes($_) : $_ }
|
|
|
|
@{ $node->{nodes} };
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
return @attributes;
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:52:44 +02:00
|
|
|
sub _translateOptionApiToConf {
|
|
|
|
my ( $self, $optionName, $prefix ) = @_;
|
2020-04-07 18:37:30 +02:00
|
|
|
|
|
|
|
# For consistency
|
|
|
|
$optionName =~ s/^clientId$/clientID/;
|
|
|
|
|
2020-04-06 22:52:44 +02:00
|
|
|
return $prefix . "MetaDataOptions" . ( ucfirst $optionName );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _translateOptionConfToApi {
|
|
|
|
my ( $self, $optionName ) = @_;
|
|
|
|
$optionName =~ s/^(\w+)MetaDataOptions//;
|
|
|
|
|
|
|
|
$optionName = lcfirst $optionName;
|
|
|
|
|
|
|
|
# iDToken looks ugly
|
|
|
|
$optionName =~ s/^iDToken/IDToken/;
|
2020-04-07 18:37:30 +02:00
|
|
|
|
|
|
|
# For consistency
|
|
|
|
$optionName =~ s/^clientID/clientId/;
|
2020-04-06 22:52:44 +02:00
|
|
|
return $optionName;
|
|
|
|
}
|
|
|
|
|
2020-12-28 14:50:45 +01:00
|
|
|
sub _translateValueConfToApi {
|
|
|
|
my ( $self, $optionName, $optionValue ) = @_;
|
|
|
|
if ( $optionName eq "oidcRPMetaDataOptionsRedirectUris" ) {
|
|
|
|
return [ split( /\s+/, $optionValue, ) ];
|
|
|
|
}
|
2020-12-28 15:10:35 +01:00
|
|
|
elsif ( $optionName eq "oidcRPMetaDataOptionsPostLogoutRedirectUris" ) {
|
|
|
|
return [ split( /\s+/, $optionValue, ) ];
|
|
|
|
}
|
2021-02-03 09:42:22 +01:00
|
|
|
elsif ( $optionName eq "oidcRPMetaDataOptionsAdditionalAudiences" ) {
|
|
|
|
return [ split( /\s+/, $optionValue, ) ];
|
|
|
|
}
|
2020-12-28 14:50:45 +01:00
|
|
|
else {
|
|
|
|
return $optionValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _translateValueApiToConf {
|
|
|
|
my ( $self, $optionName, $optionValue ) = @_;
|
|
|
|
|
|
|
|
# redirectUris is handled as an array
|
|
|
|
if ( $optionName eq 'redirectUris' ) {
|
2020-12-28 15:10:35 +01:00
|
|
|
die "redirectUris is not an array\n"
|
|
|
|
unless ( ref($optionValue) eq "ARRAY" );
|
|
|
|
return join( ' ', @{$optionValue} );
|
|
|
|
}
|
2020-12-28 15:44:47 +01:00
|
|
|
|
|
|
|
# postLogoutRedirectUris is handled as an array
|
2020-12-28 15:10:35 +01:00
|
|
|
elsif ( $optionName eq 'postLogoutRedirectUris' ) {
|
|
|
|
die "postLogoutRedirectUris is not an array\n"
|
2020-12-28 14:50:45 +01:00
|
|
|
unless ( ref($optionValue) eq "ARRAY" );
|
2021-02-03 09:42:22 +01:00
|
|
|
return join( ' ', @{$optionValue} );
|
|
|
|
}
|
|
|
|
|
|
|
|
# additionalAudiences is handled as an array
|
|
|
|
elsif ( $optionName eq 'additionalAudiences' ) {
|
|
|
|
die "postLogoutRedirectUris is not an array\n"
|
|
|
|
unless ( ref($optionValue) eq "ARRAY" );
|
2020-12-28 14:50:45 +01:00
|
|
|
return join( ' ', @{$optionValue} );
|
|
|
|
}
|
2020-12-28 15:44:47 +01:00
|
|
|
|
|
|
|
# Translate JSON booleans to integers
|
|
|
|
elsif ( JSON::is_bool($optionValue) ) {
|
|
|
|
return $optionValue ? 1 : 0;
|
|
|
|
}
|
2020-12-28 14:50:45 +01:00
|
|
|
else {
|
|
|
|
return $optionValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:00:22 +02:00
|
|
|
sub _getRegexpFromPattern {
|
|
|
|
my ( $self, $pattern ) = @_;
|
|
|
|
return unless ( $pattern =~ /[\w\.\-\*]+/ );
|
|
|
|
|
|
|
|
# . is allowed, and must be escaped
|
|
|
|
$pattern =~ s/\./\\\./g;
|
|
|
|
$pattern =~ s/\*/\.\*/g;
|
|
|
|
|
|
|
|
# anchor string, unless * was provided
|
|
|
|
$pattern = "^$pattern\$" if ( $pattern =~ /\*/ );
|
|
|
|
|
|
|
|
return qr/$pattern/;
|
|
|
|
}
|
|
|
|
|
2020-08-26 15:46:26 +02:00
|
|
|
sub _getPersistentMod {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $mod = $self->sessionTypes->{persistent};
|
|
|
|
$mod->{options}->{backend} = $mod->{module};
|
|
|
|
return $mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _getSSOMod {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $mod = $self->sessionTypes->{global};
|
|
|
|
$mod->{options}->{backend} = $mod->{module};
|
|
|
|
return $mod;
|
|
|
|
}
|
|
|
|
|
2020-12-28 17:48:09 +01:00
|
|
|
sub _saveApplyConf {
|
|
|
|
my ( $self, $conf ) = @_;
|
|
|
|
$self->_confAcc->saveConf($conf);
|
|
|
|
$self->applyConf($conf);
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
1;
|