| 1 |
package Lemonldap::NG::Manager::Api::Providers::SamlSp; |
| 2 |
|
| 3 |
our $VERSION = '2.23.0'; |
| 4 |
|
| 5 |
package Lemonldap::NG::Manager::Api; |
| 6 |
|
| 7 |
use strict; |
| 8 |
use utf8; |
| 9 |
use Mouse; |
| 10 |
|
| 11 |
extends 'Lemonldap::NG::Manager::Api::Common'; |
| 12 |
|
| 13 |
sub getSamlSpByConfKey { |
| 14 |
my ( $self, $req ) = @_; |
| 15 |
|
| 16 |
my $confKey = $req->params('confKey') |
| 17 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 18 |
|
| 19 |
$self->logger->debug("[API] SAML SP $confKey configuration requested"); |
| 20 |
|
| 21 |
# Get latest configuration |
| 22 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 23 |
my $samlSp = $self->_getSamlSpByConfKey( $conf, $confKey ); |
| 24 |
|
| 25 |
# Check if confKey is defined |
| 26 |
return $self->sendError( $req, |
| 27 |
"SAML service Provider '$confKey' not found", 404 ) |
| 28 |
unless ( defined $samlSp ); |
| 29 |
|
| 30 |
return $self->sendJSONresponse( $req, $samlSp ); |
| 31 |
} |
| 32 |
|
| 33 |
sub findSamlSpByConfKey { |
| 34 |
my ( $self, $req ) = @_; |
| 35 |
my $pattern = ( |
| 36 |
defined $req->params('uPattern') |
| 37 |
? $req->params('uPattern') |
| 38 |
: ( defined $req->params('pattern') ? $req->params('pattern') : undef ) |
| 39 |
); |
| 40 |
|
| 41 |
return $self->sendError( $req, 'Invalid input: pattern is missing', 400 ) |
| 42 |
unless ( defined $pattern ); |
| 43 |
|
| 44 |
unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) { |
| 45 |
return $self->sendError( $req, 'Invalid input: pattern is invalid', |
| 46 |
400 ); |
| 47 |
} |
| 48 |
|
| 49 |
$self->logger->debug( |
| 50 |
"[API] Find SAML SPs by confKey regexp $pattern requested"); |
| 51 |
|
| 52 |
# Get latest configuration |
| 53 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 54 |
my @samlSps = |
| 55 |
map { $_ =~ $pattern ? $self->_getSamlSpByConfKey( $conf, $_ ) : () } |
| 56 |
keys %{ $conf->{samlSPMetaDataXML} }; |
| 57 |
|
| 58 |
return $self->sendJSONresponse( $req, [@samlSps] ); |
| 59 |
} |
| 60 |
|
| 61 |
sub findSamlSpByEntityId { |
| 62 |
my ( $self, $req ) = @_; |
| 63 |
my $entityId = ( |
| 64 |
defined $req->params('uEntityId') ? $req->params('uEntityId') |
| 65 |
: ( |
| 66 |
defined $req->params('entityId') ? $req->params('entityId') |
| 67 |
: undef |
| 68 |
) |
| 69 |
); |
| 70 |
|
| 71 |
return $self->sendError( $req, 'entityId is missing', 400 ) |
| 72 |
unless ( defined $entityId ); |
| 73 |
|
| 74 |
$self->logger->debug("[API] Find SAML SPs by entityId $entityId requested"); |
| 75 |
|
| 76 |
# Get latest configuration |
| 77 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 78 |
my $samlSp = $self->_getSamlSpByEntityId( $conf, $entityId ); |
| 79 |
|
| 80 |
return $self->sendError( $req, |
| 81 |
"SAML service Provider with entityID '$entityId' not found", 404 ) |
| 82 |
unless ( defined $samlSp ); |
| 83 |
return $self->sendJSONresponse( $req, $samlSp ); |
| 84 |
} |
| 85 |
|
| 86 |
sub addSamlSp { |
| 87 |
my ( $self, $req ) = @_; |
| 88 |
my $add = $req->jsonBodyToObj; |
| 89 |
|
| 90 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 91 |
unless ($add); |
| 92 |
|
| 93 |
return $self->sendError( $req, 'Invalid input: confKey is missing', 400 ) |
| 94 |
unless ( defined $add->{confKey} ); |
| 95 |
|
| 96 |
my $option_entityid = |
| 97 |
$add->{options} ? $add->{options}->{federationEntityID} : undef; |
| 98 |
return $self->sendError( $req, 'Invalid input: metadata is missing', 400 ) |
| 99 |
unless ( defined $add->{metadata} or $option_entityid ); |
| 100 |
|
| 101 |
return $self->sendError( $req, 'Invalid input: confKey is empty', 400 ) |
| 102 |
unless ( $add->{confKey} ); |
| 103 |
|
| 104 |
my $entityId = |
| 105 |
$self->_readSamlSpEntityId( $add->{metadata} ) || $option_entityid; |
| 106 |
|
| 107 |
return $self->sendError( $req, |
| 108 |
'Invalid input: entityID is missing in metadata', 400 ) |
| 109 |
unless ( defined $entityId ); |
| 110 |
|
| 111 |
$self->logger->debug( |
| 112 |
"[API] Add SAML SP with confKey $add->{confKey} and entityID $entityId requested" |
| 113 |
); |
| 114 |
|
| 115 |
# Get latest configuration |
| 116 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 117 |
|
| 118 |
return $self->sendError( |
| 119 |
$req, |
| 120 |
"Invalid input: A SAML SP with confKey $add->{confKey} already exists", |
| 121 |
409 |
| 122 |
) if ( defined $self->_getSamlSpByConfKey( $conf, $add->{confKey} ) ); |
| 123 |
|
| 124 |
return $self->sendError( $req, |
| 125 |
"Invalid input: A SAML SP with entityID $entityId already exists", 409 ) |
| 126 |
if ( defined $self->_getSamlSpByEntityId( $conf, $entityId ) ); |
| 127 |
|
| 128 |
my $res = $self->_pushSamlSp( $conf, $add->{confKey}, $add, 1 ); |
| 129 |
|
| 130 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 131 |
unless ( $res->{res} eq 'ok' ); |
| 132 |
|
| 133 |
return $self->sendJSONresponse( |
| 134 |
$req, |
| 135 |
{ message => "Successful operation" }, |
| 136 |
code => 201 |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
sub replaceSamlSp { |
| 141 |
my ( $self, $req ) = @_; |
| 142 |
|
| 143 |
my $confKey = $req->params('confKey') |
| 144 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 145 |
|
| 146 |
my $replace = $req->jsonBodyToObj; |
| 147 |
|
| 148 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 149 |
unless ($replace); |
| 150 |
|
| 151 |
return $self->sendError( $req, 'Invalid input: metadata is missing', 400 ) |
| 152 |
unless ( defined $replace->{metadata} ); |
| 153 |
|
| 154 |
$self->logger->debug( |
| 155 |
"[API] SAML SP $confKey configuration replace requested"); |
| 156 |
|
| 157 |
# Get latest configuration |
| 158 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 159 |
|
| 160 |
# Return 404 if not found |
| 161 |
|
| 162 |
return $self->sendError( $req, |
| 163 |
"SAML service provider '$confKey' not found", 404 ) |
| 164 |
unless ( defined $self->_getSamlSpByConfKey( $conf, $confKey ) ); |
| 165 |
|
| 166 |
# check if new entityId exists already |
| 167 |
my $res = $self->_isNewSamlSpEntityIdUnique( $conf, $confKey, $replace ); |
| 168 |
|
| 169 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 170 |
unless ( $res->{res} eq 'ok' ); |
| 171 |
|
| 172 |
$res = $self->_pushSamlSp( $conf, $confKey, $replace, 1 ); |
| 173 |
|
| 174 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 175 |
unless ( $res->{res} eq 'ok' ); |
| 176 |
|
| 177 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 178 |
} |
| 179 |
|
| 180 |
sub updateSamlSp { |
| 181 |
my ( $self, $req ) = @_; |
| 182 |
my $res; |
| 183 |
my $confKey = $req->params('confKey') |
| 184 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 185 |
|
| 186 |
my $update = $req->jsonBodyToObj; |
| 187 |
|
| 188 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 189 |
unless ($update); |
| 190 |
|
| 191 |
$self->logger->debug( |
| 192 |
"[API] SAML SP $confKey configuration update requested"); |
| 193 |
|
| 194 |
# Get latest configuration |
| 195 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 196 |
my $current = $self->_getSamlSpByConfKey( $conf, $confKey ); |
| 197 |
|
| 198 |
# Return 404 if not found |
| 199 |
return $self->sendError( $req, |
| 200 |
"SAML service provider '$confKey' not found", 404 ) |
| 201 |
unless ( defined $current ); |
| 202 |
|
| 203 |
if ( defined $update->{metadata} ) { |
| 204 |
|
| 205 |
# check if new entityId exists already |
| 206 |
$res = $self->_isNewSamlSpEntityIdUnique( $conf, $confKey, $update ); |
| 207 |
|
| 208 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 209 |
unless ( $res->{res} eq 'ok' ); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
$res = $self->_pushSamlSp( $conf, $confKey, $update, 0 ); |
| 214 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 215 |
unless ( $res->{res} eq 'ok' ); |
| 216 |
|
| 217 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 218 |
} |
| 219 |
|
| 220 |
sub deleteSamlSp { |
| 221 |
my ( $self, $req ) = @_; |
| 222 |
|
| 223 |
my $confKey = $req->params('confKey') |
| 224 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 225 |
|
| 226 |
# Get latest configuration |
| 227 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 228 |
|
| 229 |
my $delete = $self->_getSamlSpByConfKey( $conf, $confKey ); |
| 230 |
|
| 231 |
# Return 404 if not found |
| 232 |
|
| 233 |
return $self->sendError( $req, |
| 234 |
"SAML service provider '$confKey' not found", 404 ) |
| 235 |
unless ( defined $delete ); |
| 236 |
|
| 237 |
delete $conf->{samlSPMetaDataXML}->{$confKey}; |
| 238 |
delete $conf->{samlSPMetaDataOptions}->{$confKey}; |
| 239 |
delete $conf->{samlSPMetaDataExportedAttributes}->{$confKey}; |
| 240 |
delete $conf->{samlSPMetaDataMacros}->{$confKey}; |
| 241 |
|
| 242 |
# Save configuration |
| 243 |
$self->_saveApplyConf($conf); |
| 244 |
|
| 245 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 246 |
} |
| 247 |
|
| 248 |
sub _getSamlSpByConfKey { |
| 249 |
my ( $self, $conf, $confKey ) = @_; |
| 250 |
|
| 251 |
# Check if confKey is defined |
| 252 |
return undef unless ( defined $conf->{samlSPMetaDataXML}->{$confKey} ); |
| 253 |
|
| 254 |
# Get metadata |
| 255 |
my $metadata = $conf->{samlSPMetaDataXML}->{$confKey}->{samlSPMetaDataXML}; |
| 256 |
|
| 257 |
# Get options |
| 258 |
my $options = {}; |
| 259 |
for my $confOption ( keys %{ $conf->{samlSPMetaDataOptions}->{$confKey} } ) |
| 260 |
{ |
| 261 |
my $optionName = $self->_translateOptionConfToApi($confOption); |
| 262 |
my $optionValue = $self->_translateValueConfToApi( $confOption, |
| 263 |
$conf->{samlSPMetaDataOptions}->{$confKey}->{$confOption} ); |
| 264 |
|
| 265 |
$options->{$optionName} = $optionValue; |
| 266 |
} |
| 267 |
|
| 268 |
# Get macros |
| 269 |
my $macros = $conf->{samlSPMetaDataMacros}->{$confKey} || {}; |
| 270 |
|
| 271 |
my $samlSp = { |
| 272 |
confKey => $confKey, |
| 273 |
metadata => $metadata, |
| 274 |
exportedAttributes => {}, |
| 275 |
macros => $macros, |
| 276 |
options => $options |
| 277 |
}; |
| 278 |
|
| 279 |
# Get exported attributes |
| 280 |
foreach ( keys %{ $conf->{samlSPMetaDataExportedAttributes}->{$confKey} } ) |
| 281 |
{ |
| 282 |
# Extract fields from exportedAttr value |
| 283 |
my ( $mandatory, $name, $format, $friendly_name ) = |
| 284 |
split( /;/, |
| 285 |
$conf->{samlSPMetaDataExportedAttributes}->{$confKey}->{$_} ); |
| 286 |
|
| 287 |
$mandatory = !!$mandatory ? 'true' : 'false'; # ???????????? |
| 288 |
|
| 289 |
$samlSp->{exportedAttributes}->{$_} = { |
| 290 |
name => $name, |
| 291 |
mandatory => $mandatory |
| 292 |
}; |
| 293 |
|
| 294 |
$samlSp->{exportedAttributes}->{$_}->{friendlyName} = $friendly_name |
| 295 |
if ( defined $friendly_name && $friendly_name ne '' ); |
| 296 |
|
| 297 |
$samlSp->{exportedAttributes}->{$_}->{format} = $format |
| 298 |
if ( defined $format && $format ne '' ); |
| 299 |
} |
| 300 |
|
| 301 |
return $samlSp; |
| 302 |
} |
| 303 |
|
| 304 |
sub _getSamlSpByEntityId { |
| 305 |
my ( $self, $conf, $entityId ) = @_; |
| 306 |
|
| 307 |
foreach ( keys %{ $conf->{samlSPMetaDataXML} } ) { |
| 308 |
my $metadata_entityId = $self->_readSamlSpEntityId( |
| 309 |
$conf->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML} ) |
| 310 |
|| ''; |
| 311 |
return $self->_getSamlSpByConfKey( $conf, $_ ) |
| 312 |
if ( $metadata_entityId eq $entityId ); |
| 313 |
} |
| 314 |
|
| 315 |
foreach ( keys %{ $conf->{samlSPMetaDataOptions} } ) { |
| 316 |
my $federated_entityId = $conf->{samlSPMetaDataOptions}->{$_} |
| 317 |
->{samlSPMetaDataOptionsFederationEntityID} || ''; |
| 318 |
return $self->_getSamlSpByConfKey( $conf, $_ ) |
| 319 |
if ( $federated_entityId eq $entityId ); |
| 320 |
} |
| 321 |
|
| 322 |
return undef; |
| 323 |
} |
| 324 |
|
| 325 |
sub _readSamlSpEntityId { |
| 326 |
my ( $self, $metadata ) = @_; |
| 327 |
|
| 328 |
return undef unless $metadata; |
| 329 |
my ($entityID) = $metadata =~ /entityID=['"](.+?)['"]/; |
| 330 |
return $entityID; |
| 331 |
} |
| 332 |
|
| 333 |
sub _readSamlSpExportedAttributes { |
| 334 |
my ( $self, $attrs, $mergeWith ) = @_; |
| 335 |
my $allowedFormats = [ |
| 336 |
"urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified", |
| 337 |
"urn:oasis:names:tc:SAML:2.0:attrname-format:uri", |
| 338 |
"urn:oasis:names:tc:SAML:2.0:attrname-format:basic" |
| 339 |
]; |
| 340 |
foreach ( keys %{$attrs} ) { |
| 341 |
if ( defined( $attrs->{$_} ) ) { |
| 342 |
return { res => "ko", msg => "Exported attribute $_ has no name" } |
| 343 |
unless ( defined $attrs->{$_}->{name} ); |
| 344 |
my $mandatory = 0; |
| 345 |
my $name = $attrs->{$_}->{name}; |
| 346 |
my $format = ''; |
| 347 |
my $friendlyName = ''; |
| 348 |
|
| 349 |
( $mandatory, $name, $format, $friendlyName ) = |
| 350 |
split( /;/, $mergeWith->{$_} ) |
| 351 |
if ( defined $mergeWith->{$_} ); |
| 352 |
|
| 353 |
if ( defined $attrs->{$_}->{mandatory} ) { |
| 354 |
$mandatory = ( |
| 355 |
$attrs->{$_}->{mandatory} eq '1' |
| 356 |
or $attrs->{$_}->{mandatory} eq 'true' |
| 357 |
) ? 1 : 0; |
| 358 |
} |
| 359 |
|
| 360 |
if ( defined $attrs->{$_}->{format} ) { |
| 361 |
$format = $attrs->{$_}->{format}; |
| 362 |
return { |
| 363 |
res => "ko", |
| 364 |
msg => "Exported attribute $_ format does not exist." |
| 365 |
} |
| 366 |
unless ( length( grep { /^$format$/ } @{$allowedFormats} ) ); |
| 367 |
} |
| 368 |
|
| 369 |
$friendlyName = $attrs->{$_}->{friendlyName} |
| 370 |
if ( defined $attrs->{$_}->{friendlyName} ); |
| 371 |
$mergeWith->{$_} = "$mandatory;$name;$format;$friendlyName"; |
| 372 |
} |
| 373 |
else { |
| 374 |
delete $mergeWith->{$_}; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return { res => "ok", exportedAttributes => $mergeWith }; |
| 379 |
} |
| 380 |
|
| 381 |
sub _pushSamlSp { |
| 382 |
my ( $self, $conf, $confKey, $push, $replace ) = @_; |
| 383 |
|
| 384 |
my $translatedOptions = {}; |
| 385 |
if ($replace) { |
| 386 |
$conf->{samlSPMetaDataXML}->{$confKey} = {}; |
| 387 |
$conf->{samlSPMetaDataOptions}->{$confKey} = {}; |
| 388 |
$conf->{samlSPMetaDataMacros}->{$confKey} = {}; |
| 389 |
$conf->{samlSPMetaDataExportedAttributes}->{$confKey} = {}; |
| 390 |
$translatedOptions = $self->_getDefaultValues('samlSPMetaDataNode'); |
| 391 |
} |
| 392 |
|
| 393 |
$conf->{samlSPMetaDataXML}->{$confKey}->{samlSPMetaDataXML} = |
| 394 |
$push->{metadata} |
| 395 |
if defined $push->{metadata}; |
| 396 |
|
| 397 |
if ( defined $push->{options} ) { |
| 398 |
|
| 399 |
foreach ( keys %{ $push->{options} } ) { |
| 400 |
my $optionName = $self->_translateOptionApiToConf( $_, 'samlSP' ); |
| 401 |
my $optionValue = |
| 402 |
$self->_translateValueApiToConf( $optionName, |
| 403 |
$push->{options}->{$_} ); |
| 404 |
$translatedOptions->{$optionName} = $optionValue; |
| 405 |
} |
| 406 |
|
| 407 |
my $res = $self->_hasAllowedAttributes( $translatedOptions, |
| 408 |
'samlSPMetaDataNode' ); |
| 409 |
return $res unless ( $res->{res} eq 'ok' ); |
| 410 |
$self->_merge_hash( $conf, 'samlSPMetaDataOptions', $confKey, |
| 411 |
$translatedOptions ); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
if ( defined $push->{macros} ) { |
| 416 |
if ( $self->_isSimpleKeyValueHash( $push->{macros} ) ) { |
| 417 |
$self->_merge_hash( $conf, 'samlSPMetaDataMacros', $confKey, |
| 418 |
$push->{macros} ); |
| 419 |
} |
| 420 |
else { |
| 421 |
return { |
| 422 |
res => 'ko', |
| 423 |
msg => |
| 424 |
"Invalid input: macros is not a hash object with \"key\":\"value\" attributes" |
| 425 |
}; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if ( defined $push->{exportedAttributes} ) { |
| 430 |
my $res = |
| 431 |
$self->_readSamlSpExportedAttributes( $push->{exportedAttributes}, |
| 432 |
$conf->{samlSPMetaDataExportedAttributes}->{$confKey} ); |
| 433 |
return $res unless ( $res->{res} eq 'ok' ); |
| 434 |
|
| 435 |
$conf->{samlSPMetaDataExportedAttributes}->{$confKey} = |
| 436 |
$res->{exportedAttributes}; |
| 437 |
} |
| 438 |
|
| 439 |
# Test new configuration |
| 440 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( { |
| 441 |
refConf => $self->_confAcc->getConf( { noCache => 1 } ), |
| 442 |
newConf => $conf, |
| 443 |
req => {}, |
| 444 |
} |
| 445 |
); |
| 446 |
unless ( $parser->testNewConf( $self->p ) ) { |
| 447 |
return { |
| 448 |
res => 'ko', |
| 449 |
code => 400, |
| 450 |
msg => "Configuration error: " |
| 451 |
. join( ". ", map { $_->{message} } @{ $parser->errors } ), |
| 452 |
}; |
| 453 |
} |
| 454 |
|
| 455 |
# Save configuration |
| 456 |
$self->_saveApplyConf($conf); |
| 457 |
|
| 458 |
return { res => 'ok' }; |
| 459 |
} |
| 460 |
|
| 461 |
sub _isNewSamlSpEntityIdUnique { |
| 462 |
my ( $self, $conf, $confKey, $newSp ) = @_; |
| 463 |
|
| 464 |
my $option_entityid = |
| 465 |
$newSp->{options} ? $newSp->{options}->{federationEntityID} : undef; |
| 466 |
my $newEntityId = |
| 467 |
$option_entityid || $self->_readSamlSpEntityId( $newSp->{metadata} ); |
| 468 |
my $curEntityId = |
| 469 |
$self->_readSamlSpEntityId( |
| 470 |
$self->_getSamlSpByConfKey( $conf, $confKey )->{metadata} ); |
| 471 |
if ( $newEntityId ne $curEntityId ) { |
| 472 |
return { |
| 473 |
res => 'ko', |
| 474 |
msg => |
| 475 |
"An SAML service provide with entityId '$newEntityId' already exists" |
| 476 |
} |
| 477 |
if ( defined $self->_getSamlSpByEntityId( $conf, $newEntityId ) ); |
| 478 |
} |
| 479 |
|
| 480 |
return { res => 'ok' }; |
| 481 |
} |
| 482 |
|
| 483 |
1; |