Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 Microsoft 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 microsoft 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' => 'Microsoft',
  41              'image' => 'https://www.microsoft.com/favicon.ico',
  42              'baseurl' => '',
  43              'loginscopes' => 'openid profile email user.read',
  44              'loginscopesoffline' => 'openid profile email user.read offline_access',
  45              'showonloginpage' => issuer::EVERYWHERE,
  46              'servicetype' => 'microsoft',
  47          ];
  48  
  49          $issuer = new issuer(0, $record);
  50          return $issuer;
  51      }
  52  
  53      /**
  54       * Create endpoints for this issuer.
  55       *
  56       * @param issuer $issuer Issuer the endpoints should be created for.
  57       * @return issuer
  58       */
  59      public static function create_endpoints(issuer $issuer): issuer {
  60          $endpoints = [
  61              'authorization_endpoint' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
  62              'token_endpoint' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
  63              'userinfo_endpoint' => 'https://graph.microsoft.com/v1.0/me/',
  64              'userpicture_endpoint' => 'https://graph.microsoft.com/v1.0/me/photo/$value',
  65          ];
  66          foreach ($endpoints as $name => $url) {
  67              $record = (object) [
  68                  'issuerid' => $issuer->get('id'),
  69                  'name' => $name,
  70                  'url' => $url
  71              ];
  72              $endpoint = new endpoint(0, $record);
  73              $endpoint->create();
  74          }
  75  
  76          // Create the field mappings.
  77          $mapping = [
  78              'givenName' => 'firstname',
  79              'surname' => 'lastname',
  80              'userPrincipalName' => 'email',
  81              'displayName' => 'alternatename',
  82              'officeLocation' => 'address',
  83              'mobilePhone' => 'phone1',
  84              'preferredLanguage' => 'lang'
  85          ];
  86          foreach ($mapping as $external => $internal) {
  87              $record = (object) [
  88                  'issuerid' => $issuer->get('id'),
  89                  'externalfield' => $external,
  90                  'internalfield' => $internal
  91              ];
  92              $userfieldmapping = new user_field_mapping(0, $record);
  93              $userfieldmapping->create();
  94          }
  95  
  96          return $issuer;
  97      }
  98  }