I'm no security expert, but encrypting credentials and storing them in a
cookie seems a bit insecure.
I've had success with just using rapache as a trusted web service. I
implement this by running another instance of apache on a non-standard
port on the same machine and only accept connections from the localhost.
Then the public apache server running my php app will use curl to access
the trusted web service.
Here's some PHP pseudocode which retrieves a PDF from rapache. You can
do the same thing for an image as well.
# Call an SSL basic authenticated rapache service with a post payload
# Arguments:
# $service - a string which names the service. just appended to
$rapache_server
# $opt - name/value pairs to place in the post payload
# $post_data - the post payload, rapache_service() passes as is.
# $debug - place debugging info in html comment
function rapache_service($service,$opt=NULL,$post_data=NULL,$debug=FALSE){
$rapache_server = 'http://yourserverhere.com';
$html = '';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_URL, "$rapache_server/$service");
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt ($ch, CURLOPT_USERPWD, "${RAPACHE_USER}:${RAPACHE_PASS}");
if (!is_null($opt) || !is_null($post_data)){
curl_setopt ($ch,CURLOPT_POST,1);
if (!is_null($opt)){
foreach( $opt as $k => $v ) $post[ ] = sprintf( "%s=%s",
$k, urlencode( $v ) );
$post_data=implode('&',$post);
}
curl_setopt ($ch,CURLOPT_POSTFIELDS,$post_data);
}
$html = curl_exec ($ch);
if ($debug){
$html = '<!-- curl: ' .
curl_getinfo($ch,CURLINFO_EFFECTIVE_URL). ' '. curl_error($ch) . '-->' .
$html;
}
curl_close ($ch);
return $html;
}
# In this call, the post payload is constructed by
# rapache_fields_to_csv(): a function that connects
# to a database and constructs a CSV string.
#
# The service is HmiscDescPDF which uses the Hmisc package
# to construct a PDF with latex and the describe function.
$pdf = rapache_service('HmiscDescPDF', NULL, "${app_title}:
${form_desc}\n".rapache_fields_to_csv($app_name,$form,$totalrecs,$user_rights['group_id']));
header("Expires: 0");
if(isset($_SERVER['HTTP_USER_AGENT']) &&
strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')){
error_log("detected IE",0);
#header('Content-Type: application/force-download');
header('Content-type: application/pdf');
header("Cache-Control: cache");
header("Pragma: cache");
} else {
header("cache-control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Type: application/octet-stream');
}
header('Content-Length: '.strlen($pdf));
header('Content-disposition: attachment; filename="'.$pdfname.'"');
print $pdf;
Oops! that should have been https instead of http.
Jeff