OAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * PHPMailer - PHP email creation and transport class.
  4. * PHP Version 5.5
  5. * @package PHPMailer
  6. * @link https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
  7. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  8. * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  9. * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  10. * @author Brent R. Matzelle (original founder)
  11. * @copyright 2012 - 2015 Marcus Bointon
  12. * @copyright 2010 - 2012 Jim Jagielski
  13. * @copyright 2004 - 2009 Andy Prevost
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  15. * @note This program is distributed in the hope that it will be useful - WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. namespace PHPMailer\PHPMailer;
  20. /**
  21. * OAuth - OAuth2 authentication wrapper class.
  22. * Uses the oauth2-client package from the League of Extraordinary Packages
  23. * @link http://oauth2-client.thephpleague.com
  24. * @package PHPMailer
  25. * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
  26. */
  27. class OAuth
  28. {
  29. /**
  30. * An instance of the League OAuth Client Provider.
  31. * @var League\OAuth2\Client\Provider\AbstractProvider
  32. */
  33. protected $provider = null;
  34. /**
  35. * The current OAuth access token.
  36. * @var League\OAuth2\Client\Token\AccessToken
  37. */
  38. protected $oauthToken = null;
  39. /**
  40. * The user's email address, usually used as the login ID
  41. * and also the from address when sending email.
  42. * @var string
  43. */
  44. protected $oauthUserEmail = '';
  45. /**
  46. * The client secret, generated in the app definition of the service you're connecting to.
  47. * @var string
  48. */
  49. protected $oauthClientSecret = '';
  50. /**
  51. * The client ID, generated in the app definition of the service you're connecting to.
  52. * @var string
  53. */
  54. protected $oauthClientId = '';
  55. /**
  56. * The refresh token, used to obtain new AccessTokens.
  57. * @var string
  58. */
  59. protected $oauthRefreshToken = '';
  60. /**
  61. * OAuth constructor.
  62. * @param array $options Associative array containing
  63. * `provider`, `userName`, `clientSecret`, `clientId` and `refreshToken` elements
  64. */
  65. public function __construct($options)
  66. {
  67. $this->provider = $options['provider'];
  68. $this->oauthUserEmail = $options['userName'];
  69. $this->oauthClientSecret = $options['clientSecret'];
  70. $this->oauthClientId = $options['clientId'];
  71. $this->oauthRefreshToken = $options['refreshToken'];
  72. }
  73. /**
  74. * Get a new RefreshToken.
  75. * @return \League\OAuth2\Client\Grant\RefreshToken
  76. */
  77. protected function getGrant()
  78. {
  79. return new \League\OAuth2\Client\Grant\RefreshToken;
  80. }
  81. /**
  82. * Get a new AccessToken.
  83. * @return League\OAuth2\Client\Token\AccessToken
  84. */
  85. protected function getToken()
  86. {
  87. return $this->provider->getAccessToken(
  88. $this->getGrant(),
  89. ['refresh_token' => $this->oauthRefreshToken]
  90. );
  91. }
  92. /**
  93. * Generate a base64-encoded OAuth token.
  94. * @return string
  95. */
  96. public function getOauth64()
  97. {
  98. // Get a new token if it's not available or has expired
  99. if (is_null($this->oauthToken) or $this->oauthToken->hasExpired()) {
  100. $this->oauthToken = $this->getToken();
  101. }
  102. return base64_encode(
  103. 'user=' .
  104. $this->oauthUserEmail .
  105. "\001auth=Bearer " .
  106. $this->oauthToken .
  107. "\001\001"
  108. );
  109. }
  110. }