1
0

BxSMTPMailAuthorizationPHPMailer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. use PHPMailer\PHPMailer\OAuthTokenProvider;
  3. /**
  4. * Mail Authorisation via oAuth2 for PHPMailer
  5. */
  6. class BxSMTPMailAuthorizationPHPMailer extends BxSMTPMailAuthorization implements OAuthTokenProvider {
  7. /**
  8. * Get singleton class instance
  9. *
  10. * Config for client secret login [clientId, clientSecret, tenantId, tenantName]
  11. * Config for certificate login [clientId, tenantId, tenantName, x509Cert, x509Key]
  12. *
  13. * @param object|array|null $config Configuration values
  14. * @return self
  15. */
  16. public static function getInstance(object|array|null $config): self{
  17. if(!self::$instance){
  18. self::$instance = new self();
  19. }
  20. return $config ? self::$instance->setConfig($config) : self::$instance;
  21. }
  22. /**
  23. * Generate a base64-encoded OAuth token ensuring that the access token has not expired.
  24. * The string to be base 64 encoded should be in the form:
  25. * "user=<user_email_address>\001auth=Bearer <access_token>\001\001"
  26. *
  27. * @return string
  28. */
  29. public function getOauth64(): string{
  30. $accessToken = $this->getAccessToken();
  31. if($this->hasErrors()){
  32. $a = $this->getErrors();
  33. foreach ($a as $s) {
  34. bx_log('bx_smtp_mailer_oauth', $s, BX_LOG_ERR);
  35. }
  36. return '';
  37. }
  38. return base64_encode(
  39. sprintf(
  40. "user=%s\001auth=Bearer %s\001\001",
  41. $this->mailAddress,
  42. $accessToken
  43. )
  44. );
  45. }
  46. /**
  47. * Load the access token and its expiration from custom caching storage.
  48. * This method should be overridden to meet the project- or framework-specific requirements and possibilities.
  49. * @param string $clientId
  50. * @return array|null $tokenData on success, null on failure
  51. */
  52. protected function getCache(string $clientId): array|null {
  53. $oCache = $this->getCacheObject();
  54. if ($oCache)
  55. return $oCache->getData($this->getCacheKey($clientId));
  56. return null;
  57. }
  58. /**
  59. * Save the access token and its expiration to the custom caching storage.
  60. * This method should be overridden to meet the project- or framework-specific requirements and possibilities.
  61. * @param string $clientId
  62. * @param array $tokenData
  63. * @return $this
  64. */
  65. protected function setCache(string $clientId, array $tokenData): self{
  66. $oCache = $this->getCacheObject();
  67. if ($oCache)
  68. $oCache->setData($this->getCacheKey($clientId, $tokenData), $tokenData);
  69. return $this;
  70. }
  71. protected function getCacheObject(): object{
  72. $sEngine = BxDolDb::getInstance()->getParam('sys_db_cache_engine');
  73. $oCache = bx_instance('BxDolCache'.$sEngine);
  74. if ($oCache->isAvailable())
  75. return $oCache;
  76. return null;
  77. }
  78. protected function getCacheKey(string $clientId): string{
  79. return 'bx_smtp_oauth_token_' . $clientId . bx_site_hash() . '.php';
  80. }
  81. }