cancel
Showing results for 
Search instead for 
Did you mean: 

Use login REST api not working in Alfresco4.2

magarcia_sm
Star Contributor
Star Contributor

Good Afternoon all

   We are trying to create a JS file from a third system to call Alfresco and obtain an authentication ticket. To do so we are using the webscript api and this is the JavaScript function we are building

function getTicket(login,password, numDoc, clave)
{


        var urlTicket="http://urlAlfresco/service/api/login?u="+login+"&pw="+password;
        
        
          var xmlHttp = new XMLHttpRequest();
          
          xmlHttp.open( "GET", urlTicket, false ); // false for synchronous request
          xmlHttp.setRequestHeader('Access-Control-Allow-Headers', '*');
          xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
          xmlHttp.onload = function()  {
            var requestData = xmlHttp.responseText;
            var ticket = "";
            if ((requestData!=='')&&(requestData!==NULL)){ticket = requestData.toString().substring(47,94);}
            if (ticket!=="")
                {
                    window.location.href = "http://urlAlfresco/service/showContent?nFact="+numDoc+"&cPro="+clave+"&alf_ticket="+ticket;
                }    
          }
          
          
         xmlHttp.onerror=function(){alert("Not working");};
         xmlHttp.send(null);
       

}

The problem is that when we try to make a call "request" GET, the function http://urlAlfresco/service/api/login is failing and is not giving us any response. (We have also tried the POST method but with the same wrong result)

We have readed in the forum that there seems to be a problem in Alfresco 4.2 with the Cors configuration. We have tried to apply the filters in the Cors configuration we found in the forums, but it is not working at all 😞

What it is strange is that if we introduce the url in the browser directly http://urlAlfresco/service/api/login?u=login&pw=password

the browser returns the ticket.... but we are not able to get it using a GET request call by JS

What we are trying to achieve is that a third system could be able (using a JS code) to make a call to the webscript login in Alfresto to obtain a ticket, and with that ticket call a custom webscript that we have created to show up the document we are looking for. However the question is, if it is not possible to use the method in the service  http://urlAlfresco/service/api/login, what other options do we have to achieve this in JS? Is this still possible in 4.2 as it was in 3.0?

Thanks a lot in advance.

3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator

I think  your error might cause by following line

 if ((requestData!=='')&&(requestData!==NULL)){ticket = requestData.toString().substring(47,94);}

You can try to change it to 

if ((requestData!=='')&&(requestData!= "undefined")){ticket = requestData.toString().substring(47,94);}

And substring start position is 48 in my env.

You 'd better use a javascript library to parse the returned xml instead of substring function ,for exmple jQuery.parseXML()

Thanks a lot for the answer Kayne

However I think the problem is that we are not able to reach the login api. When we make the request call we obtain this error (Alfresco 4.2) :

XMLHttpRequest cannot load http://ipserver:8080/alfresco/service/api/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

We are now using a code obtained from a Git project that seems it should work, but it is not for us. The code of the funcion we are using is:

 $.ajax({
    url: 'http://ipserver/alfresco/service/api/login',

    data: JSON.stringify({
      username: cmis.vm.username(),
      password: cmis.vm.password()
    }),

    contentType: 'application/json',
    type: 'POST',
    dataType: 'json',

    success: function (data) {
      cmis.vm.ticket(data.data.ticket);

      if (cmis.vm.rememberMe()) {
        store.set('username', cmis.vm.username());
        store.set('password', cmis.vm.password());
        store.set('rememberMe', cmis.vm.rememberMe());
        store.set('autoLogin', cmis.vm.autoLogin());
      } else {
        store.set('username', null);
        store.set('password', null);
        store.set('rememberMe', false);
        store.set('autoLogin', false);
      }

      // init
      $.getJSON('http://ipserver/ /alfresco/cmisbrowser', function (data) {
        cmis.vm.loggedIn(true);
        cmis.cb = data;
        for (var repo in cmis.cb) {
          cmis.repo = cmis.cb[repo];
        }
        if (parseFloat(cmis.repo.productVersion) < 4.2) {
          head.js(config.compat40, function () {
            cmis.loadRoot();
          });
        } else {
          cmis.loadRoot();
        }
      });

    }
  })

In other posts we have readed that we needed to include the CORS library: cors-filter-1.3.2.jar.

We have inclueded it in tomcat/lib in our Alfresco installation Alfresco and configured the web.xml including:

<filter>     <filter-name>springSecurityFilterChain</filter-name>     <filter-class>         org.springframework.web.filter.DelegatingFilterProxy     </filter-class> </filter>   <filter-mapping>     <filter-name>springSecurityFilterChain</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping>  <filter>     <filter-name>CORS</filter-name>     <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>      <init-param>         <param-name>cors.allowOrigin</param-name>         <param-value>*</param-value>     </init-param>     <init-param>         <param-name>cors.supportsCredentials</param-name>         <param-value>false</param-value>     </init-param>     <init-param>         <param-name>cors.supportedHeaders</param-name>         <param-value>
Accept, Origin, X-Requested-With, Content-Type, Last-Modified
</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, OPTIONS</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

But we keep getting the same error

We have also tried to include the jar file in tomcat/webapps/alfresco/web-inf/lib, but with this we are not able to start Alfresco, it just hungs up forever.

Do you know if a request call to this api work in Alfresco 4.2? I think it should be a pretty easy call, and dont know if we are spending too much time in a thing that should be eassy because we are figuring out too many things :-).

Please let me know your ipressions, I will really appreciate them!!

jpotts
World-Class Innovator
World-Class Innovator

SSH to the server and use curl to hit that API to try to get a ticket. That will rule out any CORS problems and will validate that you are hitting the API correctly and that your creds are working fine.

Also, make sure that you switch to HTTPS before you go to production, otherwise, your passwords will be in the clear.