[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/ -> XOAuth2Authenticator.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Handles XOAUTH2 authentication.
  13   *
  14   * Example:
  15   * <code>
  16   * $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
  17   *   ->setAuthMode('XOAUTH2')
  18   *   ->setUsername('YOUR_EMAIL_ADDRESS')
  19   *   ->setPassword('YOUR_ACCESS_TOKEN');
  20   * </code>
  21   *
  22   * @author xu.li<AthenaLightenedMyPath@gmail.com>
  23   *
  24   * @see        https://developers.google.com/google-apps/gmail/xoauth2_protocol
  25   */
  26  class Swift_Transport_Esmtp_Auth_XOAuth2Authenticator implements Swift_Transport_Esmtp_Authenticator
  27  {
  28      /**
  29       * Get the name of the AUTH mechanism this Authenticator handles.
  30       *
  31       * @return string
  32       */
  33      public function getAuthKeyword()
  34      {
  35          return 'XOAUTH2';
  36      }
  37  
  38      /**
  39       * {@inheritdoc}
  40       */
  41      public function authenticate(Swift_Transport_SmtpAgent $agent, $email, $token)
  42      {
  43          try {
  44              $param = $this->constructXOAuth2Params($email, $token);
  45              $agent->executeCommand('AUTH XOAUTH2 '.$param."\r\n", [235]);
  46  
  47              return true;
  48          } catch (Swift_TransportException $e) {
  49              $agent->executeCommand("RSET\r\n", [250]);
  50  
  51              throw $e;
  52          }
  53      }
  54  
  55      /**
  56       * Construct the auth parameter.
  57       *
  58       * @see https://developers.google.com/google-apps/gmail/xoauth2_protocol#the_sasl_xoauth2_mechanism
  59       */
  60      protected function constructXOAuth2Params($email, $token)
  61      {
  62          return base64_encode("user=$email\1auth=Bearer $token\1\1");
  63      }
  64  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1