Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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  /**
  19   * Provide static functions for creating and validating issuers.
  20   *
  21   * @package    repository_nextcloud
  22   * @copyright  2018 Jan Dageförde (Learnweb, University of Münster)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace repository_nextcloud;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Provide static functions for creating and validating issuers.
  31   *
  32   * @package    repository_nextcloud
  33   * @copyright  2018 Jan Dageförde (Learnweb, University of Münster)
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class issuer_management {
  37  
  38      /**
  39       * Check if an issuer provides all endpoints that are required by repository_nextcloud.
  40       * @param \core\oauth2\issuer $issuer An issuer.
  41       * @return bool True, if all endpoints exist; false otherwise.
  42       */
  43      public static function is_valid_issuer(\core\oauth2\issuer $issuer) {
  44          $endpointwebdav = false;
  45          $endpointocs = false;
  46          $endpointtoken = false;
  47          $endpointauth = false;
  48          $endpointuserinfo = false;
  49          $endpoints = \core\oauth2\api::get_endpoints($issuer);
  50          foreach ($endpoints as $endpoint) {
  51              $name = $endpoint->get('name');
  52              switch ($name) {
  53                  case 'webdav_endpoint':
  54                      $endpointwebdav = true;
  55                      break;
  56                  case 'ocs_endpoint':
  57                      $endpointocs = true;
  58                      break;
  59                  case 'token_endpoint':
  60                      $endpointtoken = true;
  61                      break;
  62                  case 'authorization_endpoint':
  63                      $endpointauth = true;
  64                      break;
  65                  case 'userinfo_endpoint':
  66                      $endpointuserinfo = true;
  67                      break;
  68              }
  69          }
  70          return $endpointwebdav && $endpointocs && $endpointtoken && $endpointauth && $endpointuserinfo;
  71      }
  72  
  73      /**
  74       * Returns the parsed url parts of an endpoint of an issuer.
  75       * @param string $endpointname
  76       * @param \core\oauth2\issuer $issuer
  77       * @return array parseurl [scheme => https/http, host=>'hostname', port=>443, path=>'path']
  78       * @throws configuration_exception if an endpoint is undefined
  79       */
  80      public static function parse_endpoint_url(string $endpointname, \core\oauth2\issuer $issuer) : array {
  81          $url = $issuer->get_endpoint_url($endpointname);
  82          if (empty($url)) {
  83              throw new configuration_exception(get_string('endpointnotdefined', 'repository_nextcloud', $endpointname));
  84          }
  85          return parse_url($url);
  86      }
  87  }