php: forward ntlm credentials to curl

佐手、 提交于 2019-12-09 12:53:45

问题


I have a dynamic php page which I need to call with a get parameter. I then want to put the generated html into a string and use it later ( I'm tryign out tonic framework for web services)

So this is similar to PHP - Read dynamically generated (and echoed) HTML into a string? and I tried the answer that uses cURL.

The issue is that authentication is done with ntlm (apache mod_auth_sspi). The php script executing curl is already authenticated, eg only valid users can ever execute it. It is somehow possible to pass on these "credentials" to cURL? (username is available but of course not the password)

Or a completely different approach would be fine too but only idea I had was to make a function that creates a string with html content.

$response = new Response($request);
$format = $request->mostAcceptable(array(
    'json', 'html', 'txt'
        ));

switch ($format) {

    case 'html':
        $response->addHeader('Content-type', 'text/html');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://localhost/viewRecord.php?identifier=' . $identifier);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); 
        $html = curl_exec($ch);
        curl_close($ch);
        $response->body = $html;
        break;
    //...   
}

回答1:


I was able to get this to work by adding the following curl options:

curl_setopt($curly[$id], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($curly[$id], CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($curly[$id], CURLOPT_USERPWD, ":");

There is a bug open for this depending on the version of php: https://bugs.php.net/bug.php?id=62195




回答2:


This is what worked for me:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM|CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($ch, CURLOPT_USERPWD, "YOUR_USER:YOUR_PWD");



回答3:


The answer is simple:

This is not possible.

A workaround is to put all the files (including php, JavaScript and CSS) in a directory that does not need require NTLM authentication.

To achieve this one either needs access to the Apache Configuration and if that is not possible only thing you can hope for is that the Apache Configuration allows overriding SSPI in .htaccess. Allow any authentication (=also none) but limit access to 127.0.0.0 since allrequest come from cURL on the same server.

For authorization, you can put the data in the php session an pass the session cookie on to cURL and then the session data can be used for authorization in the page called from cURL.

EDIT:

I've basically reduced NTLM usage even more. I now have 1 login page (authentication) and everything else is controlled by php session (authorization). See

Apache2, PHP: create automatic ntlm login page



来源:https://stackoverflow.com/questions/9111705/php-forward-ntlm-credentials-to-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!