« blib/lib/Lemonldap/NG/Common/Conf/Backends/LDAP.pm
↑ Summary
blib/lib/Lemonldap/NG/Common/PSGI/Router.pm »
| 1 | package Lemonldap::NG::Common::Conf::RESTServer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use JSON 'from_json'; | ||
| 5 | use Mouse; | ||
| 6 | use Lemonldap::NG::Common::Conf::Constants; | ||
| 7 | use Lemonldap::NG::Common::Conf::ReConstants; | ||
| 8 | |||
| 9 | our $VERSION = '2.23.1'; | ||
| 10 | |||
| 11 | extends 'Lemonldap::NG::Common::Conf::AccessLib'; | ||
| 12 | |||
| 13 | ####################### | ||
| 14 | # I. PRIVATE METHODS # | ||
| 15 | ####################### | ||
| 16 | |||
| 17 | ## @method scalar getConfKey($req, $key) | ||
| 18 | # Return key value | ||
| 19 | # | ||
| 20 | # Return the value of $key key in current configuration. If cfgNum is set to | ||
| 21 | # `latest`, get before last configuration number. | ||
| 22 | # | ||
| 23 | # Errors: set an error in $req->error and return undef if: | ||
| 24 | # * query does not have a cfgNum parameter (set by Common/PSGI/Router.pm) | ||
| 25 | # * cfgNum is not a number | ||
| 26 | # | ||
| 27 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 28 | #@param $key Key name | ||
| 29 | #@return keyvalue (string, int or hashref) | ||
| 30 | sub getConfKey { | ||
| 31 | my ( $self, $req, $key, @args ) = @_; | ||
| 32 | $self->logger->debug("Search for $key in conf"); | ||
| 33 | |||
| 34 | # Verify that cfgNum has been asked | ||
| 35 | unless ( defined $req->params('cfgNum') ) { | ||
| 36 | $req->error("Missing configuration number"); | ||
| 37 | return undef; | ||
| 38 | } | ||
| 39 | $self->logger->debug( "Cfgnum set to " . $req->params('cfgNum') ); | ||
| 40 | |||
| 41 | # when 'latest' => replace by last cfgNum | ||
| 42 | if ( $req->params('cfgNum') eq 'latest' ) { | ||
| 43 | my $tmp = $self->confAcc->lastCfg; | ||
| 44 | $req->set_param( 'cfgNum', $tmp ); | ||
| 45 | unless ($tmp) { | ||
| 46 | $req->error($Lemonldap::NG::Common::Conf::msg) | ||
| 47 | if ($Lemonldap::NG::Common::Conf::msg); | ||
| 48 | return undef; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | elsif ( $req->params('cfgNum') !~ /^\d+$/ ) { | ||
| 52 | $req->error("cfgNum must be a number"); | ||
| 53 | return undef; | ||
| 54 | } | ||
| 55 | unless ( | ||
| 56 | defined $self->getConfByNum( scalar( $req->params('cfgNum') ), @args ) ) | ||
| 57 | { | ||
| 58 | $req->error( "Configuration " | ||
| 59 | . $req->params('cfgNum') | ||
| 60 | . " is not available (" | ||
| 61 | . $Lemonldap::NG::Common::Conf::msg | ||
| 62 | . ')' ); | ||
| 63 | return undef; | ||
| 64 | } | ||
| 65 | |||
| 66 | # TODO: insert default values | ||
| 67 | # Set an error if key is not defined | ||
| 68 | return $self->currentConf->{$key}; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub getConfByNum { | ||
| 72 | my ( $self, $cfgNum, @args ) = @_; | ||
| 73 | unless ( $self->currentConf | ||
| 74 | and %{ $self->currentConf } | ||
| 75 | and $cfgNum == $self->currentConf->{cfgNum} ) | ||
| 76 | { | ||
| 77 | my $tmp = $self->confAcc->getConf( | ||
| 78 | { cfgNum => $cfgNum, raw => 1, noCache => 1, @args } ); | ||
| 79 | return undef unless ( $tmp and ref($tmp) and %$tmp ); | ||
| 80 | $self->currentConf($tmp); | ||
| 81 | } | ||
| 82 | return $cfgNum; | ||
| 83 | } | ||
| 84 | |||
| 85 | ######################## | ||
| 86 | # II. Display methods # | ||
| 87 | ######################## | ||
| 88 | |||
| 89 | # Values are send depending of the /path/info/. For example, | ||
| 90 | # /confs/1/portal to get portal value. | ||
| 91 | |||
| 92 | # This section contains several methods: | ||
| 93 | # - complex nodes: | ||
| 94 | # * complexNodesRoot() call for root queries (no subkeys) to display the list | ||
| 95 | # * virtualHosts() | ||
| 96 | # * _samlMetaDataNodes() is called by saml(IDP|RP)MetaDataNode | ||
| 97 | # * _oidcMetaDataNodes() is called by oidc(OP|RP)MetaDataNodes | ||
| 98 | # - other special nodes: | ||
| 99 | # * authChoiceModules() | ||
| 100 | # * grantSessionRules() | ||
| 101 | # * openIdIDPList() (old OpenID) | ||
| 102 | # * applicationList() | ||
| 103 | # - root: | ||
| 104 | # root query (/confs/latest for example) is redirected to metadata() | ||
| 105 | # - other requests: | ||
| 106 | # they are managed by getKey() | ||
| 107 | # - newRSAKey() returns a new RSA key pair if /confs/newRSAKey is called in a | ||
| 108 | # POST request | ||
| 109 | # - prx() load a request and return the content (for SAML/OIDC metadata) | ||
| 110 | |||
| 111 | # 31 - Complex subnodes | ||
| 112 | # ---------------- | ||
| 113 | |||
| 114 | ## @method PSGI-JSON-response complexNodesRoot($req, $query, $tpl) | ||
| 115 | # Respond to root requests for virtual hosts and SAMLmetadata | ||
| 116 | # | ||
| 117 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 118 | #@param $query Configuration root key | ||
| 119 | #@param $tpl Javascript template to use (see JS/JSON generator script) | ||
| 120 | #@return PSGI JSON response | ||
| 121 | sub complexNodesRoot { | ||
| 122 | my ( $self, $req, $query, $tpl ) = @_; | ||
| 123 | $self->logger->debug("Query for $query template keys"); | ||
| 124 | |||
| 125 | my $tmp = $self->getConfKey( $req, $query ); | ||
| 126 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 127 | |||
| 128 | my @res; | ||
| 129 | if ( ref($tmp) ) { | ||
| 130 | foreach my $f ( sort keys %$tmp ) { | ||
| 131 | push @res, | ||
| 132 | { | ||
| 133 | id => "${tpl}s/$f", | ||
| 134 | title => $f, | ||
| 135 | type => $tpl, | ||
| 136 | template => $tpl | ||
| 137 | }; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | return $self->sendJSONresponse( $req, \@res ); | ||
| 141 | } | ||
| 142 | |||
| 143 | # 311 - Virtual hosts | ||
| 144 | # ------------- | ||
| 145 | |||
| 146 | ## @method PSGI-JSON-response virtualHosts($req, @path) | ||
| 147 | # Respond to virtualhosts sub requests | ||
| 148 | # | ||
| 149 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 150 | #@param @path words in path after `virtualhosts` | ||
| 151 | #@return PSGI JSON response | ||
| 152 | sub virtualHosts { | ||
| 153 | my ( $self, $req, @path ) = @_; | ||
| 154 | |||
| 155 | return $self->complexNodesRoot( $req, 'locationRules', 'virtualHost' ) | ||
| 156 | unless (@path); | ||
| 157 | |||
| 158 | my $vh = shift @path; | ||
| 159 | my $query; | ||
| 160 | unless ( $query = shift @path ) { | ||
| 161 | return $self->sendError( $req, | ||
| 162 | 'Bad request: virtualHost query must ask for a key', 400 ); | ||
| 163 | } | ||
| 164 | |||
| 165 | # Send setDefault for new vhosts | ||
| 166 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 167 | if ( $vh =~ /^new__/ ); | ||
| 168 | |||
| 169 | # Reject unknown vhosts | ||
| 170 | return $self->sendError( $req, "Unknown virtualhost ($vh)", 400 ) | ||
| 171 | unless ( defined $self->getConfKey( $req, 'locationRules' )->{$vh} ); | ||
| 172 | |||
| 173 | if ( $query =~ /^(?:(?:exportedHeader|locationRule)s|post)$/ ) { | ||
| 174 | my ( $id, $resp ) = ( 1, [] ); | ||
| 175 | my $vhk = eval { $self->getConfKey( $req, $query )->{$vh} } // {}; | ||
| 176 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 177 | $self->logger->debug("Query for $vh/$query keys"); | ||
| 178 | |||
| 179 | # Keys are ordered except 'default' which must be at the end | ||
| 180 | foreach my $r ( | ||
| 181 | sort { | ||
| 182 | $query eq 'locationRules' | ||
| 183 | ? ( | ||
| 184 | $a eq 'default' | ||
| 185 | ? 1 | ||
| 186 | : ( $b eq 'default' ? -1 : $a cmp $b ) | ||
| 187 | ) | ||
| 188 | : $a cmp $b | ||
| 189 | } keys %$vhk | ||
| 190 | ) | ||
| 191 | { | ||
| 192 | my $res = { | ||
| 193 | id => "virtualHosts/$vh/$query/" . $id++, | ||
| 194 | title => $r, | ||
| 195 | data => $vhk->{$r}, | ||
| 196 | type => 'keyText', | ||
| 197 | }; | ||
| 198 | |||
| 199 | # If rule contains a comment or an AuthLevel, split them | ||
| 200 | if ( $query eq 'locationRules' ) { | ||
| 201 | $res->{comment} = ''; | ||
| 202 | $res->{level} = ''; | ||
| 203 | $res->{level} = $1 if ( $r =~ s/\(\?#AuthnLevel=(-?\d+)\)// ); | ||
| 204 | if ( $r =~ s/\(\?#(.*?)\)// ) { | ||
| 205 | $res->{title} = $res->{comment} = $1; | ||
| 206 | } | ||
| 207 | $res->{re} = $r; | ||
| 208 | $res->{type} = 'rule'; | ||
| 209 | } | ||
| 210 | elsif ( $query eq 'post' ) { | ||
| 211 | $res->{data} = $vhk->{$r}; | ||
| 212 | $res->{type} = 'post'; | ||
| 213 | } | ||
| 214 | push @$resp, $res; | ||
| 215 | } | ||
| 216 | return $self->sendJSONresponse( $req, $resp ); | ||
| 217 | } | ||
| 218 | elsif ( $query =~ qr/^$virtualHostKeys$/o ) { | ||
| 219 | $self->logger->debug("Query for $vh/$query key"); | ||
| 220 | |||
| 221 | # TODO: verify how this is done actually | ||
| 222 | my $k1 = $self->getConfKey( $req, 'vhostOptions' ); | ||
| 223 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 224 | |||
| 225 | # Default values are set by JS | ||
| 226 | my $res = eval { $k1->{$vh}->{$query} } // undef; | ||
| 227 | return $self->sendJSONresponse( $req, { value => $res } ); | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | return $self->sendError( $req, "Unknown vhost subkey ($query)", 400 ); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # 312 - SAML | ||
| 235 | # ---- | ||
| 236 | |||
| 237 | ## @method PSGI-JSON-response _samlMetaDataNode($type, $req, @path) | ||
| 238 | # Respond to SAML metadata subnodes | ||
| 239 | # | ||
| 240 | #@param $type `SP` or `IDP` | ||
| 241 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 242 | #@param @path words in path after `saml{IDP|SP}MetaDataNode` | ||
| 243 | #@return PSGI JSON response | ||
| 244 | sub _samlMetaDataNodes { | ||
| 245 | my ( $self, $type, $req, @path ) = @_; | ||
| 246 | |||
| 247 | return $self->complexNodesRoot( $req, "saml${type}MetaDataXML", | ||
| 248 | "saml${type}MetaDataNode" ) | ||
| 249 | unless (@path); | ||
| 250 | my $partner = shift @path; | ||
| 251 | my $query = shift @path; | ||
| 252 | unless ($query) { | ||
| 253 | return $self->sendError( $req, | ||
| 254 | "Bad request: saml${type}MetaDataNode query must ask for a key", | ||
| 255 | 400 ); | ||
| 256 | } | ||
| 257 | |||
| 258 | # setDefault response for new partners | ||
| 259 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 260 | if ( $partner =~ /^new__/ ); | ||
| 261 | |||
| 262 | # Reject unknown partners | ||
| 263 | return $self->sendError( $req, "Unknown SAML partner ($partner)", 400 ) | ||
| 264 | unless ( | ||
| 265 | defined eval { | ||
| 266 | $self->getConfKey( $req, "saml${type}MetaDataXML" )->{$partner}; | ||
| 267 | } | ||
| 268 | ); | ||
| 269 | |||
| 270 | my ( $id, $resp ) = ( 1, [] ); | ||
| 271 | |||
| 272 | # Return all exported attributes if asked | ||
| 273 | if ( $query =~ /^saml${type}MetaDataExportedAttributes$/ ) { | ||
| 274 | my $pk = | ||
| 275 | eval { $self->getConfKey( $req, $query )->{$partner} } // {}; | ||
| 276 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 277 | foreach my $h ( sort keys %$pk ) { | ||
| 278 | push @$resp, | ||
| 279 | { | ||
| 280 | id => "saml${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 281 | title => $h, | ||
| 282 | data => [ split /;/, $pk->{$h} ], | ||
| 283 | type => 'samlAttribute', | ||
| 284 | }; | ||
| 285 | } | ||
| 286 | return $self->sendJSONresponse( $req, $resp ); | ||
| 287 | } | ||
| 288 | elsif ( $query eq "samlSPMetaDataMacros" ) { | ||
| 289 | my $pk = | ||
| 290 | eval { $self->getConfKey( $req, $query )->{$partner} } // {}; | ||
| 291 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 292 | foreach my $h ( sort keys %$pk ) { | ||
| 293 | push @$resp, | ||
| 294 | { | ||
| 295 | id => "saml${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 296 | title => $h, | ||
| 297 | data => $pk->{$h}, | ||
| 298 | type => 'keyText', | ||
| 299 | }; | ||
| 300 | } | ||
| 301 | return $self->sendJSONresponse( $req, $resp ); | ||
| 302 | } | ||
| 303 | |||
| 304 | # Simple root keys | ||
| 305 | elsif ( $query =~ /^saml${type}MetaDataXML$/ ) { | ||
| 306 | my $value = | ||
| 307 | eval { $self->getConfKey( $req, $query )->{$partner}->{$query}; } | ||
| 308 | // undef; | ||
| 309 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 310 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 311 | } | ||
| 312 | |||
| 313 | # Metadata URL | ||
| 314 | elsif ( $query =~ /^saml${type}MetaDataURL$/ ) { | ||
| 315 | my $value = | ||
| 316 | eval { $self->getConfKey( $req, $query )->{$partner}->{$query}; } | ||
| 317 | // undef; | ||
| 318 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 319 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 320 | } | ||
| 321 | |||
| 322 | # These regexps are generated by jsongenerator.pl and stored in | ||
| 323 | # Lemonldap::NG::Common::Conf::ReConstants | ||
| 324 | elsif ( | ||
| 325 | $query =~ { | ||
| 326 | IDP => qr/^$samlIDPMetaDataNodeKeys$/o, | ||
| 327 | SP => qr/^$samlSPMetaDataNodeKeys$/o | ||
| 328 | }->{$type} | ||
| 329 | ) | ||
| 330 | { | ||
| 331 | my $value = eval { | ||
| 332 | $self->getConfKey( $req, "saml${type}MetaDataOptions" )->{$partner} | ||
| 333 | ->{$query}; | ||
| 334 | } // undef; | ||
| 335 | |||
| 336 | # Note that types "samlService" and "samlAssertion" will be splitted by | ||
| 337 | # manager.js in an array | ||
| 338 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 339 | } | ||
| 340 | else { | ||
| 341 | return $self->sendError( $req, | ||
| 342 | "Bad key for saml${type}MetaDataNode ($query)", 400 ); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | ## @method PSGI-JSON-response samlIDPMetaDataNode($req, @path) | ||
| 347 | # Launch _samlMetaDataNode('IDP', @_) | ||
| 348 | # | ||
| 349 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 350 | #@param @path words in path after `samlIDPMetaDataNode` | ||
| 351 | #@return PSGI JSON response | ||
| 352 | sub samlIDPMetaDataNodes { | ||
| 353 | my ( $self, $req, @path ) = @_; | ||
| 354 | return $self->_samlMetaDataNodes( 'IDP', $req, @path ); | ||
| 355 | } | ||
| 356 | |||
| 357 | ## @method PSGI-JSON-response samlSPMetaDataNode($req, @path) | ||
| 358 | # Launch _samlMetaDataNode('SP', @_) | ||
| 359 | # | ||
| 360 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 361 | #@param @path words in path after `samlSPMetaDataNode` | ||
| 362 | #@return PSGI JSON response | ||
| 363 | sub samlSPMetaDataNodes { | ||
| 364 | my ( $self, $req, @path ) = @_; | ||
| 365 | return $self->_samlMetaDataNodes( 'SP', $req, @path ); | ||
| 366 | } | ||
| 367 | |||
| 368 | # 313 - OpenID-Connect | ||
| 369 | # -------------- | ||
| 370 | |||
| 371 | ## @method PSGI-JSON-response _oidcMetaDataNodes($type, $req, @path) | ||
| 372 | # Respond to OpenID-Connect metadata subnodes | ||
| 373 | # | ||
| 374 | #@param $type `OP` or `RP` | ||
| 375 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 376 | #@param @path words in path after `oidc{OP|RP}MetaDataNode` | ||
| 377 | #@return PSGI JSON response | ||
| 378 | sub _oidcMetaDataNodes { | ||
| 379 | my ( $self, $type, $req, @path ) = @_; | ||
| 380 | |||
| 381 | my $refKey = | ||
| 382 | ( $type eq 'RP' ? 'oidcRPMetaDataOptions' : 'oidcOPMetaDataJSON' ); | ||
| 383 | return $self->complexNodesRoot( $req, $refKey, "oidc${type}MetaDataNode" ) | ||
| 384 | unless (@path); | ||
| 385 | |||
| 386 | my $partner = shift @path; | ||
| 387 | my $query = shift @path; | ||
| 388 | unless ($query) { | ||
| 389 | return $self->sendError( $req, | ||
| 390 | "Bad request: oidc${type}MetaDataNode query must ask for a key", | ||
| 391 | 400 ); | ||
| 392 | } | ||
| 393 | |||
| 394 | # setDefault response for new partners | ||
| 395 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 396 | if ( $partner =~ /^new__/ ); | ||
| 397 | |||
| 398 | # Reject unknown partners | ||
| 399 | return $self->sendError( $req, | ||
| 400 | "Unknown OpenID-Connect partner ($partner)", 400 ) | ||
| 401 | unless ( | ||
| 402 | defined eval { $self->getConfKey( $req, $refKey )->{$partner}; } ); | ||
| 403 | |||
| 404 | my ( $id, $resp ) = ( 1, [] ); | ||
| 405 | |||
| 406 | # Handle RP Attributes | ||
| 407 | if ( $query eq "oidcRPMetaDataExportedVars" ) { | ||
| 408 | my $pk = eval { $self->getConfKey( $req, $query )->{$partner} } // {}; | ||
| 409 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 410 | foreach my $h ( sort keys %$pk ) { | ||
| 411 | |||
| 412 | # Set default values for type and array | ||
| 413 | my $data = [ split /;/, $pk->{$h} ]; | ||
| 414 | unless ( $data->[1] ) { | ||
| 415 | $data->[1] = "string"; | ||
| 416 | } | ||
| 417 | unless ( $data->[2] ) { | ||
| 418 | $data->[2] = "auto"; | ||
| 419 | } | ||
| 420 | push @$resp, | ||
| 421 | { | ||
| 422 | id => "oidc${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 423 | title => $h, | ||
| 424 | data => $data, | ||
| 425 | type => 'oidcAttribute', | ||
| 426 | }; | ||
| 427 | } | ||
| 428 | return $self->sendJSONresponse( $req, $resp ); | ||
| 429 | } | ||
| 430 | |||
| 431 | # Return all exported attributes if asked | ||
| 432 | elsif ( $query =~ | ||
| 433 | /^(?:oidc${type}MetaDataExportedVars|oidcRPMetaDataOptionsExtraClaims|oidcRPMetaDataMacros|oidcRPMetaDataScopeRules)$/ | ||
| 434 | ) | ||
| 435 | { | ||
| 436 | my $pk = eval { $self->getConfKey( $req, $query )->{$partner} } // {}; | ||
| 437 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 438 | foreach my $h ( sort keys %$pk ) { | ||
| 439 | push @$resp, | ||
| 440 | { | ||
| 441 | id => "oidc${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 442 | title => $h, | ||
| 443 | data => $pk->{$h}, | ||
| 444 | type => 'keyText', | ||
| 445 | }; | ||
| 446 | } | ||
| 447 | return $self->sendJSONresponse( $req, $resp ); | ||
| 448 | } | ||
| 449 | |||
| 450 | # Long text types (OP only) | ||
| 451 | elsif ( $query =~ /^oidcOPMetaData(?:JSON|JWKS)$/ ) { | ||
| 452 | my $value = | ||
| 453 | eval { $self->getConfKey( $req, $query )->{$partner}; } // undef; | ||
| 454 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 455 | |||
| 456 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 457 | } | ||
| 458 | |||
| 459 | # Options | ||
| 460 | elsif ( | ||
| 461 | $query =~ { | ||
| 462 | OP => qr/^$oidcOPMetaDataNodeKeys$/o, | ||
| 463 | RP => qr/^$oidcRPMetaDataNodeKeys$/o | ||
| 464 | }->{$type} | ||
| 465 | ) | ||
| 466 | { | ||
| 467 | my $value = eval { | ||
| 468 | $self->getConfKey( $req, "oidc${type}MetaDataOptions" )->{$partner} | ||
| 469 | ->{$query}; | ||
| 470 | } // undef; | ||
| 471 | |||
| 472 | if ( ref($value) eq "HASH" ) { | ||
| 473 | foreach my $h ( sort keys %$value ) { | ||
| 474 | push @$resp, | ||
| 475 | { | ||
| 476 | id => "oidc${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 477 | title => $h, | ||
| 478 | data => $value->{$h}, | ||
| 479 | type => 'keyText', | ||
| 480 | }; | ||
| 481 | } | ||
| 482 | return $self->sendJSONresponse( $req, $resp ); | ||
| 483 | } | ||
| 484 | else { | ||
| 485 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | else { | ||
| 489 | return $self->sendError( $req, | ||
| 490 | "Bad key for oidc${type}MetaDataNode ($query)", 400 ); | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | ## @method PSGI-JSON-response oidcOPMetaDataNodes($req, @path) | ||
| 495 | # Launch _oidcMetaDataNodes('SP', @_) | ||
| 496 | # | ||
| 497 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 498 | #@param @path words in path after `oidcOPMetaDataNode` | ||
| 499 | #@return PSGI JSON response | ||
| 500 | sub oidcOPMetaDataNodes { | ||
| 501 | my ( $self, $req, @path ) = @_; | ||
| 502 | return $self->_oidcMetaDataNodes( 'OP', $req, @path ); | ||
| 503 | } | ||
| 504 | |||
| 505 | ## @method PSGI-JSON-response oidcRPMetaDataNodes($req, @path) | ||
| 506 | # Launch _oidcMetaDataNodes('SP', @_) | ||
| 507 | # | ||
| 508 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 509 | #@param @path words in path after `oidcRPMetaDataNode` | ||
| 510 | #@return PSGI JSON response | ||
| 511 | sub oidcRPMetaDataNodes { | ||
| 512 | my ( $self, $req, @path ) = @_; | ||
| 513 | return $self->_oidcMetaDataNodes( 'RP', $req, @path ); | ||
| 514 | } | ||
| 515 | |||
| 516 | # 314 - CAS | ||
| 517 | # --- | ||
| 518 | |||
| 519 | sub _casMetaDataNodes { | ||
| 520 | my ( $self, $type, $req, @path ) = @_; | ||
| 521 | my $refKey = | ||
| 522 | ( $type eq 'App' ? 'casAppMetaDataOptions' : 'casSrvMetaDataOptions' ); | ||
| 523 | return $self->complexNodesRoot( $req, $refKey, "cas${type}MetaDataNode" ) | ||
| 524 | unless (@path); | ||
| 525 | |||
| 526 | my $partner = shift @path; | ||
| 527 | my $query = shift @path; | ||
| 528 | unless ($query) { | ||
| 529 | return $self->sendError( $req, | ||
| 530 | "Bad request: cas${type}MetaDataNode query must ask for a key", | ||
| 531 | 400 ); | ||
| 532 | } | ||
| 533 | |||
| 534 | # setDefault response for new partners | ||
| 535 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 536 | if ( $partner =~ /^new__/ ); | ||
| 537 | |||
| 538 | # Reject unknown partners | ||
| 539 | return $self->sendError( $req, "Unknown CAS partner ($partner)", 400 ) | ||
| 540 | unless ( | ||
| 541 | defined eval { $self->getConfKey( $req, $refKey )->{$partner}; } ); | ||
| 542 | |||
| 543 | my ( $id, $resp ) = ( 1, [] ); | ||
| 544 | |||
| 545 | # Return all exported attributes if asked | ||
| 546 | if ( $query =~ | ||
| 547 | /^(?:cas${type}MetaDataExportedVars|casSrvMetaDataOptionsProxiedServices|casAppMetaDataMacros)$/ | ||
| 548 | ) | ||
| 549 | { | ||
| 550 | my $pk = eval { $self->getConfKey( $req, $query )->{$partner} } // {}; | ||
| 551 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 552 | foreach my $h ( sort keys %$pk ) { | ||
| 553 | push @$resp, | ||
| 554 | { | ||
| 555 | id => "cas${type}MetaDataNodes/$partner/$query/" . $id++, | ||
| 556 | title => $h, | ||
| 557 | data => $pk->{$h}, | ||
| 558 | type => 'keyText', | ||
| 559 | }; | ||
| 560 | } | ||
| 561 | return $self->sendJSONresponse( $req, $resp ); | ||
| 562 | } | ||
| 563 | |||
| 564 | # Options | ||
| 565 | if ( | ||
| 566 | $query =~ { | ||
| 567 | App => qr/^$casAppMetaDataNodeKeys$/o, | ||
| 568 | Srv => qr/^$casSrvMetaDataNodeKeys$/o | ||
| 569 | }->{$type} | ||
| 570 | ) | ||
| 571 | { | ||
| 572 | my $value = eval { | ||
| 573 | $self->getConfKey( $req, "cas${type}MetaDataOptions" )->{$partner} | ||
| 574 | ->{$query}; | ||
| 575 | } // undef; | ||
| 576 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 577 | } | ||
| 578 | else { | ||
| 579 | return $self->sendError( $req, | ||
| 580 | "Bad key for cas${type}MetaDataNode ($query)", 400 ); | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | sub casSrvMetaDataNodes { | ||
| 585 | my ( $self, $req, @path ) = @_; | ||
| 586 | return $self->_casMetaDataNodes( 'Srv', $req, @path ); | ||
| 587 | } | ||
| 588 | |||
| 589 | sub casAppMetaDataNodes { | ||
| 590 | my ( $self, $req, @path ) = @_; | ||
| 591 | return $self->_casMetaDataNodes( 'App', $req, @path ); | ||
| 592 | } | ||
| 593 | |||
| 594 | # 32 - Other special nodes | ||
| 595 | # ------------------- | ||
| 596 | |||
| 597 | # 321 - Choice authentication | ||
| 598 | |||
| 599 | ## @method PSGI-JSON-response authChoiceModules($req,$key) | ||
| 600 | # Returns authChoiceModules keys splitted in arrays | ||
| 601 | # | ||
| 602 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 603 | #@param key optional subkey | ||
| 604 | #@return PSGI JSON response | ||
| 605 | sub authChoiceModules { | ||
| 606 | my ( $self, $req, $key ) = @_; | ||
| 607 | my $value = $self->getConfKey( $req, 'authChoiceModules' ); | ||
| 608 | unless ($key) { | ||
| 609 | my @res; | ||
| 610 | foreach my $k ( sort keys %$value ) { | ||
| 611 | my $data = [ split /;\s*/, $value->{$k} ]; | ||
| 612 | if ( $data->[5] ) { | ||
| 613 | my $over; | ||
| 614 | eval { $over = from_json( $data->[5] ) }; | ||
| 615 | if ($@) { | ||
| 616 | $self->logger->error( | ||
| 617 | "Bad value in choice over parameters, deleted ($@)"); | ||
| 618 | } | ||
| 619 | else { | ||
| 620 | $data->[5] = [ map { [ $_, $over->{$_} ] } keys %{$over} ]; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | push @res, | ||
| 624 | { | ||
| 625 | id => "authChoiceModules/$k", | ||
| 626 | title => "$k", | ||
| 627 | data => $data, | ||
| 628 | type => 'authChoice' | ||
| 629 | }; | ||
| 630 | } | ||
| 631 | return $self->sendJSONresponse( $req, \@res ); | ||
| 632 | } | ||
| 633 | else { | ||
| 634 | my $r = $value->{$key} ? [ split( /;\s*/, $value->{$key} ) ] : []; | ||
| 635 | return $self->sendJSONresponse( $req, { value => $r } ); | ||
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | # 322 - Rules to grant sessions | ||
| 640 | |||
| 641 | ## @method PSGI-JSON-response grantSessionRules($req) | ||
| 642 | # Split grantSessionRules key=>value into 3 elements | ||
| 643 | # | ||
| 644 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 645 | #@return PSGI JSON response | ||
| 646 | sub grantSessionRules { | ||
| 647 | my ( $self, $req, $key ) = @_; | ||
| 648 | return $self->sendError( $req, 'Subkeys forbidden for grantSessionRules', | ||
| 649 | 400 ) | ||
| 650 | if ($key); | ||
| 651 | my $value = $self->getConfKey( $req, 'grantSessionRules' ); | ||
| 652 | my @res; | ||
| 653 | |||
| 654 | sub _sort { | ||
| 655 | my $A = ( $a =~ /^.*?##(.*)$/ )[0]; | ||
| 656 | my $B = ( $b =~ /^.*?##(.*)$/ )[0]; | ||
| 657 | return !$A ? 1 : !$B ? -1 : $A cmp $B; | ||
| 658 | } | ||
| 659 | my $id = 0; | ||
| 660 | foreach my $k ( sort _sort keys %$value ) { | ||
| 661 | my $r = $k; | ||
| 662 | my $c = ( $r =~ s/^(.*)?##(.*)$/$1/ ? $2 : '' ); | ||
| 663 | $id++; | ||
| 664 | push @res, | ||
| 665 | { | ||
| 666 | id => "grantSessionRules/$id", | ||
| 667 | title => $c || $r, | ||
| 668 | re => $r, | ||
| 669 | comment => $c, | ||
| 670 | data => $value->{$k}, | ||
| 671 | type => 'grant' | ||
| 672 | }; | ||
| 673 | } | ||
| 674 | return $self->sendJSONresponse( $req, \@res ); | ||
| 675 | } | ||
| 676 | |||
| 677 | # 323 - (old)OpenID IDP black/white list | ||
| 678 | |||
| 679 | ##method PSGI-JSON-response openIdIDPList($req) | ||
| 680 | # Split openIdIDPList parameter into 2 elements | ||
| 681 | sub openIdIDPList { | ||
| 682 | my ( $self, $req, $key ) = @_; | ||
| 683 | return $self->sendError( $req, 'Subkeys forbidden for openIdIDPList', 400 ) | ||
| 684 | if ($key); | ||
| 685 | my $value = $self->getConfKey( $req, 'openIdIDPList' ); | ||
| 686 | $value //= '0;'; | ||
| 687 | my ( $type, $v ) = split /;/, $value; | ||
| 688 | $v //= ''; | ||
| 689 | return $self->sendJSONresponse( $req, { value => [ $type, $v ] } ); | ||
| 690 | } | ||
| 691 | |||
| 692 | # 324 - Application for menu | ||
| 693 | # -------------------- | ||
| 694 | |||
| 695 | ## @method PSGI-JSON-response applicationList($req, @other) | ||
| 696 | # Return the full menu tree | ||
| 697 | # | ||
| 698 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 699 | #@param @other words in path after `applicationList` | ||
| 700 | #@return PSGI JSON response | ||
| 701 | sub applicationList { | ||
| 702 | my ( $self, $req, @other ) = @_; | ||
| 703 | return $self->sendError( $req, | ||
| 704 | 'There is no subkey for applicationList', 400 ) | ||
| 705 | if (@other); | ||
| 706 | my $apps = $self->getConfKey( $req, 'applicationList' ); | ||
| 707 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 708 | $apps = {} unless ( ref($apps) eq 'HASH' ); | ||
| 709 | my $json = $self->_scanCatsAndApps( $apps, 'applicationList' ); | ||
| 710 | return $self->sendJSONresponse( $req, $json ); | ||
| 711 | } | ||
| 712 | |||
| 713 | ## @method arrayRef _scanCatsAndApps($apps) | ||
| 714 | # Recursive method used to build categories & applications menu | ||
| 715 | # | ||
| 716 | #@param $apps HashRef pointing to a subnode of catAndApps conf tree | ||
| 717 | #@return arrayRef | ||
| 718 | sub _scanCatsAndApps { | ||
| 719 | my ( $self, $apps, $baseId ) = @_; | ||
| 720 | my @res; | ||
| 721 | |||
| 722 | foreach my $cat ( | ||
| 723 | sort { | ||
| 724 | ( $apps->{$a}->{order} || 0 ) <=> ( $apps->{$b}->{order} || 0 ) | ||
| 725 | or $a cmp $b | ||
| 726 | } | ||
| 727 | grep { not /^(?:catname|type|order)$/ } keys %$apps | ||
| 728 | ) | ||
| 729 | { | ||
| 730 | my $item = { id => "$baseId/$cat" }; | ||
| 731 | if ( $apps->{$cat}->{type} eq 'category' ) { | ||
| 732 | $item->{title} = $apps->{$cat}->{catname}; | ||
| 733 | $item->{type} = 'menuCat'; | ||
| 734 | $item->{nodes} = | ||
| 735 | $self->_scanCatsAndApps( $apps->{$cat}, "$baseId/$cat" ); | ||
| 736 | } | ||
| 737 | else { | ||
| 738 | $item->{title} = $apps->{$cat}->{options}->{name}; | ||
| 739 | $item->{type} = $apps->{$cat}->{type} = 'menuApp'; | ||
| 740 | foreach my $o ( | ||
| 741 | grep { not /^name$/ } | ||
| 742 | keys %{ $apps->{$cat}->{options} } | ||
| 743 | ) | ||
| 744 | { | ||
| 745 | $item->{data}->{$o} = $apps->{$cat}->{options}->{$o}; | ||
| 746 | } | ||
| 747 | } | ||
| 748 | push @res, $item; | ||
| 749 | } | ||
| 750 | return \@res; | ||
| 751 | } | ||
| 752 | |||
| 753 | # 325 - Combination modules | ||
| 754 | |||
| 755 | # Returns raw value, just transform "over" key | ||
| 756 | sub combModules { | ||
| 757 | my ( $self, $req, $key ) = @_; | ||
| 758 | return $self->sendError( $req, 'Subkeys forbidden for combModules', 400 ) | ||
| 759 | if ($key); | ||
| 760 | my $val = $self->getConfKey( $req, 'combModules' ) // {}; | ||
| 761 | my $res = []; | ||
| 762 | foreach my $mod ( keys %$val ) { | ||
| 763 | my $tmp; | ||
| 764 | $tmp->{title} = $mod; | ||
| 765 | $tmp->{id} = "combModules/$mod"; | ||
| 766 | $tmp->{type} = 'cmbModule'; | ||
| 767 | $tmp->{data}->{$_} = $val->{$mod}->{$_} foreach (qw(type for)); | ||
| 768 | my $over = $val->{$mod}->{over} // {}; | ||
| 769 | $tmp->{data}->{over} = [ map { [ $_, $over->{$_} ] } keys %$over ]; | ||
| 770 | push @$res, $tmp; | ||
| 771 | } | ||
| 772 | return $self->sendJSONresponse( $req, $res ); | ||
| 773 | } | ||
| 774 | |||
| 775 | sub sfExtra { | ||
| 776 | my ( $self, $req, $key ) = @_; | ||
| 777 | return $self->sendError( $req, 'Subkeys forbidden for sfExtra', 400 ) | ||
| 778 | if ($key); | ||
| 779 | my $val = $self->getConfKey( $req, 'sfExtra' ) // {}; | ||
| 780 | my $res = []; | ||
| 781 | foreach my $mod ( keys %$val ) { | ||
| 782 | my $tmp; | ||
| 783 | $tmp->{title} = $mod; | ||
| 784 | $tmp->{id} = "sfExtra/$mod"; | ||
| 785 | $tmp->{type} = 'sfExtra'; | ||
| 786 | $tmp->{data}->{$_} = $val->{$mod}->{$_} | ||
| 787 | foreach (qw(type rule regrule logo level label)); | ||
| 788 | $tmp->{data}->{register} = $val->{$mod}->{register} ? \1 : \0; | ||
| 789 | my $over = $val->{$mod}->{over} // {}; | ||
| 790 | $tmp->{data}->{over} = [ map { [ $_, $over->{$_} ] } keys %$over ]; | ||
| 791 | push @$res, $tmp; | ||
| 792 | } | ||
| 793 | return $self->sendJSONresponse( $req, $res ); | ||
| 794 | } | ||
| 795 | |||
| 796 | ## Keys | ||
| 797 | sub keyNodes { | ||
| 798 | my ( $self, $req, @path ) = @_; | ||
| 799 | { | ||
| 800 | local $" = ","; | ||
| 801 | $self->logger->debug("Keynode @path"); | ||
| 802 | } | ||
| 803 | |||
| 804 | return $self->complexNodesRoot( $req, "keys", "keyNode" ) unless (@path); | ||
| 805 | |||
| 806 | my $keyId = shift @path; | ||
| 807 | my $query = shift @path; | ||
| 808 | |||
| 809 | unless ($query) { | ||
| 810 | return $self->sendError( $req, | ||
| 811 | "Bad request: keyNodes query must ask for a key", 400 ); | ||
| 812 | } | ||
| 813 | |||
| 814 | # setDefault response for new keyIds | ||
| 815 | return $self->sendError( $req, 'setDefault', 200 ) | ||
| 816 | if ( $keyId =~ /^new__/ ); | ||
| 817 | |||
| 818 | # Reject unknown keyIds | ||
| 819 | return $self->sendError( $req, "Unknown key ID ($keyId)", 400 ) | ||
| 820 | unless ( defined eval { $self->getConfKey( $req, 'keys' )->{$keyId}; } ); | ||
| 821 | |||
| 822 | my ( $id, $resp ) = ( 1, [] ); | ||
| 823 | |||
| 824 | my $value = | ||
| 825 | eval { $self->getConfKey( $req, "keys" )->{$keyId}->{$query}; } // undef; | ||
| 826 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 827 | } | ||
| 828 | |||
| 829 | # 33 - Root queries | ||
| 830 | # ----------- | ||
| 831 | |||
| 832 | ## @method PSGI-JSON-response metadata($req) | ||
| 833 | # Respond to `/conf/:cfgNum` requests by sending configuration metadata | ||
| 834 | # | ||
| 835 | # NB: if `full=1` is set in the query, configuration is returned directly in | ||
| 836 | # JSON | ||
| 837 | # | ||
| 838 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 839 | #@return PSGI JSON response | ||
| 840 | sub metadata { | ||
| 841 | my ( $self, $req ) = @_; | ||
| 842 | if ( $req->params('full') and $req->params('full') !~ NO ) { | ||
| 843 | my $c = $self->getConfKey( $req, 'cfgNum' ); | ||
| 844 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 845 | if ( my $userid = $self->_userId($req) ) { | ||
| 846 | $self->auditLog( | ||
| 847 | $req, | ||
| 848 | message => ( | ||
| 849 | 'User ' . $userid . ' ask for full configuration ' . $c | ||
| 850 | ), | ||
| 851 | code => "CONFIG_DOWNLOADED", | ||
| 852 | user => $userid, | ||
| 853 | cfgNum => $c | ||
| 854 | ); | ||
| 855 | } | ||
| 856 | else { | ||
| 857 | $self->auditLog( | ||
| 858 | $req, | ||
| 859 | message => "REST request to get full configuration $c", | ||
| 860 | code => "CONFIG_DOWNLOADED", | ||
| 861 | cfgNum => $c | ||
| 862 | ); | ||
| 863 | } | ||
| 864 | return $self->sendJSONresponse( | ||
| 865 | $req, | ||
| 866 | $self->currentConf, | ||
| 867 | pretty => 1, | ||
| 868 | headers => [ | ||
| 869 | 'Content-Disposition' => "Attachment; filename=lmConf-$c.json" | ||
| 870 | ], | ||
| 871 | ); | ||
| 872 | } | ||
| 873 | elsif ( my $oidc = $req->params('oidcMetadata') | ||
| 874 | or $req->params('samlMetadata') ) | ||
| 875 | { | ||
| 876 | my $c = $self->getConfKey( $req, 'cfgNum' ); | ||
| 877 | my $type = $oidc ? 'OIDC' : 'SAML'; | ||
| 878 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 879 | if ( $self->can('userId') ) { | ||
| 880 | $self->userLogger->notice( 'User ' | ||
| 881 | . $self->userId($req) | ||
| 882 | . " ask for $type metadata " | ||
| 883 | . $c ); | ||
| 884 | } | ||
| 885 | else { | ||
| 886 | $self->logger->info("REST request to get $type metadata $c"); | ||
| 887 | } | ||
| 888 | if ($oidc) { | ||
| 889 | require Lemonldap::NG::Common::OpenIDConnect::Metadata; | ||
| 890 | my $path = $self->currentConf->{issuerDBOpenIDConnectPath}; | ||
| 891 | $path =~ s#^.*?(\w+).*?$#$1/#; | ||
| 892 | return $self->sendJSONresponse( | ||
| 893 | $req, | ||
| 894 | Lemonldap::NG::Common::OpenIDConnect::Metadata->metadataDoc( | ||
| 895 | $self->currentConf->{oidcServiceMetaDataIssuer} | ||
| 896 | || $self->p->portal, | ||
| 897 | $self->currentConf, | ||
| 898 | $path, | ||
| 899 | ), | ||
| 900 | pretty => 1, | ||
| 901 | headers => [ | ||
| 902 | 'content-Type' => 'application/json', | ||
| 903 | 'Content-Disposition' => | ||
| 904 | "Attachment; filename=openid-configuration.json" | ||
| 905 | ], | ||
| 906 | ); | ||
| 907 | } | ||
| 908 | else { | ||
| 909 | require Lemonldap::NG::Common::Conf::SAML::Metadata; | ||
| 910 | if ( my $metadata = | ||
| 911 | Lemonldap::NG::Common::Conf::SAML::Metadata->new() ) | ||
| 912 | { | ||
| 913 | my $s = | ||
| 914 | $metadata->serviceToXML( | ||
| 915 | { %{ $self->currentConf }, portal => $self->p->portal }, | ||
| 916 | 'all' ); | ||
| 917 | return $self->p->sendTextResponse( $req, $s, | ||
| 918 | type => "application/xml; charset=utf-8" ); | ||
| 919 | } | ||
| 920 | return $self->p->sendError( $req, 'Unable to build Metadata', 500 ); | ||
| 921 | } | ||
| 922 | } | ||
| 923 | else { | ||
| 924 | my $res = {}; | ||
| 925 | $res->{cfgNum} = $self->getConfKey( $req, 'cfgNum' ); | ||
| 926 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 927 | return $self->sendError( $req, "Configuration without cfgNum", 500 ) | ||
| 928 | unless ( defined $res->{cfgNum} ); | ||
| 929 | foreach my $key (qw(cfgAuthor cfgDate cfgAuthorIP cfgLog cfgVersion)) { | ||
| 930 | $res->{$key} = $self->getConfKey( $req, $key ); | ||
| 931 | } | ||
| 932 | |||
| 933 | # Find next and previous conf | ||
| 934 | my @a = $self->confAcc->available; | ||
| 935 | my $id = -1; | ||
| 936 | my ($ind) = map { $id++; $_ == $res->{cfgNum} ? ($id) : () } @a; | ||
| 937 | if ($ind) { | ||
| 938 | my $prevNum = $a[ $ind - 1 ]; | ||
| 939 | my $prevConf = | ||
| 940 | $self->confAcc->getConf( | ||
| 941 | { cfgNum => $prevNum, raw => 1, noCache => 1 } ); | ||
| 942 | $res->{prev} = { | ||
| 943 | cfgNum => $prevNum, | ||
| 944 | cfgLog => $prevConf->{cfgLog} | ||
| 945 | }; | ||
| 946 | } | ||
| 947 | if ( defined $ind and $ind < $#a ) { | ||
| 948 | my $nextNum = $a[ $ind + 1 ]; | ||
| 949 | my $nextConf = | ||
| 950 | $self->confAcc->getConf( | ||
| 951 | { cfgNum => $nextNum, raw => 1, noCache => 1 } ); | ||
| 952 | $res->{next} = { | ||
| 953 | cfgNum => $nextNum, | ||
| 954 | cfgLog => $nextConf->{cfgLog} | ||
| 955 | }; | ||
| 956 | } | ||
| 957 | if ( my $userid = $self->_userId($req) ) { | ||
| 958 | $self->auditLog( | ||
| 959 | $req, | ||
| 960 | code => "CONFIG_GET_METADATA", | ||
| 961 | user => $userid, | ||
| 962 | cfgNum => $res->{cfgNum}, | ||
| 963 | message => ( | ||
| 964 | 'User ' | ||
| 965 | . $userid | ||
| 966 | . ' ask for configuration metadata (' | ||
| 967 | . $res->{cfgNum} . ')' | ||
| 968 | ) | ||
| 969 | ); | ||
| 970 | } | ||
| 971 | else { | ||
| 972 | $self->auditLog( | ||
| 973 | $req, | ||
| 974 | code => "CONFIG_GET_METADATA", | ||
| 975 | cfgNum => $res->{cfgNum}, | ||
| 976 | message => | ||
| 977 | "REST request to get configuration metadata ($res->{cfgNum})", | ||
| 978 | ); | ||
| 979 | } | ||
| 980 | return $self->sendJSONresponse( $req, $res ); | ||
| 981 | } | ||
| 982 | } | ||
| 983 | |||
| 984 | # 34 - Other values | ||
| 985 | # ------------ | ||
| 986 | |||
| 987 | ## @method PSGI-JSON-response getKey($req, $key, $subkey) | ||
| 988 | # Return the value of a root key of current configuration | ||
| 989 | # | ||
| 990 | #@param $req Lemonldap::NG::Common::PSGI::Request | ||
| 991 | #@param $key Name of key requested | ||
| 992 | #@param $subkey Subkey for hash values | ||
| 993 | #@return PSGI JSON response | ||
| 994 | sub getKey { | ||
| 995 | my ( $self, $req, $key, $subkey ) = @_; | ||
| 996 | unless ($key) { | ||
| 997 | return $self->metadata($req); | ||
| 998 | } | ||
| 999 | if ( my $userid = $self->_userId($req) ) { | ||
| 1000 | $self->auditLog( | ||
| 1001 | $req, | ||
| 1002 | code => "CONFIG_GET_KEY", | ||
| 1003 | message => ( 'User ' . $userid . " asks for key $key" ), | ||
| 1004 | user => $userid, | ||
| 1005 | key => $key, | ||
| 1006 | ); | ||
| 1007 | } | ||
| 1008 | else { | ||
| 1009 | $self->auditLog( | ||
| 1010 | $req, | ||
| 1011 | code => "CONFIG_GET_KEY", | ||
| 1012 | message => ("REST request to get configuration key $key"), | ||
| 1013 | key => $key, | ||
| 1014 | ); | ||
| 1015 | } | ||
| 1016 | my $value = $self->getConfKey( $req, $key ); | ||
| 1017 | return $self->sendError( $req, undef, 400 ) if ( $req->error ); | ||
| 1018 | |||
| 1019 | # When "hash" | ||
| 1020 | if ( $key =~ qr/^$simpleHashKeys$/o ) { | ||
| 1021 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 1022 | unless defined($value); | ||
| 1023 | |||
| 1024 | # If a hash key is asked return its value | ||
| 1025 | if ($subkey) { | ||
| 1026 | return $self->sendJSONresponse( $req, | ||
| 1027 | { value => $value->{$subkey} // undef, } ); | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | # else return the list of keys | ||
| 1031 | my @res; | ||
| 1032 | foreach my $k ( sort keys %$value ) { | ||
| 1033 | push @res, | ||
| 1034 | { | ||
| 1035 | id => "$key/$k", | ||
| 1036 | title => "$k", | ||
| 1037 | data => $value->{$k}, | ||
| 1038 | type => 'keyText' | ||
| 1039 | }; | ||
| 1040 | } | ||
| 1041 | return $self->sendJSONresponse( $req, \@res ); | ||
| 1042 | } | ||
| 1043 | elsif ( $key =~ qr/^$doubleHashKeys$/o ) { | ||
| 1044 | my @res; | ||
| 1045 | $value ||= {}; | ||
| 1046 | foreach my $host ( sort keys %$value ) { | ||
| 1047 | my @tmp; | ||
| 1048 | foreach my $k ( sort keys %{ $value->{$host} } ) { | ||
| 1049 | push @tmp, { k => $k, v => $value->{$host}->{$k} }; | ||
| 1050 | } | ||
| 1051 | push @res, { k => $host, h => \@tmp }; | ||
| 1052 | } | ||
| 1053 | return $self->sendJSONresponse( $req, { value => \@res } ); | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | # When scalar | ||
| 1057 | return $self->sendError( $req, "Key $key is not a hash", 400 ) | ||
| 1058 | if ($subkey); | ||
| 1059 | return $self->sendJSONresponse( $req, { error => 'setDefault' } ) | ||
| 1060 | unless defined($value); | ||
| 1061 | return $self->sendJSONresponse( $req, { value => $value } ); | ||
| 1062 | |||
| 1063 | # TODO authParam key | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | sub _userId { | ||
| 1067 | my ( $self, $req ) = @_; | ||
| 1068 | if ( $self->can('p') and $self->p->can('userId') ) { | ||
| 1069 | return $self->p->userId($req); | ||
| 1070 | } | ||
| 1071 | return; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | 1; |