Complex classes like Swift_Transport_Esmtp_Auth_NTLMAuthenticator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Swift_Transport_Esmtp_Auth_NTLMAuthenticator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Swift_Transport_Esmtp_Auth_NTLMAuthenticator implements Swift_Transport_Esmtp_Authenticator |
||
|
|
|||
| 19 | { |
||
| 20 | const NTLMSIG = "NTLMSSP\x00"; |
||
| 21 | const DESCONST = 'KGS!@#$%'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Get the name of the AUTH mechanism this Authenticator handles. |
||
| 25 | * |
||
| 26 | * @return string |
||
| 27 | */ |
||
| 28 | 3 | public function getAuthKeyword() |
|
| 29 | 1 | { |
|
| 30 | 3 | return 'NTLM'; |
|
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Try to authenticate the user with $username and $password. |
||
| 35 | * |
||
| 36 | * @param Swift_Transport_SmtpAgent $agent |
||
| 37 | * @param string $username |
||
| 38 | * @param string $password |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | 2 | public function authenticate(Swift_Transport_SmtpAgent $agent, $username, $password) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $si |
||
| 74 | * @param int $bits |
||
| 75 | * |
||
| 76 | * @return null|string |
||
| 77 | */ |
||
| 78 | protected function si2bin($si, $bits = 32) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Send our auth message and returns the response. |
||
| 106 | * |
||
| 107 | * @param Swift_Transport_SmtpAgent $agent |
||
| 108 | * |
||
| 109 | * @return string SMTP Response |
||
| 110 | */ |
||
| 111 | 2 | protected function sendMessage1(Swift_Transport_SmtpAgent $agent) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Fetch all details of our response (message 2). |
||
| 120 | * |
||
| 121 | * @param string $response |
||
| 122 | * |
||
| 123 | * @return array our response parsed |
||
| 124 | */ |
||
| 125 | 1 | protected function parseMessage2($response) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Read the blob information in from message2. |
||
| 154 | * |
||
| 155 | * @param string $block |
||
| 156 | * |
||
| 157 | * @return string[] |
||
| 158 | */ |
||
| 159 | 1 | protected function readSubBlock($block) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Send our final message with all our data. |
||
| 186 | * |
||
| 187 | * @param string $response Message 1 response (message 2) |
||
| 188 | * @param string $username |
||
| 189 | * @param string $password |
||
| 190 | * @param string $timestamp |
||
| 191 | * @param string $client |
||
| 192 | * @param Swift_Transport_SmtpAgent $agent |
||
| 193 | * @param bool $v2 Use version2 of the protocol |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 1 | protected function sendMessage3($response, $username, $password, $timestamp, $client, Swift_Transport_SmtpAgent $agent, $v2 = true) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Create our message 1. |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | 3 | protected function createMessage1() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Create our message 3. |
||
| 234 | * |
||
| 235 | * @param string $domain |
||
| 236 | * @param string $username |
||
| 237 | * @param string $workstation |
||
| 238 | * @param string $lmResponse |
||
| 239 | * @param string $ntlmResponse |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 3 | protected function createMessage3($domain, $username, $workstation, $lmResponse, $ntlmResponse) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $timestamp Epoch timestamp in microseconds |
||
| 274 | * @param string $client Random bytes |
||
| 275 | * @param string $targetInfo |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | 1 | protected function createBlob($timestamp, $client, $targetInfo) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get domain and username from our username. |
||
| 292 | * |
||
| 293 | * @example DOMAIN\username |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 6 | protected function getDomainAndUsername($name) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Create LMv1 response. |
||
| 317 | * |
||
| 318 | * @param string $password |
||
| 319 | * @param string $challenge |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | 1 | protected function createLMPassword($password, $challenge) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Create NTLMv1 response. |
||
| 346 | * |
||
| 347 | * @param string $password |
||
| 348 | * @param string $challenge |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function createNTLMPassword($password, $challenge) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Convert a normal timestamp to a tenth of a microtime epoch time. |
||
| 367 | * |
||
| 368 | * @param string $time |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | protected function getCorrectTimestamp($time) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Create LMv2 response. |
||
| 390 | * |
||
| 391 | * @param string $password |
||
| 392 | * @param string $username |
||
| 393 | * @param string $domain |
||
| 394 | * @param string $challenge NTLM Challenge |
||
| 395 | * @param string $client Random string |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 2 | protected function createLMv2Password($password, $username, $domain, $challenge, $client) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Create NTLMv2 response. |
||
| 415 | * |
||
| 416 | * @param string $password |
||
| 417 | * @param string $username |
||
| 418 | * @param string $domain |
||
| 419 | * @param string $challenge Hex values |
||
| 420 | * @param string $targetInfo Hex values |
||
| 421 | * @param string $timestamp |
||
| 422 | * @param string $client Random bytes |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | * |
||
| 426 | * @see http://davenport.sourceforge.net/ntlm.html#theNtlmResponse |
||
| 427 | */ |
||
| 428 | 1 | protected function createNTLMv2Hash($password, $username, $domain, $challenge, $targetInfo, $timestamp, $client) |
|
| 440 | |||
| 441 | 1 | protected function createDesKey($key) |
|
| 478 | |||
| 479 | /** HELPER FUNCTIONS */ |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Create our security buffer depending on length and offset. |
||
| 483 | * |
||
| 484 | * @param string $value Value we want to put in |
||
| 485 | * @param int $offset start of value |
||
| 486 | * @param bool $is16 Do we 16bit string or not? |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 3 | protected function createSecurityBuffer($value, $offset, $is16 = false) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Read our security buffer to fetch length and offset of our value. |
||
| 501 | * |
||
| 502 | * @param string $value Securitybuffer in hex |
||
| 503 | * |
||
| 504 | * @return double[] array with length and offset |
||
| 505 | */ |
||
| 506 | 3 | protected function readSecurityBuffer($value) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Cast to byte java equivalent to (byte). |
||
| 516 | * |
||
| 517 | * @param int $v |
||
| 518 | * |
||
| 519 | * @return int |
||
| 520 | */ |
||
| 521 | 1 | protected function castToByte($v) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Java unsigned right bitwise |
||
| 528 | * $a >>> $b. |
||
| 529 | * |
||
| 530 | * @param int $a |
||
| 531 | * @param int $b |
||
| 532 | * |
||
| 533 | * @return int |
||
| 534 | */ |
||
| 535 | 1 | protected function uRShift($a, $b) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Right padding with 0 to certain length. |
||
| 546 | * |
||
| 547 | * @param string $input |
||
| 548 | * @param int $bytes Length of bytes |
||
| 549 | * @param bool $isHex Did we provided hex value |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | 7 | protected function createByte($input, $bytes = 4, $isHex = true) |
|
| 563 | |||
| 564 | /** ENCRYPTION ALGORITHMS */ |
||
| 565 | |||
| 566 | /** |
||
| 567 | * DES Encryption. |
||
| 568 | * |
||
| 569 | * @param string $value An 8-byte string |
||
| 570 | * @param string $key |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | 1 | protected function desEncrypt($value, $key) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * MD5 Encryption. |
||
| 582 | * |
||
| 583 | * @param string $key Encryption key |
||
| 584 | * @param string $msg Message to encrypt |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | 2 | protected function md5Encrypt($key, $msg) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * MD4 Encryption. |
||
| 604 | * |
||
| 605 | * @param string $input |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | * |
||
| 609 | * @see http://php.net/manual/en/ref.hash.php |
||
| 610 | */ |
||
| 611 | 2 | protected function md4Encrypt($input) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Convert UTF-8 to UTF-16. |
||
| 620 | * |
||
| 621 | * @param string $input |
||
| 622 | * |
||
| 623 | * @return string |
||
| 624 | */ |
||
| 625 | 4 | protected function convertTo16bit($input) |
|
| 629 | |||
| 630 | /** |
||
| 631 | * @param string $message |
||
| 632 | */ |
||
| 633 | protected function debug($message) |
||
| 706 | } |
||
| 707 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.