Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403]

   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  namespace core\oauth2\service;
  18  
  19  use core\oauth2\issuer;
  20  use core\oauth2\endpoint;
  21  use core\oauth2\user_field_mapping;
  22  use core\oauth2\discovery\openidconnect;
  23  
  24  /**
  25   * Class for Nextcloud oAuth service, with the specific methods related to it.
  26   *
  27   * @package    core
  28   * @copyright  2021 Sara Arjona (sara@moodle.com)
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class nextcloud extends openidconnect implements issuer_interface {
  32  
  33      /**
  34       * Build an OAuth2 issuer, with all the default values for this service.
  35       *
  36       * @return issuer The issuer initialised with proper default values.
  37       */
  38      public static function init(): issuer {
  39          $record = (object) [
  40              'name' => 'Nextcloud',
  41              'image' => 'https://nextcloud.com/wp-content/uploads/2022/03/favicon.png',
  42              'basicauth' => 1,
  43              'servicetype' => 'nextcloud',
  44          ];
  45  
  46          $issuer = new issuer(0, $record);
  47  
  48          return $issuer;
  49      }
  50  
  51      /**
  52       * Create endpoints for this issuer.
  53       *
  54       * @param issuer $issuer Issuer the endpoints should be created for.
  55       * @return issuer
  56       */
  57      public static function create_endpoints(issuer $issuer): issuer {
  58          // Nextcloud has a custom baseurl. Thus, the creation of endpoints has to be done later.
  59          $baseurl = $issuer->get('baseurl');
  60          // Add trailing slash to baseurl, if needed.
  61          if (substr($baseurl, -1) !== '/') {
  62              $baseurl .= '/';
  63          }
  64  
  65          $endpoints = [
  66              // Baseurl will be prepended later.
  67              'authorization_endpoint' => 'index.php/apps/oauth2/authorize',
  68              'token_endpoint' => 'index.php/apps/oauth2/api/v1/token',
  69              'userinfo_endpoint' => 'ocs/v2.php/cloud/user?format=json',
  70              'webdav_endpoint' => 'remote.php/webdav/',
  71              'ocs_endpoint' => 'ocs/v1.php/apps/files_sharing/api/v1/shares',
  72          ];
  73  
  74          foreach ($endpoints as $name => $url) {
  75              $record = (object) [
  76                  'issuerid' => $issuer->get('id'),
  77                  'name' => $name,
  78                  'url' => $baseurl . $url,
  79              ];
  80              $endpoint = new \core\oauth2\endpoint(0, $record);
  81              $endpoint->create();
  82          }
  83  
  84          // Create the field mappings.
  85          $mapping = [
  86              'ocs-data-email' => 'email',
  87              'ocs-data-id' => 'username',
  88          ];
  89          foreach ($mapping as $external => $internal) {
  90              $record = (object) [
  91                  'issuerid' => $issuer->get('id'),
  92                  'externalfield' => $external,
  93                  'internalfield' => $internal
  94              ];
  95              $userfieldmapping = new \core\oauth2\user_field_mapping(0, $record);
  96              $userfieldmapping->create();
  97          }
  98          return $issuer;
  99      }
 100  }