[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Net_IMSP_Auth_cram_md5 class for IMSP authentication. 4 * 5 * Required parameters:<pre> 6 * 'username' Username to logon to IMSP server as. 7 * 'password' Password for current user. 8 * 'server' The hostname of the IMSP server. 9 * 'port' The port of the IMSP server.</pre> 10 * 11 * $Horde: framework/Net_IMSP/IMSP/Auth/cram_md5.php,v 1.6.10.9 2006/01/01 21:28:28 jan Exp $ 12 * 13 * Copyright 2003-2006 Michael Rubinsky <mrubinsk@horde.org> 14 * 15 * See the enclosed file COPYING for license information (LGPL). If you 16 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 17 * 18 * @author Michael Rubinsky <mrubinsk@horde.org> 19 * @package Net_IMSP 20 */ 21 class Net_IMSP_Auth_cram_md5 extends Net_IMSP_Auth { 22 23 /** 24 * Private authentication function. Provides actual 25 * authentication code. 26 * 27 * @access private 28 * @param mixed $params Hash of IMSP parameters. 29 * 30 * @return mixed Net_IMSP object connected to server if successful, 31 * PEAR_Error on failure. 32 */ 33 function &_authenticate($params) 34 { 35 $imsp = &Net_IMSP::singleton('none', $params); 36 $result = $imsp->init(); 37 if (is_a($result, 'PEAR_Error')) { 38 return $result; 39 } 40 41 $userId = $params['username']; 42 $credentials = $params['password']; 43 $result = $imsp->imspSend('AUTHENTICATE CRAM-MD5'); 44 if (is_a($result, 'PEAR_Error')) { 45 return $result; 46 } 47 48 /* Get response and decode it. Note that we remove the 1st 2 49 * characters from the response to get rid of the '+' 50 * continuation character and the space that is sent as part 51 * of the CRAM-MD5 response (at least on cyrus-imspd). */ 52 $server_response = $imsp->imspReceive(); 53 if (is_a($server_response, 'PEAR_Error')) { 54 return $server_response; 55 } 56 57 $server_response = base64_decode(trim(substr($server_response, 2))); 58 59 /* Build and base64 encode the response to the challange. */ 60 $response_to_send = $userId . ' ' . $this->_hmac($credentials, $server_response); 61 $command_string = base64_encode($response_to_send); 62 63 /* Send the response. */ 64 $result = $imsp->imspSend($command_string, false); 65 if (is_a($result, 'PEAR_Error')) { 66 return $result; 67 } 68 69 $result = $imsp->imspReceive(); 70 if (is_a($result, 'PEAR_Error')) { 71 return $result; 72 } 73 74 if ($result != 'OK') { 75 return $imsp->imspError('Login to IMSP host failed.', __FILE__, __LINE__); 76 } else { 77 return $imsp; 78 } 79 } 80 81 /** 82 * RFC 2104 HMAC implementation. Eliminates the reliance on mhash. 83 * 84 * @access private 85 * @param string $key The HMAC key. 86 * @param string $data The data to hash with the key. 87 * 88 * @return string The MD5 HMAC. 89 */ 90 function _hmac($key, $data) 91 { 92 /* Byte length for md5. */ 93 $b = 64; 94 95 if (strlen($key) > $b) { 96 $key = pack('H*', md5($key)); 97 } 98 99 $key = str_pad($key, $b, chr(0x00)); 100 $ipad = str_pad('', $b, chr(0x36)); 101 $opad = str_pad('', $b, chr(0x5c)); 102 $k_ipad = $key ^ $ipad; 103 $k_opad = $key ^ $opad; 104 return md5($k_opad . pack('H*', md5($k_ipad . $data))); 105 } 106 107 /** 108 * Force a logout command to the imsp stream. 109 * 110 */ 111 function logout() 112 { 113 $this->_imsp->logout(); 114 } 115 116 /** 117 * Return the driver type 118 * 119 * @return string the type of this IMSP_Auth driver 120 */ 121 function getDriverType() 122 { 123 return 'cram_md5'; 124 } 125 126 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |