| Conditions | 52 |
| Paths | 1768 |
| Total Lines | 346 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 245 | function verifyOIDCParams() |
||
| 246 | { |
||
| 247 | $retval = false; // Assume OIDC session info is not valid |
||
| 248 | |||
| 249 | // Combine the $_GET and $_POST arrays into a single array which can be |
||
| 250 | // stored in the 'clientparams' session variable as a JSON object. |
||
| 251 | $clientparams = array(); |
||
| 252 | foreach ($_GET as $key => $value) { |
||
| 253 | $clientparams[$key] = $value; |
||
| 254 | } |
||
| 255 | foreach ($_POST as $key => $value) { |
||
| 256 | $clientparams[$key] = $value; |
||
| 257 | } |
||
| 258 | |||
| 259 | // CIL-624 If X509 certs are disabled, check for 'getcert' scope. |
||
| 260 | // If found, show an error message. |
||
| 261 | $scope = Util::getGetVar('scope'); |
||
| 262 | if ( |
||
| 263 | (defined('DISABLE_X509')) && |
||
| 264 | (DISABLE_X509 === true) && |
||
| 265 | (preg_match('/edu.uiuc.ncsa.myproxy.getcert/', $scope)) |
||
| 266 | ) { |
||
| 267 | Util::sendErrorAlert( |
||
| 268 | 'CILogon OIDC authz endpoint error', |
||
| 269 | 'The CILogon OIDC authorization endpoint received a request ' . |
||
| 270 | 'including the "edu.ncsa.uiuc.myproxy.getcert" scope, ' . |
||
| 271 | 'but the server is configured with DISABLE_X509 to prevent ' . |
||
| 272 | 'downloading certificates. ' . |
||
| 273 | "\n\n" . |
||
| 274 | 'clientparams = ' . print_r($clientparams, true) . |
||
| 275 | "\n" |
||
| 276 | ); |
||
| 277 | Util::setSessionVar( |
||
| 278 | 'client_error_msg', |
||
| 279 | 'The CILogon Service is currently configured to prevent ' . |
||
| 280 | 'downloading X.509 certificates, but the incoming request ' . |
||
| 281 | 'included the "edu.ncsa.uiuc.myproxy.getcert" scope. ' . |
||
| 282 | 'CILogon system administrators have been notified.' |
||
| 283 | ); |
||
| 284 | $clientparams = array(); |
||
| 285 | |||
| 286 | // If the 'redirect_uri' parameter was passed in then let the 'real' |
||
| 287 | // OA4MP OIDC authz endpoint handle parse the request since it might be |
||
| 288 | // possible to return an error code to the client. |
||
| 289 | } elseif (isset($clientparams['redirect_uri'])) { |
||
| 290 | $ch = curl_init(); |
||
| 291 | if ($ch !== false) { |
||
| 292 | $url = OAUTH2_CREATE_TRANSACTION_URL; |
||
| 293 | if (count($_GET) > 0) { |
||
| 294 | // CIL-658 Look for double-encoded spaces in 'scope' |
||
| 295 | if (strlen($scope) > 0) { |
||
| 296 | $_GET['scope'] = preg_replace('/(\+|%2B)/', ' ', $scope); |
||
| 297 | } |
||
| 298 | $url .= (preg_match('/\?/', $url) ? '&' : '?') . |
||
| 299 | http_build_query($_GET); |
||
| 300 | } |
||
| 301 | if (count($_POST) > 0) { |
||
| 302 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 303 | curl_setopt($ch, CUROPT_POSTFIELDS, http_build_query($_POST)); |
||
| 304 | } |
||
| 305 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 306 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 307 | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
||
| 308 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // Catch redirects |
||
| 309 | $output = curl_exec($ch); |
||
| 310 | if (curl_errno($ch)) { // Send alert on curl errors |
||
| 311 | Util::sendErrorAlert( |
||
| 312 | 'cUrl Error', |
||
| 313 | 'cUrl Error = ' . curl_error($ch) . "\n" . |
||
| 314 | "URL Accessed = $url" . |
||
| 315 | "\n\n" . |
||
| 316 | 'clientparams = ' . print_r($clientparams, true) |
||
| 317 | ); |
||
| 318 | $clientparams = array(); |
||
| 319 | } else { |
||
| 320 | $info = curl_getinfo($ch); |
||
| 321 | if ($info !== false) { |
||
| 322 | if ( |
||
| 323 | (isset($info['http_code'])) && |
||
| 324 | ($info['http_code'] == 200) |
||
| 325 | ) { |
||
| 326 | // The OA4MP OIDC authz endpoint responded with 200 |
||
| 327 | // (success). The body of the message should be a |
||
| 328 | // JSON token containing the appropriate parameters |
||
| 329 | // such as the 'code'. |
||
| 330 | $json = json_decode($output, true); |
||
| 331 | if (isset($json['code'])) { |
||
| 332 | // Got 'code' - save to session and read OIDC |
||
| 333 | // client info from the database to display |
||
| 334 | // to the user |
||
| 335 | $clientparams['redirect_url'] = |
||
| 336 | $clientparams['redirect_uri'] . |
||
| 337 | (preg_match('/\?/', $clientparams['redirect_uri']) ? '&' : '?') . |
||
| 338 | http_build_query($json); |
||
| 339 | $clientparams['code'] = $json['code']; |
||
| 340 | // CIL-618 Read OIDC client info from database |
||
| 341 | if (!Util::getOIDCClientParams($clientparams)) { |
||
| 342 | Util::sendErrorAlert( |
||
| 343 | 'getOIDCClientParams Error', |
||
| 344 | 'Error getting OIDC client parameters ' . |
||
| 345 | 'in verifyOIDCParams() function for ' . |
||
| 346 | 'client_id="' . |
||
| 347 | $clientparams['client_id'] . '".' |
||
| 348 | ); |
||
| 349 | $clientparams = array(); |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | // Either the output returned was not a valid |
||
| 353 | // JSON token, or there was no 'code' found in |
||
| 354 | // the returned JSON token. |
||
| 355 | $errortxt = getErrorStatusText($output, $clientparams); |
||
| 356 | |||
| 357 | Util::sendErrorAlert( |
||
| 358 | 'OA4MP OIDC authz endpoint error', |
||
| 359 | (!empty($errortxt) ? $errortxt : |
||
| 360 | 'The OA4MP OIDC authorization endpoint ' . |
||
| 361 | 'returned an HTTP response 200, but either ' . |
||
| 362 | 'the output was not a valid JSON token, or ' . |
||
| 363 | 'there was no "code" in the JSON token. ' . |
||
| 364 | ((strlen($output) > 0) ? |
||
| 365 | "\n\nReturned output =\n$output" : '')) . |
||
| 366 | "\n\n" . |
||
| 367 | 'curl_getinfo = ' . print_r($info, true) . "\n\n" . |
||
| 368 | 'clientparams = ' . print_r($clientparams, true) . |
||
| 369 | "\n" |
||
| 370 | ); |
||
| 371 | Util::setSessionVar( |
||
| 372 | 'client_error_msg', |
||
| 373 | 'There was an unrecoverable error during the transaction. ' . |
||
| 374 | 'CILogon system administrators have been notified. ' . |
||
| 375 | (!empty($errortxt) ? "<p><b>Error message: $errortxt</b><p>" : '') |
||
| 376 | ); |
||
| 377 | $clientparams = array(); |
||
| 378 | } |
||
| 379 | } elseif ( |
||
| 380 | (isset($info['http_code'])) && |
||
| 381 | ($info['http_code'] == 302) |
||
| 382 | ) { |
||
| 383 | // The OA4MP OIDC authz endpoint responded with 302 |
||
| 384 | // (redirect) which indicates an OIDC error was |
||
| 385 | // detected. We need to check the response for an |
||
| 386 | // 'error' and simply redirect error to OIDC client. |
||
| 387 | $redirect_url = ''; |
||
| 388 | if (isset($info['redirect_url'])) { |
||
| 389 | $redirect_url = $info['redirect_url']; |
||
| 390 | $clientparams['redirect_url'] = $redirect_url; |
||
| 391 | // CIL-407 - In case of two question marks '?' |
||
| 392 | // in redirect_url (caused by OIDC authz endpoint |
||
| 393 | // blindly appending "?error=..."), change all |
||
| 394 | // but the first '?' to '&'. |
||
| 395 | // https://stackoverflow.com/a/37150213 |
||
| 396 | if (substr_count($redirect_url, '?') > 1) { |
||
| 397 | $arr = explode('?', $redirect_url, 2); |
||
| 398 | $arr[1] = str_replace('?', '&', $arr[1]); |
||
| 399 | $redirect_url = implode('?', $arr); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | // Get components of redirect_url - need 'query' |
||
| 403 | $comps = parse_url($redirect_url); |
||
| 404 | if ($comps !== false) { |
||
| 405 | // Look for 'error' in query |
||
| 406 | $query = ''; |
||
| 407 | if (isset($comps['query'])) { |
||
| 408 | $query = $comps['query']; |
||
| 409 | $query = html_entity_decode($query); |
||
| 410 | } |
||
| 411 | $queries = explode('&', $query); |
||
| 412 | $params = array(); |
||
| 413 | foreach ($queries as $value) { |
||
| 414 | $x = explode('=', $value); |
||
| 415 | $params[$x[0]] = $x[1]; |
||
| 416 | } |
||
| 417 | if (isset($params['error'])) { |
||
| 418 | // Got 'error' - simply return to OIDC client |
||
| 419 | Util::unsetAllUserSessionVars(); |
||
| 420 | header("Location: $redirect_url"); |
||
| 421 | exit; // No further processing necessary |
||
| 422 | } else { // Weird params - Should never get here! |
||
| 423 | Util::sendErrorAlert( |
||
| 424 | 'OA4MP OIDC 302 Error', |
||
| 425 | 'The OA4MP OIDC authz endpoint ' . |
||
| 426 | 'returned a 302 redirect (error) ' . |
||
| 427 | 'response, but there was no "error" ' . |
||
| 428 | "query parameter.\n\n" . |
||
| 429 | "redirect_url = $redirect_url\n\n" . |
||
| 430 | 'clientparams = ' . |
||
| 431 | print_r($clientparams, true) . |
||
| 432 | "\n" |
||
| 433 | ); |
||
| 434 | $clientparams = array(); |
||
| 435 | } |
||
| 436 | } else { // parse_url($redirect_url) gave error |
||
| 437 | Util::sendErrorAlert( |
||
| 438 | 'parse_url(redirect_url) error', |
||
| 439 | 'There was an error when attempting to ' . |
||
| 440 | 'parse the redirect_url. This should never ' . |
||
| 441 | "happen.\n\n" . |
||
| 442 | "redirect_url = $redirect_url\n\n" . |
||
| 443 | 'clientparams = ' . print_r($clientparams, true) . |
||
| 444 | "\n" |
||
| 445 | ); |
||
| 446 | $clientparams = array(); |
||
| 447 | } |
||
| 448 | } else { |
||
| 449 | // An HTTP return code other than 200 (success) or |
||
| 450 | // 302 (redirect) means that the OA4MP OIDC authz |
||
| 451 | // endpoint tried to handle an unrecoverable error, |
||
| 452 | // possibly by outputting HTML. If so, then we |
||
| 453 | // ignore it and output our own error message to the |
||
| 454 | // user. |
||
| 455 | Util::sendErrorAlert( |
||
| 456 | 'OA4MP OIDC authz endpoint error', |
||
| 457 | 'The OA4MP OIDC authorization endpoint returned ' . |
||
| 458 | 'an HTTP response other than 200 or 302. ' . |
||
| 459 | ((strlen($output) > 0) ? |
||
| 460 | "\n\nReturned output =\n$output" : '') . |
||
| 461 | "\n\n" . |
||
| 462 | 'curl_getinfo = ' . print_r($info, true) . "\n\n" . |
||
| 463 | 'clientparams = ' . print_r($clientparams, true) . |
||
| 464 | "\n" |
||
| 465 | ); |
||
| 466 | // CIL-423 Better end-user error output for errors. |
||
| 467 | // Scan output for ServletException message. |
||
| 468 | $errstr = ''; |
||
| 469 | if ( |
||
| 470 | preg_match( |
||
| 471 | '/javax.servlet.ServletException:\s?(.*)/', |
||
| 472 | $output, |
||
| 473 | $matches |
||
| 474 | ) |
||
| 475 | ) { |
||
| 476 | $output = ''; |
||
| 477 | $errstr = ' |
||
| 478 | <div> |
||
| 479 | <p>Error Message: <b>' . |
||
| 480 | $matches[1] . '</b>.</p> |
||
| 481 | <ul> |
||
| 482 | <li>Did you <b>register</b> your OAuth2/OIDC client? If not, go |
||
| 483 | <b><a target="_blank" href="https://' . |
||
| 484 | Util::getHN() |
||
| 485 | . '/oauth2/register">here</a></b> to do so.</li> |
||
| 486 | <li>Did you receive confirmation that your OAuth2/OIDC client |
||
| 487 | was <b>approved</b>? If not, please wait up to 48 hours for an |
||
| 488 | approval email from CILogon administrators.</li> |
||
| 489 | <li>Did you configure your OAuth2/OIDC client with the |
||
| 490 | registered <b>client ID and secret</b>?</li> |
||
| 491 | </ul> |
||
| 492 | </div>'; |
||
| 493 | } |
||
| 494 | Util::setSessionVar( |
||
| 495 | 'client_error_msg', |
||
| 496 | 'There was an unrecoverable error during the transaction. ' . |
||
| 497 | 'CILogon system administrators have been notified.' . |
||
| 498 | ((strlen($errstr) > 0) ? $errstr : '') . |
||
| 499 | ((strlen($output) > 0) ? |
||
| 500 | '<br/><pre>' . |
||
| 501 | preg_replace('/\+/', ' ', $output) . |
||
| 502 | '</pre>' : '') |
||
| 503 | ); |
||
| 504 | $clientparams = array(); |
||
| 505 | } |
||
| 506 | } else { // curl_getinfo() returned false - should not happen |
||
| 507 | Util::sendErrorAlert( |
||
| 508 | 'curl_getinfo error', |
||
| 509 | 'When attempting to talk to the OA4MP OIDC ' . |
||
| 510 | 'authorization endpoint, curl_getinfo() returned ' . |
||
| 511 | "false. This should never happen.\n\n" . |
||
| 512 | 'clientparams = ' . print_r($clientparams, true) . "\n" |
||
| 513 | ); |
||
| 514 | $clientparams = array(); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | curl_close($ch); |
||
| 518 | } else { // curl_init() returned false - should not happen |
||
| 519 | Util::sendErrorAlert( |
||
| 520 | 'curl_init error', |
||
| 521 | 'When attempting to talk to the OA4MP OIDC authorization ' . |
||
| 522 | 'endpoint, curl_init() returned false. This should never ' . |
||
| 523 | "happen.\n\n" . |
||
| 524 | 'clientparams = ' . print_r($clientparams, true) . "\n" |
||
| 525 | ); |
||
| 526 | $clientparams = array(); |
||
| 527 | } |
||
| 528 | |||
| 529 | // If redirect_uri was not passed in, but one of the other required OIDC |
||
| 530 | // parameters WAS passed in, then assume that this was an attempt by an |
||
| 531 | // OIDC client to use the authz endpoint, and display an error message |
||
| 532 | // that at least one parameter (redirect_uri) was missing from the |
||
| 533 | // request. Note that since we don't have a redirect_uri, we cannot |
||
| 534 | // return code flow back to the OIDC client. |
||
| 535 | } elseif ( |
||
| 536 | (isset($clientparams['client_id'])) || |
||
| 537 | (isset($clientparams['scope'])) || |
||
| 538 | (isset($clientparams['response_type'])) |
||
| 539 | ) { |
||
| 540 | $missing = 'redirect_uri' . |
||
| 541 | ((isset($clientparams['client_id'])) ? '' : ', client_id') . |
||
| 542 | ((isset($clientparams['scope'])) ? '' : ', scope') . |
||
| 543 | ((isset($clientparams['response_type'])) ? '' : ', response_type'); |
||
| 544 | Util::sendErrorAlert( |
||
| 545 | 'CILogon OIDC authz endpoint error', |
||
| 546 | 'The CILogon OIDC authorization endpoint received a request ' . |
||
| 547 | 'from an OIDC client, but at least one of the required ' . |
||
| 548 | 'parameters (' . $missing . ') was missing. ' . |
||
| 549 | "\n\n" . |
||
| 550 | 'clientparams = ' . print_r($clientparams, true) . |
||
| 551 | "\n" |
||
| 552 | ); |
||
| 553 | Util::setSessionVar( |
||
| 554 | 'client_error_msg', |
||
| 555 | 'It appears that an OpenID Connect client attempted to ' . |
||
| 556 | 'initiate a session with the CILogon Service, but at least ' . |
||
| 557 | 'one of the requried parameters (' . $missing . ') ' . |
||
| 558 | 'was missing. CILogon system administrators have been notified.' |
||
| 559 | ); |
||
| 560 | $clientparams = array(); |
||
| 561 | |||
| 562 | // If none of the required OIDC authz endpoint parameters were passed |
||
| 563 | // in, then this might be a later step in the authz process. So check |
||
| 564 | // the session variable array 'clientparams' for the required |
||
| 565 | // information. |
||
| 566 | } else { |
||
| 567 | $clientparams = json_decode(Util::getSessionVar('clientparams'), true); |
||
| 568 | } |
||
| 569 | |||
| 570 | // Now check to verify all variables have data |
||
| 571 | if ( |
||
| 572 | (isset($clientparams['redirect_uri'])) && |
||
| 573 | (isset($clientparams['scope'])) && |
||
| 574 | (isset($clientparams['response_type'])) && |
||
| 575 | (isset($clientparams['client_id'])) && |
||
| 576 | (isset($clientparams['code'])) && |
||
| 577 | (isset($clientparams['client_name'])) && |
||
| 578 | (isset($clientparams['client_home_url'])) && |
||
| 579 | (isset($clientparams['client_callback_uri'])) && |
||
| 580 | (isset($clientparams['client_scopes'])) && |
||
| 581 | (isset($clientparams['redirect_url'])) && |
||
| 582 | (isset($clientparams['clientstatus'])) && |
||
| 583 | (!($clientparams['clientstatus'] & 1)) |
||
| 584 | ) { // STATUS_OK* are even |
||
| 585 | $retval = true; |
||
| 586 | Util::setSessionVar('clientparams', json_encode($clientparams)); |
||
| 587 | } |
||
| 588 | |||
| 589 | return $retval; |
||
| 590 | } |
||
| 591 | |||
| 680 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.