| 1 |
package Lemonldap::NG::Manager::Api::Providers::CasApp; |
| 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 |
use Lemonldap::NG::Manager::Conf::Parser; |
| 11 |
|
| 12 |
extends 'Lemonldap::NG::Manager::Api::Common'; |
| 13 |
|
| 14 |
sub getCasAppByConfKey { |
| 15 |
my ( $self, $req ) = @_; |
| 16 |
|
| 17 |
my $confKey = $req->params('confKey') |
| 18 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 19 |
|
| 20 |
$self->logger->debug("[API] CAS App $confKey configuration requested"); |
| 21 |
|
| 22 |
# Get latest configuration |
| 23 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 24 |
|
| 25 |
my $casApp = $self->_getCasAppByConfKey( $conf, $confKey ); |
| 26 |
|
| 27 |
# Return 404 if not found |
| 28 |
return $self->sendError( $req, "CAS application '$confKey' not found", 404 ) |
| 29 |
unless ( defined $casApp ); |
| 30 |
|
| 31 |
return $self->sendJSONresponse( $req, $casApp ); |
| 32 |
} |
| 33 |
|
| 34 |
sub findCasAppByConfKey { |
| 35 |
my ( $self, $req ) = @_; |
| 36 |
|
| 37 |
my $pattern = ( |
| 38 |
defined $req->params('uPattern') |
| 39 |
? $req->params('uPattern') |
| 40 |
: ( defined $req->params('pattern') ? $req->params('pattern') : undef ) |
| 41 |
); |
| 42 |
|
| 43 |
return $self->sendError( $req, 'Invalid input: pattern is missing', 400 ) |
| 44 |
unless ( defined $pattern ); |
| 45 |
|
| 46 |
unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) { |
| 47 |
return $self->sendError( $req, 'Invalid input: pattern is invalid', |
| 48 |
400 ); |
| 49 |
} |
| 50 |
|
| 51 |
$self->logger->debug( |
| 52 |
"[API] Find CAS Apps by confKey regexp $pattern requested"); |
| 53 |
|
| 54 |
# Get latest configuration |
| 55 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 56 |
|
| 57 |
my @casApps = |
| 58 |
map { $_ =~ $pattern ? $self->_getCasAppByConfKey( $conf, $_ ) : () } |
| 59 |
keys %{ $conf->{casAppMetaDataOptions} }; |
| 60 |
|
| 61 |
return $self->sendJSONresponse( $req, [@casApps] ); |
| 62 |
} |
| 63 |
|
| 64 |
sub findCasAppsByServiceUrl { |
| 65 |
my ( $self, $req ) = @_; |
| 66 |
|
| 67 |
my $serviceUrl = ( |
| 68 |
defined $req->params('uServiceUrl') ? $req->params('uServiceUrl') |
| 69 |
: ( |
| 70 |
defined $req->params('serviceUrl') ? $req->params('serviceUrl') |
| 71 |
: undef |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
return $self->sendError( $req, 'Invalid input: serviceUrl is missing', 400 ) |
| 76 |
unless ( defined $serviceUrl ); |
| 77 |
|
| 78 |
$self->logger->debug( |
| 79 |
"[API] Find CAS Apps by service URL $serviceUrl requested"); |
| 80 |
|
| 81 |
# Get latest configuration |
| 82 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 83 |
|
| 84 |
my $casApp = $self->_getCasAppByServiceUrl( $conf, $serviceUrl ); |
| 85 |
return $self->sendError( $req, |
| 86 |
"CAS application with service '$serviceUrl' not found", 404 ) |
| 87 |
unless ( defined $casApp ); |
| 88 |
|
| 89 |
return $self->sendJSONresponse( $req, $casApp ); |
| 90 |
} |
| 91 |
|
| 92 |
sub addCasApp { |
| 93 |
my ( $self, $req ) = @_; |
| 94 |
my $add = $req->jsonBodyToObj; |
| 95 |
|
| 96 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 97 |
unless ($add); |
| 98 |
|
| 99 |
return $self->sendError( $req, 'Invalid input: confKey is missing', 400 ) |
| 100 |
unless ( defined $add->{confKey} ); |
| 101 |
|
| 102 |
return $self->sendError( $req, 'Invalid input: confKey is not a string', |
| 103 |
400 ) |
| 104 |
if ( ref $add->{confKey} ); |
| 105 |
|
| 106 |
return $self->sendError( $req, 'Invalid input: confKey is empty', 400 ) |
| 107 |
unless ( $add->{confKey} ); |
| 108 |
|
| 109 |
return $self->sendError( $req, 'Invalid input: service is missing', 400 ) |
| 110 |
unless ( defined $add->{options}->{service} ); |
| 111 |
|
| 112 |
return $self->sendError( $req, 'Invalid input: service must be an array', |
| 113 |
400 ) |
| 114 |
unless ( ref $add->{options}->{service} eq "ARRAY" ); |
| 115 |
|
| 116 |
$self->logger->debug( |
| 117 |
"[API] Add CAS App with confKey $add->{confKey} requested"); |
| 118 |
|
| 119 |
# Get latest configuration |
| 120 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 121 |
|
| 122 |
return $self->sendError( |
| 123 |
$req, |
| 124 |
"Invalid input: A CAS App with confKey $add->{confKey} already exists", |
| 125 |
409 |
| 126 |
) if ( defined $self->_getCasAppByConfKey( $conf, $add->{confKey} ) ); |
| 127 |
|
| 128 |
for my $serviceUrl ( @{ $add->{options}->{service} } ) { |
| 129 |
my $res = $self->_getCasAppByServiceUrl( $conf, $serviceUrl ); |
| 130 |
if ( defined $res ) { |
| 131 |
return $self->sendError( |
| 132 |
$req, |
| 133 |
"Invalid input: A CAS application with service URL $serviceUrl already exists", |
| 134 |
409 |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
my $res = $self->_pushCasApp( $conf, $add->{confKey}, $add, 1 ); |
| 140 |
|
| 141 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 142 |
unless ( $res->{res} eq 'ok' ); |
| 143 |
|
| 144 |
return $self->sendJSONresponse( |
| 145 |
$req, |
| 146 |
{ message => "Successful operation" }, |
| 147 |
code => 201 |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
sub updateCasApp { |
| 152 |
my ( $self, $req ) = @_; |
| 153 |
my $confKey = $req->params('confKey') |
| 154 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 155 |
|
| 156 |
my $update = $req->jsonBodyToObj; |
| 157 |
|
| 158 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 159 |
unless ($update); |
| 160 |
|
| 161 |
$self->logger->debug( |
| 162 |
"[API] CAS App $confKey configuration update requested"); |
| 163 |
|
| 164 |
# Get latest configuration |
| 165 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 166 |
|
| 167 |
my $current = $self->_getCasAppByConfKey( $conf, $confKey ); |
| 168 |
|
| 169 |
# Return 404 if not found |
| 170 |
|
| 171 |
return $self->sendError( $req, "CAS application '$confKey' not found", 404 ) |
| 172 |
unless ( defined $current ); |
| 173 |
|
| 174 |
# check if new clientID exists already |
| 175 |
my $res = $self->_isNewCasAppServiceUrlUnique( $conf, $confKey, $update ); |
| 176 |
|
| 177 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 178 |
unless ( $res->{res} eq 'ok' ); |
| 179 |
|
| 180 |
$res = $self->_pushCasApp( $conf, $confKey, $update, 0 ); |
| 181 |
|
| 182 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 183 |
unless ( $res->{res} eq 'ok' ); |
| 184 |
|
| 185 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 186 |
} |
| 187 |
|
| 188 |
sub replaceCasApp { |
| 189 |
my ( $self, $req ) = @_; |
| 190 |
my $confKey = $req->params('confKey') |
| 191 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 192 |
|
| 193 |
my $replace = $req->jsonBodyToObj; |
| 194 |
|
| 195 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 196 |
unless ($replace); |
| 197 |
|
| 198 |
$self->logger->debug( |
| 199 |
"[API] CAS App $confKey configuration replace requested"); |
| 200 |
|
| 201 |
# Get latest configuration |
| 202 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 203 |
|
| 204 |
# Return 404 if not found |
| 205 |
|
| 206 |
return $self->sendError( $req, "CAS application '$confKey' not found", 404 ) |
| 207 |
unless ( defined $self->_getCasAppByConfKey( $conf, $confKey ) ); |
| 208 |
|
| 209 |
# check if new clientID exists already |
| 210 |
my $res = $self->_isNewCasAppServiceUrlUnique( $conf, $confKey, $replace ); |
| 211 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 212 |
unless ( $res->{res} eq 'ok' ); |
| 213 |
|
| 214 |
$res = $self->_pushCasApp( $conf, $confKey, $replace, 1 ); |
| 215 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 216 |
unless ( $res->{res} eq 'ok' ); |
| 217 |
|
| 218 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 219 |
} |
| 220 |
|
| 221 |
sub deleteCasApp { |
| 222 |
my ( $self, $req ) = @_; |
| 223 |
my $confKey = $req->params('confKey') |
| 224 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 225 |
|
| 226 |
$self->logger->debug( |
| 227 |
"[API] CAS App $confKey configuration delete requested"); |
| 228 |
|
| 229 |
# Get latest configuration |
| 230 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 231 |
|
| 232 |
my $delete = $self->_getCasAppByConfKey( $conf, $confKey ); |
| 233 |
|
| 234 |
# Return 404 if not found |
| 235 |
|
| 236 |
return $self->sendError( $req, "CAS application '$confKey' not found", 404 ) |
| 237 |
unless ( defined $delete ); |
| 238 |
|
| 239 |
delete $conf->{casAppMetaDataOptions}->{$confKey}; |
| 240 |
delete $conf->{casAppMetaDataExportedVars}->{$confKey}; |
| 241 |
delete $conf->{casAppMetaDataMacros}->{$confKey}; |
| 242 |
|
| 243 |
# Save configuration |
| 244 |
$self->_saveApplyConf($conf); |
| 245 |
|
| 246 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 247 |
} |
| 248 |
|
| 249 |
sub _getCasAppByConfKey { |
| 250 |
my ( $self, $conf, $confKey ) = @_; |
| 251 |
|
| 252 |
# Check if confKey is defined |
| 253 |
return undef unless ( defined $conf->{casAppMetaDataOptions}->{$confKey} ); |
| 254 |
|
| 255 |
# Get exported vars |
| 256 |
my $exportedVars = $conf->{casAppMetaDataExportedVars}->{$confKey}; |
| 257 |
|
| 258 |
# # Get extra claim |
| 259 |
# my $extraClaims = $conf->{casAppMetaDataOptionsExtraClaims}->{$confKey}; |
| 260 |
|
| 261 |
# Get macros |
| 262 |
my $macros = $conf->{casAppMetaDataMacros}->{$confKey} || {}; |
| 263 |
|
| 264 |
# Get options |
| 265 |
my $options = {}; |
| 266 |
for |
| 267 |
my $configOption ( keys %{ $conf->{casAppMetaDataOptions}->{$confKey} } ) |
| 268 |
{ |
| 269 |
my $optionName = $self->_translateOptionConfToApi($configOption); |
| 270 |
my $optionValue = $self->_translateValueConfToApi( $configOption, |
| 271 |
$conf->{casAppMetaDataOptions}->{$confKey}->{$configOption} ); |
| 272 |
$options->{$optionName} = $optionValue; |
| 273 |
} |
| 274 |
|
| 275 |
return { |
| 276 |
confKey => $confKey, |
| 277 |
exportedVars => $exportedVars, |
| 278 |
macros => $macros, |
| 279 |
options => $options |
| 280 |
}; |
| 281 |
} |
| 282 |
|
| 283 |
sub _getCasAppByServiceUrl { |
| 284 |
my ( $self, $conf, $serviceUrl ) = @_; |
| 285 |
|
| 286 |
my ($serviceHost) = $serviceUrl =~ m#^(https?://[^/]+)(?:/.*)?$#; |
| 287 |
return undef unless $serviceHost; |
| 288 |
for my $confKey ( keys %{ $conf->{casAppMetaDataOptions} } ) { |
| 289 |
for my $url ( |
| 290 |
split( |
| 291 |
/\s+/, |
| 292 |
$conf->{casAppMetaDataOptions}->{$confKey} |
| 293 |
->{casAppMetaDataOptionsService} |
| 294 |
) |
| 295 |
) |
| 296 |
{ |
| 297 |
my ($curHost) = $url =~ m#^(https?://[^/]+)(?:/.*)?$#; |
| 298 |
if ( $serviceHost eq $curHost ) { |
| 299 |
return $self->_getCasAppByConfKey( $conf, $confKey ); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return undef; |
| 305 |
} |
| 306 |
|
| 307 |
sub _isNewCasAppServiceUrlUnique { |
| 308 |
my ( $self, $conf, $confKey, $casApp ) = @_; |
| 309 |
my $curServiceUrl = |
| 310 |
$self->_getCasAppByConfKey( $conf, $confKey )->{options}->{service}; |
| 311 |
|
| 312 |
# Check service paramater |
| 313 |
unless ( ref $casApp->{options}->{service} eq "ARRAY" ) { |
| 314 |
return { |
| 315 |
res => 'ko', |
| 316 |
msg => "The parameter 'service' must be an array", |
| 317 |
}; |
| 318 |
} |
| 319 |
|
| 320 |
my $newService = $casApp->{options}->{service} || []; |
| 321 |
for my $newServiceUrl (@$newService) { |
| 322 |
if ( $newServiceUrl ne '' |
| 323 |
&& !grep( /^$newServiceUrl$/, @$curServiceUrl ) ) |
| 324 |
{ |
| 325 |
return { |
| 326 |
res => 'ko', |
| 327 |
msg => |
| 328 |
"A CAS application with service URL '$newServiceUrl' already exists" |
| 329 |
} |
| 330 |
if ( |
| 331 |
defined $self->_getCasAppByServiceUrl( $conf, $newServiceUrl ) |
| 332 |
); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return { res => 'ok' }; |
| 337 |
} |
| 338 |
|
| 339 |
sub _pushCasApp { |
| 340 |
my ( $self, $conf, $confKey, $push, $replace ) = @_; |
| 341 |
|
| 342 |
my $translatedOptions = {}; |
| 343 |
if ($replace) { |
| 344 |
$conf->{casAppMetaDataOptions}->{$confKey} = {}; |
| 345 |
$conf->{casAppMetaDataExportedVars}->{$confKey} = {}; |
| 346 |
$conf->{casAppMetaDataMacros}->{$confKey} = {}; |
| 347 |
$translatedOptions = $self->_getDefaultValues('casAppMetaDataNodes'); |
| 348 |
} |
| 349 |
|
| 350 |
if ( defined $push->{options} ) { |
| 351 |
|
| 352 |
foreach ( keys %{ $push->{options} } ) { |
| 353 |
my $optionName = $self->_translateOptionApiToConf( $_, 'casApp' ); |
| 354 |
my $optionValue = |
| 355 |
$self->_translateValueApiToConf( $optionName, |
| 356 |
$push->{options}->{$_} ); |
| 357 |
|
| 358 |
$translatedOptions->{$optionName} = $optionValue; |
| 359 |
} |
| 360 |
|
| 361 |
my $res = $self->_hasAllowedAttributes( $translatedOptions, |
| 362 |
'casAppMetaDataNode' ); |
| 363 |
return $res unless ( $res->{res} eq 'ok' ); |
| 364 |
|
| 365 |
$self->_merge_hash( $conf, 'casAppMetaDataOptions', $confKey, |
| 366 |
$translatedOptions ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( defined $push->{exportedVars} ) { |
| 370 |
if ( $self->_isSimpleKeyValueHash( $push->{exportedVars} ) ) { |
| 371 |
$self->_merge_hash( |
| 372 |
$conf, 'casAppMetaDataExportedVars', |
| 373 |
$confKey, $push->{exportedVars} |
| 374 |
); |
| 375 |
} |
| 376 |
else { |
| 377 |
return { |
| 378 |
res => 'ko', |
| 379 |
msg => |
| 380 |
"Invalid input: exportedVars is not a hash object with \"key\":\"value\" attributes" |
| 381 |
}; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if ( defined $push->{macros} ) { |
| 386 |
if ( $self->_isSimpleKeyValueHash( $push->{macros} ) ) { |
| 387 |
$self->_merge_hash( $conf, 'casAppMetaDataMacros', $confKey, |
| 388 |
$push->{macros} ); |
| 389 |
} |
| 390 |
else { |
| 391 |
return { |
| 392 |
res => 'ko', |
| 393 |
msg => |
| 394 |
"Invalid input: macros is not a hash object with \"key\":\"value\" attributes" |
| 395 |
}; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
# Test new configuration |
| 400 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( { |
| 401 |
refConf => $self->_confAcc->getConf( { noCache => 1 } ), |
| 402 |
newConf => $conf, |
| 403 |
req => {}, |
| 404 |
} |
| 405 |
); |
| 406 |
unless ( $parser->testNewConf( $self->p ) ) { |
| 407 |
return { |
| 408 |
res => 'ko', |
| 409 |
code => 400, |
| 410 |
msg => "Configuration error: " |
| 411 |
. join( ". ", map { $_->{message} } @{ $parser->errors } ), |
| 412 |
}; |
| 413 |
} |
| 414 |
|
| 415 |
# Save configuration |
| 416 |
$self->_saveApplyConf($conf); |
| 417 |
|
| 418 |
return { res => 'ok' }; |
| 419 |
} |
| 420 |
|
| 421 |
1; |