Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file allows for testing of login via configured oauth2 IDP poviders.
  19   *
  20   * @package auth_oauth2
  21   * @copyright 2021 Matt Porritt <mattp@catalyst-au.net>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23   */
  24  
  25  // Require_login is not needed here.
  26  // phpcs:disable moodle.Files.RequireLogin.Missing
  27  require_once('../../config.php');
  28  
  29  require_sesskey();
  30  
  31  $issuerid = required_param('id', PARAM_INT);
  32  $url = new moodle_url('/auth/oauth2/test.php', ['id' => $issuerid, 'sesskey' => sesskey()]);
  33  
  34  $PAGE->set_context(context_system::instance());
  35  $PAGE->set_url($url);
  36  $PAGE->set_pagelayout('admin');
  37  
  38  if (!\auth_oauth2\api::is_enabled()) {
  39      throw new \moodle_exception('notenabled', 'auth_oauth2');
  40  }
  41  
  42  $issuer = new \core\oauth2\issuer($issuerid);
  43  if (!$issuer->is_available_for_login()) {
  44      throw new \moodle_exception('issuernologin', 'auth_oauth2');
  45  }
  46  
  47  $client = \core\oauth2\api::get_user_oauth_client($issuer, $url);
  48  
  49  if ($client) {
  50      // We have a valid client, now lets see if we can log into the IDP.
  51      if (!$client->is_logged_in()) {
  52          redirect($client->get_login_url());
  53      }
  54  
  55      echo $OUTPUT->header();
  56  
  57      // We were successful logging into the IDP.
  58      echo $OUTPUT->notification(get_string('loggedin', 'auth_oauth2'), 'notifysuccess');
  59  
  60      // Try getting user info from the IDP.
  61      $endpointurl = $client->get_issuer()->get_endpoint_url('userinfo');
  62      $response = $client->get($endpointurl);
  63      $userinfo = json_decode($response, true);
  64  
  65      $templateinfo = [];
  66      foreach ($userinfo as $key => $value) {
  67          // We are just displaying the data from the IdP for testing purposes,
  68          // so we are more interested in displaying it to the admin than
  69          // processing it.
  70          if (is_array($value)) {
  71              $value = json_encode($value);
  72          }
  73          $templateinfo[] = ['name' => $key, 'value' => $value];
  74      }
  75  
  76      // Display user info.
  77      if (!empty($templateinfo)) {
  78          echo $OUTPUT->render_from_template('auth_oauth2/idpresponse', ['pairs' => $templateinfo]);
  79      }
  80  
  81  } else {
  82      throw new moodle_exception('Could not get an OAuth client.');
  83  }
  84  
  85  echo $OUTPUT->footer();