| 1 |
package Lemonldap::NG::Manager::Api::Common; |
| 2 |
|
| 3 |
our $VERSION = '2.23.0'; |
| 4 |
|
| 5 |
package Lemonldap::NG::Manager::Api; |
| 6 |
|
| 7 |
use strict; |
| 8 |
use Exporter 'import'; |
| 9 |
use Lemonldap::NG::Manager::Build::Attributes; |
| 10 |
use Lemonldap::NG::Manager::Build::CTrees; |
| 11 |
|
| 12 |
our @EXPORTS = qw(&_listAttributes); |
| 13 |
|
| 14 |
# use Scalar::Util 'weaken'; ? |
| 15 |
|
| 16 |
my @arrayize = qw( |
| 17 |
oidcRPMetaDataOptionsRedirectUris |
| 18 |
oidcRPMetaDataOptionsPostLogoutRedirectUris |
| 19 |
oidcRPMetaDataOptionsAdditionalAudiences |
| 20 |
casAppMetaDataOptionsService |
| 21 |
oidcRPMetaDataOptionsRequestUris |
| 22 |
); |
| 23 |
|
| 24 |
sub _mustArrayizeOption { |
| 25 |
my ( $self, $option ) = @_; |
| 26 |
return ( grep { $_ eq $option } @arrayize ) ? 1 : 0; |
| 27 |
} |
| 28 |
|
| 29 |
sub _isSimpleKeyValueHash { |
| 30 |
my ( $self, $hash ) = @_; |
| 31 |
return 0 if ( ref($hash) ne "HASH" ); |
| 32 |
|
| 33 |
foreach ( keys %$hash ) { |
| 34 |
return 0 if ( ref( $hash->{$_} ) ne '' || ref($_) ne '' ); |
| 35 |
} |
| 36 |
|
| 37 |
return 1; |
| 38 |
} |
| 39 |
|
| 40 |
sub _getDefaultValues { |
| 41 |
my ( $self, $rootNode ) = @_; |
| 42 |
my @allAttrs = $self->_listAttributes($rootNode); |
| 43 |
my $defaultAttrs = Lemonldap::NG::Manager::Build::Attributes::attributes(); |
| 44 |
my $attrs = {}; |
| 45 |
|
| 46 |
foreach my $attr (@allAttrs) { |
| 47 |
$attrs->{$attr} = $defaultAttrs->{$attr}->{default} |
| 48 |
if ( defined $defaultAttrs->{$attr} |
| 49 |
&& defined $defaultAttrs->{$attr}->{default} ); |
| 50 |
} |
| 51 |
|
| 52 |
return $attrs; |
| 53 |
} |
| 54 |
|
| 55 |
sub _hasAllowedAttributes { |
| 56 |
my ( $self, $attributes, $rootNode ) = @_; |
| 57 |
my @allowedAttributes = $self->_listAttributes($rootNode); |
| 58 |
|
| 59 |
foreach my $attribute ( keys %{$attributes} ) { |
| 60 |
if ( length( ref($attribute) ) ) { |
| 61 |
return { |
| 62 |
res => "ko", |
| 63 |
msg => "Invalid input: Attribute $attribute is not a string." |
| 64 |
}; |
| 65 |
} |
| 66 |
unless ( grep { $_ eq $attribute } @allowedAttributes ) { |
| 67 |
return { |
| 68 |
res => "ko", |
| 69 |
msg => "Invalid input: Attribute $attribute does not exist." |
| 70 |
}; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return { res => "ok" }; |
| 75 |
} |
| 76 |
|
| 77 |
sub _listAttributes { |
| 78 |
my ( $self, $rootNode ) = @_; |
| 79 |
my $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees(); |
| 80 |
my $rootNodes = [ grep { ref($_) eq "HASH" } @{ $mainTree->{$rootNode} } ]; |
| 81 |
my @attributes = map { $self->_listNodeAttributes($_) } @$rootNodes; |
| 82 |
|
| 83 |
return @attributes; |
| 84 |
} |
| 85 |
|
| 86 |
sub _listNodeAttributes { |
| 87 |
my ( $self, $node ) = @_; |
| 88 |
my @attributes = |
| 89 |
map { ref($_) eq "HASH" ? $self->_listNodeAttributes($_) : $_ } |
| 90 |
@{ $node->{nodes} }; |
| 91 |
|
| 92 |
return @attributes; |
| 93 |
} |
| 94 |
|
| 95 |
sub _translateOptionApiToConf { |
| 96 |
my ( $self, $optionName, $prefix ) = @_; |
| 97 |
|
| 98 |
# For consistency |
| 99 |
$optionName =~ s/^clientId$/clientID/; |
| 100 |
|
| 101 |
return $prefix . "MetaDataOptions" . ( ucfirst $optionName ); |
| 102 |
} |
| 103 |
|
| 104 |
sub _translateOptionConfToApi { |
| 105 |
my ( $self, $optionName ) = @_; |
| 106 |
$optionName =~ s/^(\w+)MetaDataOptions//; |
| 107 |
|
| 108 |
$optionName = lcfirst $optionName; |
| 109 |
|
| 110 |
# iDToken looks ugly |
| 111 |
$optionName =~ s/^iDToken/IDToken/; |
| 112 |
|
| 113 |
# For consistency |
| 114 |
$optionName =~ s/^clientID/clientId/; |
| 115 |
return $optionName; |
| 116 |
} |
| 117 |
|
| 118 |
sub _translateValueConfToApi { |
| 119 |
my ( $self, $optionName, $optionValue ) = @_; |
| 120 |
|
| 121 |
if ( $self->_mustArrayizeOption($optionName) ) { |
| 122 |
return [ split( /\s+/, $optionValue, ) ]; |
| 123 |
} |
| 124 |
else { |
| 125 |
return $optionValue; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
sub _translateValueApiToConf { |
| 130 |
my ( $self, $optionName, $optionValue ) = @_; |
| 131 |
|
| 132 |
if ( $self->_mustArrayizeOption($optionName) ) { |
| 133 |
die "$optionName is not an array\n" |
| 134 |
unless ( ref($optionValue) eq "ARRAY" ); |
| 135 |
return join( ' ', @{$optionValue} ); |
| 136 |
} |
| 137 |
|
| 138 |
# Translate JSON booleans to integers |
| 139 |
elsif ( JSON::is_bool($optionValue) ) { |
| 140 |
return $optionValue ? 1 : 0; |
| 141 |
} |
| 142 |
else { |
| 143 |
return $optionValue; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
sub _getRegexpFromPattern { |
| 148 |
my ( $self, $pattern ) = @_; |
| 149 |
return unless ( $pattern =~ /[\w\.\-\*]+/ ); |
| 150 |
|
| 151 |
# . is allowed, and must be escaped |
| 152 |
$pattern =~ s/\./\\\./g; |
| 153 |
$pattern =~ s/\*/\.\*/g; |
| 154 |
|
| 155 |
# anchor string, unless * was provided |
| 156 |
$pattern = "^$pattern\$" if ( $pattern =~ /\*/ ); |
| 157 |
|
| 158 |
return qr/$pattern/; |
| 159 |
} |
| 160 |
|
| 161 |
sub _getPersistentMod { |
| 162 |
my ($self) = @_; |
| 163 |
my $mod = $self->sessionTypes->{persistent}; |
| 164 |
$mod->{options}->{backend} = $mod->{module}; |
| 165 |
return $mod; |
| 166 |
} |
| 167 |
|
| 168 |
sub _getSSOMod { |
| 169 |
my ($self) = @_; |
| 170 |
my $mod = $self->sessionTypes->{global}; |
| 171 |
$mod->{options}->{backend} = $mod->{module}; |
| 172 |
return $mod; |
| 173 |
} |
| 174 |
|
| 175 |
sub _saveApplyConf { |
| 176 |
my ( $self, $conf ) = @_; |
| 177 |
$self->_confAcc->saveConf($conf); |
| 178 |
$self->applyConf($conf); |
| 179 |
} |
| 180 |
|
| 181 |
sub _merge_hash { |
| 182 |
my ( $self, $conf, $option, $provider, $hash_to_merge ) = @_; |
| 183 |
|
| 184 |
while ( my ( $key, $value ) = each %{$hash_to_merge} ) { |
| 185 |
if ( defined $value ) { |
| 186 |
$conf->{$option}->{$provider}->{$key} = $value; |
| 187 |
} |
| 188 |
else { |
| 189 |
delete $conf->{$option}->{$provider}->{$key}; |
| 190 |
} |
| 191 |
} |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
1; |