Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  namespace core\oauth2\service;
  18  
  19  use core\oauth2\issuer;
  20  use core\oauth2\discovery\openidconnect;
  21  use core\oauth2\endpoint;
  22  use core\oauth2\user_field_mapping;
  23  
  24  /**
  25   * Class for Clever OAuth service, with the specific methods related to it.
  26   *
  27   * @package    core
  28   * @copyright  2022 OpenStax
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class clever extends openidconnect implements issuer_interface {
  32      /**
  33       * Build an OAuth2 issuer, with all the default values for this service.
  34       *
  35       * @return issuer The issuer initialised with proper default values.
  36       */
  37      public static function init(): issuer {
  38          $record = (object) [
  39              'name' => 'Clever',
  40              'image' => 'https://apps.clever.com/favicon.ico',
  41              'basicauth' => 1,
  42              'baseurl' => '',
  43              'showonloginpage' => issuer::LOGINONLY,
  44              'servicetype' => 'clever',
  45          ];
  46  
  47          return new issuer(0, $record);
  48      }
  49  
  50      /**
  51       * Create endpoints for this issuer.
  52       *
  53       * @param issuer $issuer Issuer the endpoints should be created for.
  54       * @return issuer
  55       */
  56      public static function create_endpoints(issuer $issuer): issuer {
  57          $endpoints = [
  58              'authorization_endpoint' => 'https://clever.com/oauth/authorize',
  59              'token_endpoint' => 'https://clever.com/oauth/tokens',
  60              'userinfo_endpoint' => 'https://api.clever.com/v3.0/me',
  61              'userdata_endpoint' => 'https://api.clever.com/v3.0/users'
  62          ];
  63          foreach ($endpoints as $name => $url) {
  64              $record = (object) [
  65                  'issuerid' => $issuer->get('id'),
  66                  'name' => $name,
  67                  'url' => $url
  68              ];
  69              $endpoint = new endpoint(0, $record);
  70              $endpoint->create();
  71          }
  72  
  73          // Create the field mappings.
  74          $mapping = [
  75              'data-id' => 'idnumber',
  76              'data-name-first' => 'firstname',
  77              'data-name-last' => 'lastname',
  78              'data-email' => 'email'
  79          ];
  80          foreach ($mapping as $external => $internal) {
  81              $record = (object) [
  82                  'issuerid' => $issuer->get('id'),
  83                  'externalfield' => $external,
  84                  'internalfield' => $internal
  85              ];
  86              $userfieldmapping = new user_field_mapping(0, $record);
  87              $userfieldmapping->create();
  88          }
  89  
  90          return $issuer;
  91      }
  92  }