cancel
Showing results for 
Search instead for 
Did you mean: 

Content Repository and Basic Authentication

vincent_os
Champ in-the-making
Champ in-the-making
Folks,

I am trying to remotely retrieve content from an alfresco repository using a URL to the content in question.

The URL is as follows:
http://127.0.0.1:8080/alfresco/d/d/workspace/SpacesStore/57000863-5dbd-4153-9489-36f007a457d4/asampl...

The client application is attempting to retrieve a PDF file from alfresco using a HTPP request that includes basic authentication:


      String name = "admin";
      String password = "admin";
   
      String authString = name + ":" + password;
      byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
      String authStringEnc = new String(authEncBytes);

      try {
         URL url = new URL(aPdfURL);
         URLConnection urlConnection = url.openConnection();
         urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        
         is = urlConnection.getInputStream();
         byte[] byteChunk = new byte[READ_CHUNK_SIZE];
         int n;

         while ((n = is.read(byteChunk)) > 0) {
            bais.write(byteChunk, 0, n);
         }

         pdfAsByteArray = bais.toByteArray();
      } catch (Throwable t) {
         logger.warn("Failed to retrieve PDF for URL " + aPdfURL, t);
         return pdfAsByteArray;
      }


The Alfresco server does not seem to recognise/accept the basic authentication in the http request, and redirects the request to the login screen.

Using curl I can see the following interaction:


C:\temp>curl -v -uadmin:admin http://127.0.0.1:8080/alfresco/d/d/workspace/SpacesStore/57000863-5dbd-4153-9489-36f007a457d4/sample...
* About to connect() to 127.0.0.1 port 8080 (#0)
*   Trying 127.0.0.1…
* Adding handle: conn: 0x63adc0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x63adc0) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
* Server auth using Basic with user 'admin'
> GET /alfresco/d/d/workspace/SpacesStore/57000863-5dbd-4153-9489-36f007a457d4/sampleDoc.pdf HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: curl/7.32.0
> Host: 127.0.0.1:8080
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=2AB897F5161270BB2BA923CEDFE1C7BD; Path=/alfresco
< Location: http://127.0.0.1:8080/alfresco/faces/jsp/login.jsp?_alfRedirect=%2Falfresco%2Fd%2Fd%2Fworkspace%2FSp...
< Content-Length: 0
< Date: Mon, 09 Dec 2013 14:23:30 GMT
<
* Connection #0 to host 127.0.0.1 left intact


Is what I am attempting to do here possible? or am I missing a trick? Does the authentication service prevent basic auth for interactions of this nature?





2 REPLIES 2

vincent_os
Champ in-the-making
Champ in-the-making
What I am trying to do here is authenticate and get data in a single http request. If I pre-authenticate, getting a "ticket" and then use the ticket in the subsequent request for the file, the interaction will work. Is there anyway to authenticate and request data in a single http request?

There is probably a nicer way to do this, but here is how I got the auth ticket:

<java>
  
      String name = "admin";
      String password = "admin";
     
      //First we need to pre-auth and get an authentication ticket
      String authURL = URL_PREFIX  +  "service/api/login?u=" + name + "&pw=" + password;
     
      ByteArrayOutputStream bais = null;
      InputStream is = null;
      byte[] xmlTicketByteArray = null;
      byte[] pdfAsByteArray = null;
      String xmlTicketString = null;
      String ticket = null;
     
      try {
         bais = new ByteArrayOutputStream();
         URL url = new URL(authURL);
         URLConnection urlConnection = url.openConnection();
        
         is = urlConnection.getInputStream();
         byte[] byteChunk = new byte[READ_CHUNK_SIZE];
         int n;

         while ((n = is.read(byteChunk)) > 0) {
            bais.write(byteChunk, 0, n);
         }

         xmlTicketByteArray = bais.toByteArray();
         xmlTicketString = new String(xmlTicketByteArray, "UTF-8");
      } catch (Throwable t) {
         logger.warn("Failed to retrieve PDF for URL " + aPdfURL, t);
         return pdfAsByteArray;
      }
     
      ticket = this.getAuthenticationTicket(xmlTicketString);  




   /**
    * Parses out the authentication ticket from the XML.
    * @param anXMLAuthTicket
    * @return
    */
   private String getAuthenticationTicket(String anXMLAuthTicket){
     
      String extractPattern = "(?s)^.*?<ticket>(.*?)</ticket>";
      String authString = null;

      Matcher m = Pattern.compile(extractPattern).matcher(anXMLAuthTicket);

      if (m.find()) {
         authString = m.group(1);
      }
      return authString;
   }


</java>