title: Auto login Rocket.Chat with LDAP
body: <p>I am integrating Rocket.Chat into my system that share user account through LDAP database. We created a shortcut to go Rocket.Chat from our system, when user click to this shortcut, our system will open Rocket.Chat page with url type: <code>http://rocketchat.host:3000/?username={username}&amp;password={password}</code> with <code>username</code> and <code>password</code> is current account.</p>
  <p>We changed something on compiled bundle of Rocket.Chat:</p>
  <pre><code>// Changed file: {bundle}\programs\web.browser\head.html
  
  &lt;title&gt;Rocket.Chat&lt;/title&gt;
  &lt;meta charset=&quot;utf-8&quot; /&gt;
  &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
  &lt;meta http-equiv=&quot;expires&quot; content=&quot;-1&quot; /&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;fragment&quot; content=&quot;!&quot; /&gt;
  &lt;meta name=&quot;distribution&quot; content=&quot;global&quot; /&gt;
  &lt;meta name=&quot;rating&quot; content=&quot;general&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no&quot; /&gt;
  &lt;meta name=&quot;msapplication-TileColor&quot; content=&quot;#04436a&quot;&gt;
  &lt;meta name=&quot;msapplication-TileImage&quot; content=&quot;images/logo/mstile-144x144.png?v=3&quot;&gt;
  &lt;meta name=&quot;msapplication-config&quot; content=&quot;images/logo/browserconfig.xml?v=3&quot;&gt;
  &lt;meta name=&quot;theme-color&quot; content=&quot;#04436a&quot;&gt;
  &lt;link rel=&quot;manifest&quot; href=&quot;images/logo/manifest.json?v=3&quot;&gt;
  &lt;link rel=&quot;chrome-webstore-item&quot; href=&quot;https://chrome.google.com/webstore/detail/nocfbnnmjnndkbipkabodnheejiegccf&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;any&quot; type=&quot;image/svg+xml&quot; href=&quot;assets/favicon.svg?v=3&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;256x256&quot; type=&quot;image/png&quot; href=&quot;assets/favicon_256.png?v=3&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;192x192&quot; type=&quot;image/png&quot; href=&quot;assets/favicon_192.png?v=3&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;128x128&quot; type=&quot;image/png&quot; href=&quot;assets/favicon_128.png?v=3&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;96x96&quot; type=&quot;image/png&quot; href=&quot;assets/favicon_96.png?v=3&quot;&gt;
  &lt;link rel=&quot;icon&quot; sizes=&quot;64x64&quot; type=&quot;image/png&quot; href=&quot;assets/favicon_64.png?v=3&quot;&gt;
  &lt;link rel=&quot;shortcut icon&quot; sizes=&quot;16x16 32x32 48x48&quot; type=&quot;image/x-icon&quot; href=&quot;assets/favicon_ico.ico?v=3&quot; /&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;57x57&quot; href=&quot;images/logo/apple-touch-icon-57x57.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;60x60&quot; href=&quot;images/logo/apple-touch-icon-60x60.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;72x72&quot; href=&quot;images/logo/apple-touch-icon-72x72.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;76x76&quot; href=&quot;images/logo/apple-touch-icon-76x76.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;114x114&quot; href=&quot;images/logo/apple-touch-icon-114x114.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;120x120&quot; href=&quot;images/logo/apple-touch-icon-120x120.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;144x144&quot; href=&quot;images/logo/apple-touch-icon-144x144.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;152x152&quot; href=&quot;images/logo/apple-touch-icon-152x152.png?v=3&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;180x180&quot; href=&quot;images/logo/apple-touch-icon-180x180.png?v=3&quot;&gt;
  &lt;script type=&quot;text/javascript&quot;&gt;
      // alert(&quot;test js&quot;);
  &lt;/script&gt;
  
  &lt;script type=&quot;text/javascript&quot;&gt;
      function getURLParameter(name) {
          console.log(&quot;location.search: &quot; + location.search);
          var result = decodeURIComponent((new RegExp('[?|&amp;]' + name + '=' + '([^&amp;;]+?)(&amp;|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
          console.log(&quot;getURLParameter, &quot; + name + &quot;: &quot; + result);
          return result;
      }
      var username = getURLParameter('username'),
          password = getURLParameter('password'); // Query parameter from url that our system passed
      
  
      Meteor.loginWithPassword(username, password, function () {
          console.log('loginWithPassword callback, username: ' + username + &quot;; password:&quot; + password);    
      }); // Call login direct to Meteor.login
  
      // Query &quot;username&quot; and &quot;password&quot; input fields from login from then pass data and simulate click login button
      document.addEventListener(&quot;DOMContentLoaded&quot;, function(event) { 
          document.getElementById('username').value = username;
          document.getElementById('password').value = password;
          document.getElementById('loginButton').click();
      });
  &lt;/script&gt;
  </code></pre>
  <p>We also changed in minified javascript file of Rocket.Chat at <code>{bundle}\programs\web.browser\{sso number}.js</code> where <code>sso number</code> is randomize number that build tool generate :</p>
  <p>Original:</p>
  <pre><code>...
  function(){o.loginLayout.onRendered(function(){$(&quot;#initial-page-loading&quot;).remove()})}.call(this)
  ...
  </code></pre>
  <p>To:</p>
  <pre><code>...
  function(){o.loginLayout.onRendered(function(){function e(e){return decodeURIComponent((new RegExp(&quot;[?|&amp;]&quot;+e+&quot;=([^&amp;;]+?)(&amp;|#|;|$)&quot;).exec(location.search)||[null,&quot;&quot;])[1].replace(/\+/g,&quot;%20&quot;))||null}$(&quot;#initial-pageloading&quot;).remove();varn=e(&quot;username&quot;),t=e(&quot;password&quot;);console.log(&quot;username,password=&quot;+n+&quot;,&quot;+t),console.log(&quot;getElementById(username)=&quot;+$(&quot;input[name=emailOrUsername]&quot;).val()),&quot;null&quot;!=n&amp;&amp;&quot;null&quot;!=t&amp;&amp;($(&quot;input[name=emailOrUsername]&quot;).val(n),$(&quot;input[name=pass]&quot;).val(t),$(&quot;.login&quot;)[0].click())})}.call(this)
  ...
  </code></pre>
  <p>It corresponds to the following code at file &quot;{source code}\packages\rocketchat-ui-login\login\layout.js&quot; on Rocket.Chat source code:</p>
  <pre><code>Template.loginLayout.onRendered(function() {
  $('#initial-page-loading').remove();
  
  function getURLParameter(name) {
      return decodeURIComponent((new RegExp('[?|&amp;]' + name + '=' + '([^&amp;;]+?)(&amp;|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
  }
  var username = getURLParameter('username'),
      password = getURLParameter('password');
  
  console.log(&quot;username,password=&quot;+username+&quot;,&quot;+password);
  console.log(&quot;getElementById(username)=&quot;+$('input[name=emailOrUsername]').val());
  
  if (username != 'null' &amp;&amp; password != 'null') {
      $('input[name=emailOrUsername]').val(username);
      $('input[name=pass]').val(password);
      $('.login')[0].click();
  }
  });
  </code></pre>
  <p>With account has been previously logged in by Rocket.Chat login form (<strong>case 1</strong>), it work ok. But if account hasn't been yet (<strong>case 2</strong>), it fail.</p>
  <p><strong>Case 1</strong> : This Chrome log:</p>
  <p><a href="https://i.stack.imgur.com/4YA0E.png" rel="nofollow noreferrer">Case 1 log</a></p>
  <p><strong>Case 2</strong> : Chrome log:</p>
  <p><a href="https://i.stack.imgur.com/UtrXg.png" rel="nofollow noreferrer">Case 2 log</a></p>
  <p>I know Rocket.Chat has issue with LDAP is with first login by <code>Meteor.loginWithPassword()</code> api, it won't work then I simulated login UI. And I know these logs mean &quot;login form&quot; wasn't found. My question is why my login UI simulation not work? How can I fix it?</p>