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  /**
   3   * Copyright 2013-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2013-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Generates an OAuth 2.0 authentication token as used in the Gmail XOAUTH2
  16   * authentication mechanism.
  17   *
  18   * See: https://developers.google.com/gmail/xoauth2_protocol
  19   *
  20   * @author    Michael Slusarz <slusarz@horde.org>
  21   * @category  Horde
  22   * @copyright 2013-2017 Horde LLC
  23   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  24   * @package   Imap_Client
  25   * @since     2.16.0
  26   */
  27  class Horde_Imap_Client_Password_Xoauth2
  28  implements Horde_Imap_Client_Base_Password
  29  {
  30      /**
  31       * Access token.
  32       *
  33       * @var string
  34       */
  35      public $access_token;
  36  
  37      /**
  38       * Username.
  39       *
  40       * @var string
  41       */
  42      public $username;
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param string $username      The username.
  48       * @param string $access_token  The access token.
  49       */
  50      public function __construct($username, $access_token)
  51      {
  52          $this->username = $username;
  53          $this->access_token = $access_token;
  54      }
  55  
  56      /**
  57       * Return the password to use for the server connection.
  58       *
  59       * @return string  The password.
  60       */
  61      public function getPassword()
  62      {
  63          // base64("user=" {User} "^Aauth=Bearer " {Access Token} "^A^A")
  64          // ^A represents a Control+A (\001)
  65          return base64_encode(
  66              'user=' . $this->username . "\1" .
  67              'auth=Bearer ' . $this->access_token . "\1\1"
  68          );
  69      }
  70  
  71  }