PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/swiftMailer/lib/classes/Swift/Transport/Esmtp/Auth/CramMd5Authenticator.php

https://bitbucket.org/graaaf/erso
PHP | 98 lines | 44 code | 11 blank | 43 comment | 2 complexity | 0823decaab651feb3d4d49bfedfbcafa MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /*
  3. The AUTH PLAIN mechanism in Swift Mailer.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //@require 'Swift/Transport/Esmtp/Authenticator.php';
  16. //@require 'Swift/Transport/SmtpAgent.php';
  17. //@require 'Swift/TransportException.php';
  18. /**
  19. * Handles CRAM-MD5 authentication.
  20. * @package Swift
  21. * @subpackage Transport
  22. * @author Chris Corbyn
  23. */
  24. class Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  25. implements Swift_Transport_Esmtp_Authenticator
  26. {
  27. /**
  28. * Get the name of the AUTH mechanism this Authenticator handles.
  29. * @return string
  30. */
  31. public function getAuthKeyword()
  32. {
  33. return 'CRAM-MD5';
  34. }
  35. /**
  36. * Try to authenticate the user with $username and $password.
  37. * @param Swift_Transport_SmtpAgent $agent
  38. * @param string $username
  39. * @param string $password
  40. * @return boolean
  41. */
  42. public function authenticate(Swift_Transport_SmtpAgent $agent,
  43. $username, $password)
  44. {
  45. try
  46. {
  47. $challenge = $agent->executeCommand("AUTH CRAM-MD5\r\n", array(334));
  48. $challenge = base64_decode(substr($challenge, 4));
  49. $message = base64_encode(
  50. $username . ' ' . $this->_getResponse($password, $challenge)
  51. );
  52. $agent->executeCommand(sprintf("%s\r\n", $message), array(235));
  53. return true;
  54. }
  55. catch (Swift_TransportException $e)
  56. {
  57. $agent->executeCommand("RSET\r\n", array(250));
  58. return false;
  59. }
  60. }
  61. /**
  62. * Generate a CRAM-MD5 response from a server challenge.
  63. * @param string $secret
  64. * @param string $challenge
  65. * @return string
  66. */
  67. private function _getResponse($secret, $challenge)
  68. {
  69. if (strlen($secret) > 64)
  70. {
  71. $secret = pack('H32', md5($secret));
  72. }
  73. if (strlen($secret) < 64)
  74. {
  75. $secret = str_pad($secret, 64, chr(0));
  76. }
  77. $k_ipad = substr($secret, 0, 64) ^ str_repeat(chr(0x36), 64);
  78. $k_opad = substr($secret, 0, 64) ^ str_repeat(chr(0x5C), 64);
  79. $inner = pack('H32', md5($k_ipad . $challenge));
  80. $digest = md5($k_opad . $inner);
  81. return $digest;
  82. }
  83. }