|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laminas\Mail\Protocol\Smtp\Auth; |
| 4 | + |
| 5 | +use Laminas\Mail\Protocol\Smtp; |
| 6 | +use Laminas\Mail\Protocol\Xoauth2\Xoauth2 as Xoauth2AuthEncoder; |
| 7 | + |
| 8 | +use function array_replace_recursive; |
| 9 | +use function is_array; |
| 10 | + |
| 11 | +/** |
| 12 | + * Performs Xoauth2 authentication |
| 13 | + * |
| 14 | + * @psalm-suppress PropertyNotSetInConstructor |
| 15 | + */ |
| 16 | +final class Xoauth2 extends Smtp |
| 17 | +{ |
| 18 | + /** |
| 19 | + * SMTP username |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $username; |
| 24 | + |
| 25 | + /** |
| 26 | + * Xoauth2 access token |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $accessToken; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param string|array $host (Default: 127.0.0.1) |
| 34 | + * @param int|null $port (Default: null) |
| 35 | + * @param array|null $config Auth-specific parameters |
| 36 | + */ |
| 37 | + public function __construct($host = '127.0.0.1', $port = null, ?array $config = null) |
| 38 | + { |
| 39 | + // Did we receive a configuration array? |
| 40 | + $origConfig = $config; |
| 41 | + if (is_array($host)) { |
| 42 | + // Merge config array with principal array, if provided |
| 43 | + if (is_array($config)) { |
| 44 | + $config = array_replace_recursive($host, $config); |
| 45 | + } else { |
| 46 | + $config = $host; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (is_array($config)) { |
| 51 | + if (isset($config['username'])) { |
| 52 | + $this->setUsername((string) $config['username']); |
| 53 | + } |
| 54 | + if (isset($config['access_token'])) { |
| 55 | + $this->setAccessToken((string) $config['access_token']); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Call parent with original arguments |
| 60 | + parent::__construct($host, $port, $origConfig); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Perform XOAUTH2 authentication with supplied credentials |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function auth() |
| 69 | + { |
| 70 | + // Ensure AUTH has not already been initiated. |
| 71 | + parent::auth(); |
| 72 | + |
| 73 | + $this->_send('AUTH XOAUTH2'); |
| 74 | + $this->_expect('334'); |
| 75 | + $this->_send(Xoauth2AuthEncoder::encodeXoauth2Sasl($this->getUsername(), $this->getAccessToken())); |
| 76 | + $this->_expect('235'); |
| 77 | + $this->auth = true; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set value for username |
| 82 | + * |
| 83 | + * @param string $username |
| 84 | + * @return Xoauth2 |
| 85 | + */ |
| 86 | + public function setUsername($username) |
| 87 | + { |
| 88 | + $this->username = $username; |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get username |
| 94 | + * |
| 95 | + * @return string |
| 96 | + */ |
| 97 | + public function getUsername() |
| 98 | + { |
| 99 | + return $this->username; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Set value for access token |
| 104 | + * |
| 105 | + * @param string $token |
| 106 | + * @return Xoauth2 |
| 107 | + */ |
| 108 | + public function setAccessToken($token) |
| 109 | + { |
| 110 | + $this->accessToken = $token; |
| 111 | + return $this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get access token |
| 116 | + * |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + public function getAccessToken() |
| 120 | + { |
| 121 | + return $this->accessToken; |
| 122 | + } |
| 123 | +} |
0 commit comments