login.ws.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. // External login module : WS (for Web Services)
  5. /**
  6. * This file is included in main/inc/local.inc.php at user login if the user
  7. * have 'ws' in his auth_source field instead of 'platform'.
  8. */
  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 __DIR__.'/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 = api_get_user_info_from_username($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::eventLogin($_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. *
  38. * @param string The username, as provided in form
  39. * @param string The cleartext password, as provided in form
  40. * @param string The WS URL, as provided at the beginning of this script
  41. */
  42. function loginWSAuthenticate($username, $password, $wsUrl)
  43. {
  44. // check params
  45. if (empty($username) || empty($password) || empty($wsUrl)) {
  46. return false;
  47. }
  48. // Create new SOAP client instance
  49. $client = new SoapClient($wsUrl);
  50. if (!$client) {
  51. return false;
  52. }
  53. // Include phpseclib methods, because of a bug with AES/CFB in mcrypt
  54. include_once api_get_path(LIBRARY_PATH).'phpseclib/Crypt/AES.php';
  55. // Define all elements necessary to the encryption
  56. $key = '-+*%$({[]})$%*+-';
  57. // Complete password con PKCS7-specific padding
  58. $blockSize = 16;
  59. $padding = $blockSize - (strlen($password) % $blockSize);
  60. $password .= str_repeat(chr($padding), $padding);
  61. $cipher = new Crypt_AES(CRYPT_AES_MODE_CFB);
  62. $cipher->setKeyLength(128);
  63. $cipher->setKey($key);
  64. $cipher->setIV($key);
  65. $cipheredPass = $cipher->encrypt($password);
  66. // Mcrypt call left for documentation purposes - broken, see https://bugs.php.net/bug.php?id=51146
  67. //$cipheredPass = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $password, MCRYPT_MODE_CFB, $key);
  68. // Following lines present for debug purposes only
  69. /*
  70. $arr = preg_split('//', $cipheredPass, -1, PREG_SPLIT_NO_EMPTY);
  71. foreach ($arr as $char) {
  72. error_log(ord($char));
  73. }
  74. */
  75. // Change to base64 to avoid communication alteration
  76. $passCrypted = base64_encode($cipheredPass);
  77. // The call to the webservice will change depending on your definition
  78. try {
  79. $response = $client->validateUser(
  80. [
  81. 'user' => $username,
  82. 'pass' => $passCrypted,
  83. 'system' => 'chamilo',
  84. ]
  85. );
  86. } catch (SoapFault $fault) {
  87. error_log('Caught something');
  88. if ($fault->faultstring != 'Could not connect to host') {
  89. error_log('Not a connection problem');
  90. throw $fault;
  91. } else {
  92. error_log('Could not connect to WS host');
  93. }
  94. return 0;
  95. }
  96. return $response->validateUserResult;
  97. }