| 1 |
package Lemonldap::NG::Manager::Api::Providers::OidcRp; |
| 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 getOidcRpByConfKey { |
| 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] OIDC RP $confKey configuration requested"); |
| 21 |
|
| 22 |
# Get latest configuration |
| 23 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 24 |
|
| 25 |
my $oidcRp = $self->_getOidcRpByConfKey( $conf, $confKey ); |
| 26 |
|
| 27 |
# Return 404 if not found |
| 28 |
return $self->sendError( $req, |
| 29 |
"OIDC relying party '$confKey' not found", 404 ) |
| 30 |
unless ( defined $oidcRp ); |
| 31 |
|
| 32 |
return $self->sendJSONresponse( $req, $oidcRp ); |
| 33 |
} |
| 34 |
|
| 35 |
sub findOidcRpByConfKey { |
| 36 |
my ( $self, $req ) = @_; |
| 37 |
|
| 38 |
my $pattern = ( |
| 39 |
defined $req->params('uPattern') |
| 40 |
? $req->params('uPattern') |
| 41 |
: ( defined $req->params('pattern') ? $req->params('pattern') : undef ) |
| 42 |
); |
| 43 |
|
| 44 |
return $self->sendError( $req, 'Invalid input: pattern is missing', 400 ) |
| 45 |
unless ( defined $pattern ); |
| 46 |
|
| 47 |
unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) { |
| 48 |
return $self->sendError( $req, 'Invalid input: pattern is invalid', |
| 49 |
400 ); |
| 50 |
} |
| 51 |
|
| 52 |
$self->logger->debug( |
| 53 |
"[API] Find OIDC RPs by confKey regexp $pattern requested"); |
| 54 |
|
| 55 |
# Get latest configuration |
| 56 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 57 |
|
| 58 |
my @oidcRps = |
| 59 |
map { $_ =~ $pattern ? $self->_getOidcRpByConfKey( $conf, $_ ) : () } |
| 60 |
keys %{ $conf->{oidcRPMetaDataOptions} }; |
| 61 |
|
| 62 |
return $self->sendJSONresponse( $req, [@oidcRps] ); |
| 63 |
} |
| 64 |
|
| 65 |
sub findOidcRpByClientId { |
| 66 |
my ( $self, $req ) = @_; |
| 67 |
|
| 68 |
my $clientId = ( |
| 69 |
defined $req->params('uClientId') ? $req->params('uClientId') |
| 70 |
: ( |
| 71 |
defined $req->params('clientId') ? $req->params('clientId') |
| 72 |
: undef |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
return $self->sendError( $req, 'Invalid input: clientId is missing', 400 ) |
| 77 |
unless ( defined $clientId ); |
| 78 |
|
| 79 |
$self->logger->debug("[API] Find OIDC RPs by clientId $clientId requested"); |
| 80 |
|
| 81 |
# Get latest configuration |
| 82 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 83 |
|
| 84 |
my $oidcRp = $self->_getOidcRpByClientId( $conf, $clientId ); |
| 85 |
return $self->sendError( $req, |
| 86 |
"OIDC relying party with clientId '$clientId' not found", 404 ) |
| 87 |
unless ( defined $oidcRp ); |
| 88 |
|
| 89 |
return $self->sendJSONresponse( $req, $oidcRp ); |
| 90 |
} |
| 91 |
|
| 92 |
sub addOidcRp { |
| 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: clientId is missing', 400 ) |
| 110 |
unless ( defined $add->{clientId} ); |
| 111 |
|
| 112 |
return $self->sendError( $req, 'Invalid input: clientId is not a string', |
| 113 |
400 ) |
| 114 |
if ( ref $add->{clientId} ); |
| 115 |
|
| 116 |
return $self->sendError( $req, 'Invalid input: redirectUris is missing', |
| 117 |
400 ) |
| 118 |
unless ( defined $add->{redirectUris} ); |
| 119 |
|
| 120 |
return $self->sendError( $req, |
| 121 |
'Invalid input: redirectUris must be an array', 400 ) |
| 122 |
unless ( ref( $add->{redirectUris} ) eq "ARRAY" ); |
| 123 |
|
| 124 |
$self->logger->debug( |
| 125 |
"[API] Add OIDC RP with confKey $add->{confKey} and clientId $add->{clientId} requested" |
| 126 |
); |
| 127 |
|
| 128 |
# Get latest configuration |
| 129 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 130 |
|
| 131 |
return $self->sendError( |
| 132 |
$req, |
| 133 |
"Invalid input: An OIDC RP with confKey $add->{confKey} already exists", |
| 134 |
409 |
| 135 |
) if ( defined $self->_getOidcRpByConfKey( $conf, $add->{confKey} ) ); |
| 136 |
|
| 137 |
return $self->sendError( |
| 138 |
$req, |
| 139 |
"Invalid input: An OIDC RP with clientId $add->{clientId} already exists", |
| 140 |
409 |
| 141 |
) if ( defined $self->_getOidcRpByClientId( $conf, $add->{clientId} ) ); |
| 142 |
|
| 143 |
$add->{options} = {} unless ( defined $add->{options} ); |
| 144 |
$add->{options}->{clientId} = $add->{clientId}; |
| 145 |
$add->{options}->{redirectUris} = $add->{redirectUris}; |
| 146 |
|
| 147 |
my $res = $self->_pushOidcRp( $conf, $add->{confKey}, $add, 1 ); |
| 148 |
|
| 149 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 150 |
unless ( $res->{res} eq 'ok' ); |
| 151 |
|
| 152 |
return $self->sendJSONresponse( |
| 153 |
$req, |
| 154 |
{ message => "Successful operation" }, |
| 155 |
code => 201 |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
sub updateOidcRp { |
| 160 |
my ( $self, $req ) = @_; |
| 161 |
my $confKey = $req->params('confKey') |
| 162 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 163 |
|
| 164 |
my $update = $req->jsonBodyToObj; |
| 165 |
|
| 166 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 167 |
unless ($update); |
| 168 |
|
| 169 |
if ( $update->{redirectUris} ) { |
| 170 |
if ( ref( $update->{redirectUris} ) eq "ARRAY" ) { |
| 171 |
$update->{options}->{redirectUris} = $update->{redirectUris}; |
| 172 |
} |
| 173 |
else { |
| 174 |
return $self->sendError( $req, |
| 175 |
'Invalid input: redirectUris must be an array', 400 ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$self->logger->debug( |
| 180 |
"[API] OIDC RP $confKey configuration update requested"); |
| 181 |
|
| 182 |
# Get latest configuration |
| 183 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 184 |
|
| 185 |
my $current = $self->_getOidcRpByConfKey( $conf, $confKey ); |
| 186 |
|
| 187 |
# Return 404 if not found |
| 188 |
|
| 189 |
return $self->sendError( $req, |
| 190 |
"OIDC relying party '$confKey' not found", 404 ) |
| 191 |
unless ( defined $current ); |
| 192 |
|
| 193 |
# check if new clientID exists already |
| 194 |
my $res = $self->_isNewOidcRpClientIdUnique( $conf, $confKey, $update ); |
| 195 |
|
| 196 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 197 |
unless ( $res->{res} eq 'ok' ); |
| 198 |
|
| 199 |
$res = $self->_pushOidcRp( $conf, $confKey, $update, 0 ); |
| 200 |
|
| 201 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 202 |
unless ( $res->{res} eq 'ok' ); |
| 203 |
|
| 204 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 205 |
} |
| 206 |
|
| 207 |
sub replaceOidcRp { |
| 208 |
my ( $self, $req ) = @_; |
| 209 |
my $confKey = $req->params('confKey') |
| 210 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 211 |
|
| 212 |
my $replace = $req->jsonBodyToObj; |
| 213 |
|
| 214 |
return $self->sendError( $req, "Invalid input: " . $req->error, 400 ) |
| 215 |
unless ($replace); |
| 216 |
|
| 217 |
return $self->sendError( $req, 'Invalid input: clientId is missing', 400 ) |
| 218 |
unless ( defined $replace->{clientId} ); |
| 219 |
|
| 220 |
return $self->sendError( $req, 'Invalid input: clientId is not a string', |
| 221 |
400 ) |
| 222 |
if ( ref $replace->{clientId} ); |
| 223 |
|
| 224 |
return $self->sendError( $req, 'Invalid input: redirectUris is missing', |
| 225 |
400 ) |
| 226 |
unless ( defined $replace->{redirectUris} ); |
| 227 |
|
| 228 |
return $self->sendError( $req, |
| 229 |
'Invalid input: redirectUris must be an array', 400 ) |
| 230 |
unless ( ref( $replace->{redirectUris} ) eq "ARRAY" ); |
| 231 |
|
| 232 |
$self->logger->debug( |
| 233 |
"[API] OIDC RP $confKey configuration replace requested"); |
| 234 |
|
| 235 |
# Get latest configuration |
| 236 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 237 |
|
| 238 |
# Return 404 if not found |
| 239 |
|
| 240 |
return $self->sendError( $req, |
| 241 |
"OIDC relying party '$confKey' not found", 404 ) |
| 242 |
unless ( defined $self->_getOidcRpByConfKey( $conf, $confKey ) ); |
| 243 |
|
| 244 |
# check if new clientID exists already |
| 245 |
my $res = $self->_isNewOidcRpClientIdUnique( $conf, $confKey, $replace ); |
| 246 |
return $self->sendError( $req, $res->{msg}, 409 ) |
| 247 |
unless ( $res->{res} eq 'ok' ); |
| 248 |
|
| 249 |
$replace->{options} = {} unless ( defined $replace->{options} ); |
| 250 |
$replace->{options}->{clientId} = $replace->{clientId}; |
| 251 |
$replace->{options}->{redirectUris} = $replace->{redirectUris}; |
| 252 |
|
| 253 |
$res = $self->_pushOidcRp( $conf, $confKey, $replace, 1 ); |
| 254 |
return $self->sendError( $req, $res->{msg}, 400 ) |
| 255 |
unless ( $res->{res} eq 'ok' ); |
| 256 |
|
| 257 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 258 |
} |
| 259 |
|
| 260 |
sub deleteOidcRp { |
| 261 |
my ( $self, $req ) = @_; |
| 262 |
my $confKey = $req->params('confKey') |
| 263 |
or return $self->sendError( $req, 'confKey is missing', 400 ); |
| 264 |
|
| 265 |
# Get latest configuration |
| 266 |
my $conf = $self->_confAcc->getConf( { noCache => 1 } ); |
| 267 |
|
| 268 |
my $delete = $self->_getOidcRpByConfKey( $conf, $confKey ); |
| 269 |
|
| 270 |
# Return 404 if not found |
| 271 |
|
| 272 |
return $self->sendError( $req, |
| 273 |
"OIDC relying party '$confKey' not found", 404 ) |
| 274 |
unless ( defined $delete ); |
| 275 |
|
| 276 |
delete $conf->{oidcRPMetaDataOptions}->{$confKey}; |
| 277 |
delete $conf->{oidcRPMetaDataExportedVars}->{$confKey}; |
| 278 |
delete $conf->{oidcRPMetaDataOptionsExtraClaims}->{$confKey}; |
| 279 |
delete $conf->{oidcRPMetaDataMacros}->{$confKey}; |
| 280 |
delete $conf->{oidcRPMetaDataScopeRules}->{$confKey}; |
| 281 |
|
| 282 |
# Save configuration |
| 283 |
$self->_saveApplyConf($conf); |
| 284 |
|
| 285 |
return $self->sendJSONresponse( $req, undef, code => 204 ); |
| 286 |
} |
| 287 |
|
| 288 |
sub _getOidcRpByConfKey { |
| 289 |
my ( $self, $conf, $confKey ) = @_; |
| 290 |
|
| 291 |
# Check if confKey is defined |
| 292 |
return undef unless ( defined $conf->{oidcRPMetaDataOptions}->{$confKey} ); |
| 293 |
|
| 294 |
# Get Client ID |
| 295 |
my $clientId = $conf->{oidcRPMetaDataOptions}->{$confKey} |
| 296 |
->{oidcRPMetaDataOptionsClientID}; |
| 297 |
|
| 298 |
# Get exported vars |
| 299 |
my $exportedVars = $conf->{oidcRPMetaDataExportedVars}->{$confKey}; |
| 300 |
|
| 301 |
# Get extra claim |
| 302 |
my $extraClaims = $conf->{oidcRPMetaDataOptionsExtraClaims}->{$confKey}; |
| 303 |
|
| 304 |
# Get macros |
| 305 |
my $macros = $conf->{oidcRPMetaDataMacros}->{$confKey} || {}; |
| 306 |
|
| 307 |
# Get scope rules |
| 308 |
my $scopeRules = $conf->{oidcRPMetaDataScopeRules}->{$confKey} || {}; |
| 309 |
|
| 310 |
# Redirect URIs, filled later |
| 311 |
my $redirectUris = $self->_translateValueConfToApi( |
| 312 |
'oidcRPMetaDataOptionsRedirectUris', |
| 313 |
$conf->{oidcRPMetaDataOptions}->{$confKey} |
| 314 |
->{oidcRPMetaDataOptionsRedirectUris} |
| 315 |
); |
| 316 |
|
| 317 |
# Get options |
| 318 |
my $options = {}; |
| 319 |
for |
| 320 |
my $configOption ( keys %{ $conf->{oidcRPMetaDataOptions}->{$confKey} } ) |
| 321 |
{ |
| 322 |
my $apiName = $self->_translateOptionConfToApi($configOption); |
| 323 |
my $apiValue = $self->_translateValueConfToApi( $configOption, |
| 324 |
$conf->{oidcRPMetaDataOptions}->{$confKey}->{$configOption} ); |
| 325 |
$options->{$apiName} = $apiValue; |
| 326 |
} |
| 327 |
|
| 328 |
return { |
| 329 |
confKey => $confKey, |
| 330 |
clientId => $clientId, |
| 331 |
redirectUris => $redirectUris, |
| 332 |
exportedVars => $exportedVars, |
| 333 |
extraClaims => $extraClaims, |
| 334 |
macros => $macros, |
| 335 |
scopeRules => $scopeRules, |
| 336 |
options => $options |
| 337 |
}; |
| 338 |
} |
| 339 |
|
| 340 |
sub _getOidcRpByClientId { |
| 341 |
my ( $self, $conf, $clientId ) = @_; |
| 342 |
|
| 343 |
foreach ( keys %{ $conf->{oidcRPMetaDataOptions} } ) { |
| 344 |
return $self->_getOidcRpByConfKey( $conf, $_ ) |
| 345 |
if ( $conf->{oidcRPMetaDataOptions}->{$_} |
| 346 |
->{oidcRPMetaDataOptionsClientID} eq $clientId ); |
| 347 |
} |
| 348 |
|
| 349 |
return undef; |
| 350 |
} |
| 351 |
|
| 352 |
sub _isNewOidcRpClientIdUnique { |
| 353 |
my ( $self, $conf, $confKey, $oidcRp ) = @_; |
| 354 |
my $curClientId = $self->_getOidcRpByConfKey( $conf, $confKey )->{clientId}; |
| 355 |
my $newClientId = |
| 356 |
$oidcRp->{clientId} |
| 357 |
|| $oidcRp->{options}->{clientId} |
| 358 |
|| ""; |
| 359 |
if ( $newClientId ne '' && $newClientId ne $curClientId ) { |
| 360 |
return { |
| 361 |
res => 'ko', |
| 362 |
msg => |
| 363 |
"An OIDC relying party with clientId '$newClientId' already exists" |
| 364 |
} |
| 365 |
if ( defined $self->_getOidcRpByClientId( $conf, $newClientId ) ); |
| 366 |
} |
| 367 |
|
| 368 |
return { res => 'ok' }; |
| 369 |
} |
| 370 |
|
| 371 |
sub _pushOidcRp { |
| 372 |
my ( $self, $conf, $confKey, $push, $replace ) = @_; |
| 373 |
|
| 374 |
my $translatedOptions = {}; |
| 375 |
if ($replace) { |
| 376 |
$conf->{oidcRPMetaDataOptions}->{$confKey} = {}; |
| 377 |
$conf->{oidcRPMetaDataExportedVars}->{$confKey} = {}; |
| 378 |
$conf->{oidcRPMetaDataOptionsExtraClaims}->{$confKey} = {}; |
| 379 |
$conf->{oidcRPMetaDataScopeRules}->{$confKey} = {}; |
| 380 |
$conf->{oidcRPMetaDataMacros}->{$confKey} = {}; |
| 381 |
$translatedOptions = $self->_getDefaultValues('oidcRPMetaDataNode'); |
| 382 |
} |
| 383 |
|
| 384 |
if ( defined $push->{options} ) { |
| 385 |
|
| 386 |
foreach ( keys %{ $push->{options} } ) { |
| 387 |
|
| 388 |
my $optionName = $self->_translateOptionApiToConf( $_, 'oidcRP' ); |
| 389 |
eval { |
| 390 |
my $optionValue = |
| 391 |
$self->_translateValueApiToConf( $optionName, |
| 392 |
$push->{options}->{$_} ); |
| 393 |
$translatedOptions->{$optionName} = $optionValue; |
| 394 |
}; |
| 395 |
if ($@) { |
| 396 |
return { |
| 397 |
res => 'ko', |
| 398 |
msg => "Invalid input: $@", |
| 399 |
}; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
my $res = $self->_hasAllowedAttributes( $translatedOptions, |
| 404 |
'oidcRPMetaDataNode' ); |
| 405 |
return $res unless ( $res->{res} eq 'ok' ); |
| 406 |
|
| 407 |
$self->_merge_hash( $conf, 'oidcRPMetaDataOptions', $confKey, |
| 408 |
$translatedOptions ); |
| 409 |
} |
| 410 |
|
| 411 |
$conf->{oidcRPMetaDataOptions}->{$confKey}->{oidcRPMetaDataOptionsClientID} |
| 412 |
= $push->{clientId} |
| 413 |
if ( defined $push->{clientId} ); |
| 414 |
|
| 415 |
if ( defined $push->{exportedVars} ) { |
| 416 |
if ( $self->_isSimpleKeyValueHash( $push->{exportedVars} ) ) { |
| 417 |
$self->_merge_hash( |
| 418 |
$conf, 'oidcRPMetaDataExportedVars', |
| 419 |
$confKey, $push->{exportedVars} |
| 420 |
); |
| 421 |
} |
| 422 |
else { |
| 423 |
return { |
| 424 |
res => 'ko', |
| 425 |
msg => |
| 426 |
"Invalid input: exportedVars is not a hash object with \"key\":\"value\" attributes" |
| 427 |
}; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if ( defined $push->{macros} ) { |
| 432 |
if ( $self->_isSimpleKeyValueHash( $push->{macros} ) ) { |
| 433 |
$self->_merge_hash( $conf, 'oidcRPMetaDataMacros', $confKey, |
| 434 |
$push->{macros} ); |
| 435 |
} |
| 436 |
else { |
| 437 |
return { |
| 438 |
res => 'ko', |
| 439 |
msg => "Invalid input: macros is not a hash object" |
| 440 |
. " with \"key\":\"value\" attributes" |
| 441 |
}; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if ( defined $push->{scopeRules} ) { |
| 446 |
if ( $self->_isSimpleKeyValueHash( $push->{scopeRules} ) ) { |
| 447 |
$self->_merge_hash( |
| 448 |
$conf, 'oidcRPMetaDataScopeRules', |
| 449 |
$confKey, $push->{scopeRules} |
| 450 |
); |
| 451 |
} |
| 452 |
else { |
| 453 |
return { |
| 454 |
res => 'ko', |
| 455 |
msg => "Invalid input: scopeRules is not a hash object" |
| 456 |
. " with \"key\":\"value\" attributes" |
| 457 |
}; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
if ( defined $push->{extraClaims} ) { |
| 462 |
if ( $self->_isSimpleKeyValueHash( $push->{extraClaims} ) ) { |
| 463 |
$self->_merge_hash( $conf, 'oidcRPMetaDataOptionsExtraClaims', |
| 464 |
$confKey, $push->{extraClaims} ); |
| 465 |
} |
| 466 |
else { |
| 467 |
return { |
| 468 |
res => 'ko', |
| 469 |
msg => "Invalid input: extraClaims is not a hash object" |
| 470 |
. " with \"key\":\"value\" attributes" |
| 471 |
}; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
# Test new configuration |
| 476 |
my $parser = Lemonldap::NG::Manager::Conf::Parser->new( { |
| 477 |
refConf => $self->_confAcc->getConf( { noCache => 1 } ), |
| 478 |
newConf => $conf, |
| 479 |
req => {}, |
| 480 |
} |
| 481 |
); |
| 482 |
unless ( $parser->testNewConf( $self->p ) ) { |
| 483 |
return { |
| 484 |
res => 'ko', |
| 485 |
code => 400, |
| 486 |
msg => "Configuration error: " |
| 487 |
. join( ". ", map { $_->{message} } @{ $parser->errors } ), |
| 488 |
}; |
| 489 |
} |
| 490 |
|
| 491 |
# Save configuration |
| 492 |
$self->_saveApplyConf($conf); |
| 493 |
|
| 494 |
return { res => 'ok' }; |
| 495 |
} |
| 496 |
|
| 497 |
1; |