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\endpoint;
  21  use core\oauth2\user_field_mapping;
  22  use core\oauth2\discovery\openidconnect;
  23  
  24  /**
  25   * Class for Facebook 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 facebook 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' => 'Facebook',
  41              'image' => 'https://facebookbrand.com/wp-content/uploads/2016/05/flogo_rgb_hex-brc-site-250.png',
  42              'baseurl' => '',
  43              'loginscopes' => 'public_profile email',
  44              'loginscopesoffline' => 'public_profile email',
  45              'showonloginpage' => issuer::EVERYWHERE,
  46              'servicetype' => 'facebook',
  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          // The Facebook API version.
  61          $apiversion = '2.12';
  62          // The Graph API URL.
  63          $graphurl = 'https://graph.facebook.com/v' . $apiversion;
  64          // User information fields that we want to fetch.
  65          $infofields = [
  66              'id',
  67              'first_name',
  68              'last_name',
  69              'picture.type(large)',
  70              'name',
  71              'email',
  72          ];
  73          $endpoints = [
  74              'authorization_endpoint' => sprintf('https://www.facebook.com/v%s/dialog/oauth', $apiversion),
  75              'token_endpoint' => $graphurl . '/oauth/access_token',
  76              'userinfo_endpoint' => $graphurl . '/me?fields=' . implode(',', $infofields)
  77          ];
  78  
  79          foreach ($endpoints as $name => $url) {
  80              $record = (object) [
  81                  'issuerid' => $issuer->get('id'),
  82                  'name' => $name,
  83                  'url' => $url
  84              ];
  85              $endpoint = new endpoint(0, $record);
  86              $endpoint->create();
  87          }
  88  
  89          // Create the field mappings.
  90          $mapping = [
  91              'name' => 'alternatename',
  92              'last_name' => 'lastname',
  93              'email' => 'email',
  94              'first_name' => 'firstname',
  95              'picture-data-url' => 'picture',
  96          ];
  97          foreach ($mapping as $external => $internal) {
  98              $record = (object) [
  99                  'issuerid' => $issuer->get('id'),
 100                  'externalfield' => $external,
 101                  'internalfield' => $internal
 102              ];
 103              $userfieldmapping = new user_field_mapping(0, $record);
 104              $userfieldmapping->create();
 105          }
 106  
 107          return $issuer;
 108      }
 109  }