login.ws.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php /* For licensing terms, see /license.txt */
  2. // External login module : WS (for Web Services)
  3. /**
  4. *
  5. * This file is included in main/inc/local.inc.php at user login if the user
  6. * have 'ws' in his auth_source field instead of 'platform'.
  7. */
  8. use ChamiloSession as Session;
  9. // Configure the web service URL here. e.g. http://174.1.1.19:8020/login.asmx?WSDL
  10. $wsUrl = '';
  11. // include common authentication functions
  12. require_once dirname(__FILE__) . '/functions.inc.php';
  13. // call the login checker (defined below)
  14. $isValid = loginWSAuthenticate($login, $password, $wsUrl);
  15. // if the authentication was successful, proceed
  16. if ($isValid === 1) {
  17. //error_log('WS authentication worked');
  18. $chamiloUser = UserManager::get_user_info($login);
  19. $loginFailed = false;
  20. $_user['user_id'] = $chamiloUser['user_id'];
  21. $_user['status'] = (isset($chamiloUser['status']) ? $chamiloUser['status'] : 5);
  22. $_user['uidReset'] = true;
  23. Session::write('_user', $_user);
  24. $uidReset = true;
  25. $logging_in = true;
  26. Event::event_login($_user['user_id']);
  27. } else {
  28. //error_log('WS authentication error - user not approved by external WS');
  29. $loginFailed = true;
  30. $uidReset = false;
  31. if (isset($_user) && isset($_user['user_id'])) {
  32. unset($_user['user_id']);
  33. }
  34. }
  35. /**
  36. * Checks whether a user has the right to enter on the platform or not
  37. * @param string The username, as provided in form
  38. * @param string The cleartext password, as provided in form
  39. * @param string The WS URL, as provided at the beginning of this script
  40. */
  41. function loginWSAuthenticate($username, $password, $wsUrl) {
  42. // check params
  43. if (empty($username) or empty($password) or empty($wsUrl)) {
  44. return false;
  45. }
  46. // Create new SOAP client instance
  47. $client = new SoapClient($wsUrl);
  48. if (!$client) {
  49. return false;
  50. }
  51. // Include phpseclib methods, because of a bug with AES/CFB in mcrypt
  52. include_once api_get_path(LIBRARY_PATH).'phpseclib/Crypt/AES.php';
  53. // Define all elements necessary to the encryption
  54. $key = '-+*%$({[]})$%*+-';
  55. // Complete password con PKCS7-specific padding
  56. $blockSize = 16;
  57. $padding = $blockSize - (strlen($password)%$blockSize);
  58. $password .= str_repeat(chr($padding),$padding);
  59. $cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
  60. $cipher->setKeyLength(128);
  61. $cipher->setKey($key);
  62. $cipher->setIV($key);
  63. $cipheredPass = $cipher->encrypt($password);
  64. // Mcrypt call left for documentation purposes - broken, see https://bugs.php.net/bug.php?id=51146
  65. //$cipheredPass = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $password, MCRYPT_MODE_CFB, $key);
  66. // Following lines present for debug purposes only
  67. /*
  68. $arr = preg_split('//', $cipheredPass, -1, PREG_SPLIT_NO_EMPTY);
  69. foreach ($arr as $char) {
  70. error_log(ord($char));
  71. }
  72. */
  73. // Change to base64 to avoid communication alteration
  74. $passCrypted = base64_encode($cipheredPass);
  75. // The call to the webservice will change depending on your definition
  76. try {
  77. $response = $client->validateUser(array('user' => $username, 'pass' => $passCrypted, 'system' => 'chamilo'));
  78. } catch (SoapFault $fault) {
  79. error_log('Caught something');
  80. if ($fault->faultstring != 'Could not connect to host') {
  81. error_log('Not a connection problem');
  82. throw $fault;
  83. } else {
  84. error_log('Could not connect to WS host');
  85. }
  86. return 0;
  87. }
  88. return $response->validateUserResult;
  89. }