class OAuth

OAuth - OAuth2 authentication wrapper class. Uses the oauth2-client package from the League of Extraordinary Packages.

@author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>

Hierarchy

  • class \PHPMailer\PHPMailer\OAuth

Expanded class hierarchy of OAuth

See also

http://oauth2-client.thephpleague.com

File

inc/PHPMailer/src/OAuth.php, line 35

Namespace

PHPMailer\PHPMailer
View source
class OAuth
 {
  /**
   * An instance of the League OAuth Client Provider.
   *
   * @var AbstractProvider
   */
  protected $provider;

  /**
   * The current OAuth access token.
   *
   * @var AccessToken
   */
  protected $oauthToken;

  /**
   * The user's email address, usually used as the login ID
   * and also the from address when sending email.
   *
   * @var string
   */
  protected $oauthUserEmail = '';

  /**
   * The client secret, generated in the app definition of the service you're connecting to.
   *
   * @var string
   */
  protected $oauthClientSecret = '';

  /**
   * The client ID, generated in the app definition of the service you're connecting to.
   *
   * @var string
   */
  protected $oauthClientId = '';

  /**
   * The refresh token, used to obtain new AccessTokens.
   *
   * @var string
   */
  protected $oauthRefreshToken = '';

  /**
   * OAuth constructor.
   *
   * @param array $options Associative array containing
   *                       `provider`, `userName`, `clientSecret`, `clientId` and `refreshToken` elements
   */
  public function __construct($options) 
   {
    $this->provider = $options ['provider'];
    $this->oauthUserEmail = $options ['userName'];
    $this->oauthClientSecret = $options ['clientSecret'];
    $this->oauthClientId = $options ['clientId'];
    $this->oauthRefreshToken = $options ['refreshToken'];
  }

  /**
   * Get a new RefreshToken.
   *
   * @return RefreshToken
   */
  protected function getGrant() 
   {
    return new RefreshToken();
  }

  /**
   * Get a new AccessToken.
   *
   * @return AccessToken
   */
  protected function getToken() 
   {
    return $this->provider->getAccessToken(
    $this->getGrant(), 
    ['refresh_token' => $this->oauthRefreshToken], 
    );
  }

  /**
   * Generate a base64-encoded OAuth token.
   *
   * @return string
   */
  public function getOauth64() 
   {
    // Get a new token if it's not available or has expired
    if (null === $this->oauthToken or $this->oauthToken->hasExpired()) {
      $this->oauthToken = $this->getToken();
    }

    return base64_encode(
    'user=' .
      $this->oauthUserEmail .
      "\001auth=Bearer " .
      $this->oauthToken .
      "\001\001"
      );
  }
}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
OAuth::$oauthClientId protected property The client ID, generated in the app definition of the service you're connecting to.
OAuth::$oauthClientSecret protected property The client secret, generated in the app definition of the service you're connecting to.
OAuth::$oauthRefreshToken protected property The refresh token, used to obtain new AccessTokens.
OAuth::$oauthToken protected property The current OAuth access token.
OAuth::$oauthUserEmail protected property The user's email address, usually used as the login ID and also the from address when sending email.
OAuth::$provider protected property An instance of the League OAuth Client Provider.
OAuth::getGrant protected function Get a new RefreshToken.
OAuth::getOauth64 public function Generate a base64-encoded OAuth token.
OAuth::getToken protected function Get a new AccessToken.
OAuth::__construct public function OAuth constructor.