OAuth.php 3.7 KB

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