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.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [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  
  23  /**
  24   * Class linkedin.
  25   *
  26   * Custom oauth2 issuer for linkedin as it doesn't support OIDC and has a different way to get
  27   * key information for users - firstname, lastname, email.
  28   *
  29   * @copyright  2021 Peter Dias
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   * @package    core
  32   */
  33  class linkedin implements issuer_interface {
  34      /**
  35       * Build an OAuth2 issuer, with all the default values for this service.
  36       *
  37       * @return issuer The issuer initialised with proper default values.
  38       */
  39      public static function init(): issuer {
  40          $record = (object) [
  41              'name' => 'LinkedIn',
  42              'image' => 'https://static.licdn.com/scds/common/u/images/logos/favicons/v1/favicon.ico',
  43              'baseurl' => 'https://api.linkedin.com/v2',
  44              'loginscopes' => 'r_liteprofile r_emailaddress',
  45              'loginscopesoffline' => 'r_liteprofile r_emailaddress',
  46              'showonloginpage' => issuer::EVERYWHERE,
  47              'servicetype' => 'linkedin',
  48          ];
  49  
  50          $issuer = new issuer(0, $record);
  51          return $issuer;
  52      }
  53  
  54      /**
  55       * Create endpoints for this issuer.
  56       *
  57       * @param issuer $issuer Issuer the endpoints should be created for.
  58       * @return issuer
  59       */
  60      public static function create_endpoints(issuer $issuer): issuer {
  61          $endpoints = [
  62              'authorization_endpoint' => 'https://www.linkedin.com/oauth/v2/authorization',
  63              'token_endpoint' => 'https://www.linkedin.com/oauth/v2/accessToken',
  64              'email_endpoint' => 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))',
  65              'userinfo_endpoint' => "https://api.linkedin.com/v2/me?projection=(localizedFirstName,localizedLastName,"
  66                                          . "profilePicture(displayImage~digitalmediaAsset:playableStreams))",
  67          ];
  68          foreach ($endpoints as $name => $url) {
  69              $record = (object) [
  70                  'issuerid' => $issuer->get('id'),
  71                  'name' => $name,
  72                  'url' => $url
  73              ];
  74              $endpoint = new endpoint(0, $record);
  75              $endpoint->create();
  76          }
  77  
  78          // Create the field mappings.
  79          $mapping = [
  80              'localizedFirstName' => 'firstname',
  81              'localizedLastName' => 'lastname',
  82              'elements[0]-handle~-emailAddress' => 'email',
  83              'profilePicture-displayImage~-elements[0]-identifiers[0]-identifier' => 'picture'
  84          ];
  85          foreach ($mapping as $external => $internal) {
  86              $record = (object) [
  87                  'issuerid' => $issuer->get('id'),
  88                  'externalfield' => $external,
  89                  'internalfield' => $internal
  90              ];
  91              $userfieldmapping = new user_field_mapping(0, $record);
  92              $userfieldmapping->create();
  93          }
  94  
  95          return $issuer;
  96      }
  97  
  98      /**
  99       * Linkedin does not have a discovery url that could be found. Return empty.
 100       * @param issuer $issuer
 101       * @return int
 102       */
 103      public static function discover_endpoints($issuer): int {
 104          return 0;
 105      }
 106  }