AnsweredAssumed Answered

REST API -> changepassword programatically? HELP!!!

Question asked by urnamma on Dec 14, 2010
Latest reply on Dec 20, 2010 by lista
I have been trying for several days to make a little application that will generate a new randomly generated password for a given inputted username. I have tried setting alf_ticket to the parsed out ticket, and including the ticket in the http headers… What else can I do? The code below represents the 'last try' before posting this.

I see a number of different threads containing similar requests. My code below is php, but I can reverse engineer anything short of assembly code, if someone happens to have done something like this in PERL, Ruby, Java, or Haskell for that matter… The key is that it's an external app interfacing with Alfresco. Any help would be appreciated. Is it appropriate to offer to buy someone beer over a forum?  :D

Many thanks for any guidance that anyone may be able to give. I have yet to see a clear answer to any question regarding the changepassword web script included in the restful api, so maybe this will be useful to a number of people!

$username and $password are both global variables;


function adminLogin()
        {
                // $username = 'admin';
                // $password = 'admin';
                $url = 'https://url:8085/alfresco/service/api/login';

                // open connection
                $ch = curl_init();
                //set the url, number of POST vars, POST data
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_URL, $url);
            //Remove the following and use proper ssl verification after test!
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                /*
             * curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
                 * curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                 * curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/fun.pem");
             */
            
                // printf($ch);
                // curl_setopt($ch,CURLOPT_POST,count($fields));
                curl_setopt($ch,CURLOPT_POSTFIELDS,'{"username":"admin","password":"admin"}');
                // curl_setopt($ch, CURLINFO_HEADER_OUT, true);
                //execute post
                // $result = curl_exec($ch);

                if( ! $result = curl_exec($ch))
                {
                        trigger_error(curl_error($ch));
                }
            
                //close connection
                curl_close($ch);

                return $result;
                // after this, it should return a ticket. We'll need to capture this and use it later… Perhaps establish global?
        }
   
   function adminLogout($ticket)
   {
               // not yet implemented
   }

  function resetPassword($username, $password)
        {

         $jsonResponse = adminLogin();
            $json_a = json_decode($jsonResponse,true);
            // print_r($json_a);
            $ticket = $json_a["data"]["ticket"];

            $url = 'https://url:8085/alfresco/service/api/person/changepassword/{userName}'; // ?alf_ticket='.$ticket.'&format=json';
           
         /*
         $fields = array(
                'userName' =>urlencode($username),
                'newpw' =>urlencode($password)
            );
                $fields_string = '';

                foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
                rtrim($fields_string,'&');
         */
                // open connection
                $ch = curl_init();

                //set the url, number of POST vars, POST data
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json',/*'Authorization: Basic'*/,'ticket: '.$ticket.''));
                curl_setopt($ch,CURLOPT_URL,$url);
                //Remove the following and use proper ssl verification after test!
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        /*
         * curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
         * curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
         * curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/fun.pem");
         */
        // curl_setopt($ch,CURLOPT_POST,count($fields));
        // curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch,CURLOPT_POSTFIELDS,'{"userName":"'.$username.'","newpw":"'.$password.'"}');
      
      //execute post
      if( ! $result = curl_exec($ch))
        {
            trigger_error(curl_error($ch));
        }
      
      //close connection
      curl_close($ch);
   
      return $result;
   
      //somehow close session as admin, and create new session as user with username and password… Does password need to be global?
   }
   
   $r = resetPassword($username,$password);
   print_r($r);
   
?>

Alternatively, it gives me the following errors:

<html><head><title>Apache Tomcat/6.0.18 - Error report</title><style><!–H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}–></style> </head><body><h1>HTTP Status 401 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>This request requires HTTP authentication ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.18</h3></body></html>

{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  },

  "message" : "11140025 Do not have appropriate auth or wrong auth details provided.",
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 11140025 Do not have appropriate auth or wrong auth details provided.",

  "callstack" :
  [
          ""      ,"org.springframework.extensions.webscripts.WebScriptException: 11140025 Do not have appropriate auth or wrong auth details provided."
      ,"org.alfresco.repo.web.scripts.person.ChangePasswordPost.executeImpl(ChangePasswordPost.java:120)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.executeImpl(DeclarativeWebScript.java:231)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:64)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:367)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:321)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:417)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:434)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:298)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:307)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:178)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:116)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)"
      ,"org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)"
      ,"org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)"
      ,"org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)"
      ,"org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697)"
      ,"org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889)"
      ,"org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)"
      ,"java.lang.Thread.run(Thread.java:662)"

  ],

  "server" : "Alfresco Community v3.3.0 (2765) schema 4,009",
  "time" : "Dec 14, 2010 1:48:06 AM"
}

Outcomes