Search code examples
phpjirajira-rest-api

Jira API getting attachment


I've written some code that displays a list of attachments. The problem is, though, that I want the user to be able to view the attachment without being logged in. Is there a way to authenticate for the user? Here is my code:

 //to get cases.  this returns a list of attachments, with the url in Jira

  $attachments = $jira_case->issues[0]->fields->attachment;

   //iterates over the lists and creates links
   foreach ($attachments as $i=> $attachment) {
      $html .="
      <tr>
          <td style='padding-left: 10px; width:0px; padding-top: 20px;' colspan='3'>
          ". ($i+1) .") <a target = '_blank' href ='". $attachment->content ."'>". nl2br($attachment->filename) ."</a>
          </td>
     </tr>";
}

The problem is that when the user clicks the link, if they are not logged in, they will be requested to log in. I don't want this, as it is my app the authenticates so that the user does not need a jira account. Is this possible? Maybe by passing some sort of token?


Solution

  • In case anyone else wonders how to do this:

    $url = "https://mySite.atlassian.net/secure/attachment/". $attachment_id ."/". $attachment_name ."?os_username=". $this->jira_user_name ."&os_password=" . $this->jira_password;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
    $file = curl_exec($ch);
    curl_close($ch);
    
    if($file){
         header('Content-Description: File Transfer');
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename='.$attachment_name);
         header('Expires: 0');
         header('Cache-Control: must-revalidate');
         header('Pragma: public');
         echo ($file);
         exit;
    }else{
         throw new Exception("Error:  file now found.");
    }