AuthHandler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * An ESMTP handler for AUTH support (RFC 5248).
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Transport_Esmtp_AuthHandler implements Swift_Transport_EsmtpHandler
  15. {
  16. /**
  17. * Authenticators available to process the request.
  18. *
  19. * @var Swift_Transport_Esmtp_Authenticator[]
  20. */
  21. private $authenticators = [];
  22. /**
  23. * The username for authentication.
  24. *
  25. * @var string
  26. */
  27. private $username;
  28. /**
  29. * The password for authentication.
  30. *
  31. * @var string
  32. */
  33. private $password;
  34. /**
  35. * The auth mode for authentication.
  36. *
  37. * @var string
  38. */
  39. private $auth_mode;
  40. /**
  41. * The ESMTP AUTH parameters available.
  42. *
  43. * @var string[]
  44. */
  45. private $esmtpParams = [];
  46. /**
  47. * Create a new AuthHandler with $authenticators for support.
  48. *
  49. * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
  50. */
  51. public function __construct(array $authenticators)
  52. {
  53. $this->setAuthenticators($authenticators);
  54. }
  55. /**
  56. * Set the Authenticators which can process a login request.
  57. *
  58. * @param Swift_Transport_Esmtp_Authenticator[] $authenticators
  59. */
  60. public function setAuthenticators(array $authenticators)
  61. {
  62. $this->authenticators = $authenticators;
  63. }
  64. /**
  65. * Get the Authenticators which can process a login request.
  66. *
  67. * @return Swift_Transport_Esmtp_Authenticator[]
  68. */
  69. public function getAuthenticators()
  70. {
  71. return $this->authenticators;
  72. }
  73. /**
  74. * Set the username to authenticate with.
  75. *
  76. * @param string $username
  77. */
  78. public function setUsername($username)
  79. {
  80. $this->username = $username;
  81. }
  82. /**
  83. * Get the username to authenticate with.
  84. *
  85. * @return string
  86. */
  87. public function getUsername()
  88. {
  89. return $this->username;
  90. }
  91. /**
  92. * Set the password to authenticate with.
  93. *
  94. * @param string $password
  95. */
  96. public function setPassword($password)
  97. {
  98. $this->password = $password;
  99. }
  100. /**
  101. * Get the password to authenticate with.
  102. *
  103. * @return string
  104. */
  105. public function getPassword()
  106. {
  107. return $this->password;
  108. }
  109. /**
  110. * Set the auth mode to use to authenticate.
  111. *
  112. * @param string $mode
  113. */
  114. public function setAuthMode($mode)
  115. {
  116. $this->auth_mode = $mode;
  117. }
  118. /**
  119. * Get the auth mode to use to authenticate.
  120. *
  121. * @return string
  122. */
  123. public function getAuthMode()
  124. {
  125. return $this->auth_mode;
  126. }
  127. /**
  128. * Get the name of the ESMTP extension this handles.
  129. *
  130. * @return string
  131. */
  132. public function getHandledKeyword()
  133. {
  134. return 'AUTH';
  135. }
  136. /**
  137. * Set the parameters which the EHLO greeting indicated.
  138. *
  139. * @param string[] $parameters
  140. */
  141. public function setKeywordParams(array $parameters)
  142. {
  143. $this->esmtpParams = $parameters;
  144. }
  145. /**
  146. * Runs immediately after a EHLO has been issued.
  147. *
  148. * @param Swift_Transport_SmtpAgent $agent to read/write
  149. */
  150. public function afterEhlo(Swift_Transport_SmtpAgent $agent)
  151. {
  152. if ($this->username) {
  153. $count = 0;
  154. $errors = [];
  155. foreach ($this->getAuthenticatorsForAgent() as $authenticator) {
  156. if (\in_array(strtolower($authenticator->getAuthKeyword() ?? ''), array_map('strtolower', $this->esmtpParams))) {
  157. ++$count;
  158. try {
  159. if ($authenticator->authenticate($agent, $this->username, $this->password)) {
  160. return;
  161. }
  162. } catch (Swift_TransportException $e) {
  163. // keep the error message, but tries the other authenticators
  164. $errors[] = [$authenticator->getAuthKeyword(), $e->getMessage()];
  165. }
  166. }
  167. }
  168. $message = 'Failed to authenticate on SMTP server with username "'.$this->username.'" using '.$count.' possible authenticators.';
  169. foreach ($errors as $error) {
  170. $message .= ' Authenticator '.$error[0].' returned '.$error[1].'.';
  171. }
  172. throw new Swift_TransportException($message);
  173. }
  174. }
  175. /**
  176. * Not used.
  177. */
  178. public function getMailParams()
  179. {
  180. return [];
  181. }
  182. /**
  183. * Not used.
  184. */
  185. public function getRcptParams()
  186. {
  187. return [];
  188. }
  189. /**
  190. * Not used.
  191. */
  192. public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false)
  193. {
  194. }
  195. /**
  196. * Returns +1, -1 or 0 according to the rules for usort().
  197. *
  198. * This method is called to ensure extensions can be execute in an appropriate order.
  199. *
  200. * @param string $esmtpKeyword to compare with
  201. *
  202. * @return int
  203. */
  204. public function getPriorityOver($esmtpKeyword)
  205. {
  206. return 0;
  207. }
  208. /**
  209. * Returns an array of method names which are exposed to the Esmtp class.
  210. *
  211. * @return string[]
  212. */
  213. public function exposeMixinMethods()
  214. {
  215. return ['setUsername', 'getUsername', 'setPassword', 'getPassword', 'setAuthMode', 'getAuthMode'];
  216. }
  217. /**
  218. * Not used.
  219. */
  220. public function resetState()
  221. {
  222. }
  223. /**
  224. * Returns the authenticator list for the given agent.
  225. *
  226. * @return array
  227. */
  228. protected function getAuthenticatorsForAgent()
  229. {
  230. if (!$mode = strtolower($this->auth_mode ?? '')) {
  231. return $this->authenticators;
  232. }
  233. foreach ($this->authenticators as $authenticator) {
  234. if (strtolower($authenticator->getAuthKeyword() ?? '') == $mode) {
  235. return [$authenticator];
  236. }
  237. }
  238. throw new Swift_TransportException('Auth mode '.$mode.' is invalid');
  239. }
  240. }