djoos / EscapeWSSEAuthenticationBundle

Symfony bundle to implement WSSE authentication
http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html
137 stars 59 forks source link

Get redirecting by curl #50

Closed MGDSoft closed 10 years ago

MGDSoft commented 10 years ago

Hi @djoos , I have this easy code

callAction();

 function callAction()
{
    $content=  curl_download('http://sym2_pay_gateway.dev/api/doc', generate_wsse_header('app_53b14441d854d', 'f73ea3026372fbdafdcaefd8d79249efa530470a'));
    echo $content;
    die;
}

 function generate_wsse_header($username, $secret)
{
    // date_default_timezone_set('Europe/Paris');
    $nonce = md5(rand(), true);
    $created = date(DATE_ATOM);

    $digest = base64_encode(sha1($nonce.$created.$secret,true));
    $b64nonce = base64_encode($nonce);

    return sprintf('X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
        $username,
        $digest,
        $b64nonce,
        $created
    );
}

function curl_download($Url, $header){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER,0 );

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    //curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

    // Header
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));

    // Download the given URL, and return output
    $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}

but i get this

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="refresh" content="1;url=http://sym2_pay_gateway.dev/api/doc/" />

        <title>Redirecting to http://sym2_pay_gateway.dev/api/doc/</title>
    </head>
    <body>
        Redirecting to <a href="http://sym2_pay_gateway.dev/api/doc/">http://sym2_pay_gateway.dev/api/doc/</a>.
    </body>
</html>

In postman it works perfectly but by curl it doesn`t. Sorry for this dummy question :_

djoos commented 10 years ago

Hi @MGDSoft,

thanks for getting in touch!

I'd suggest you to have a look at the execute-method of one of our (base classes used for building our API clients)[https://github.com/escapestudios/EscapeAPIClientBundle/blob/master/REST/Request.php] and furthermore at the RequestCore-library either (here)[https://github.com/pzb/requestcore] or (here)[https://github.com/amazonwebservices/aws-sdk-for-php/tree/master/lib/requestcore] for the cURL side of things.

Also: have a look at your log file to see what is going on exactly - the redirect seems to suggest your application is redirecting the request, as this is not something the WSSE Authentication-bundle would be doing...

Hope this helps, let me know how it goes!

Kind regards, David

MGDSoft commented 10 years ago

Thanks @djoos . I will look https://github.com/escapestudios/EscapeAPIClientBundle/blob/master/REST/Request.php

And again thanks for your great job and your patience ! :smiley:

djoos commented 10 years ago

Hi @MGDSoft,

even though this one is closed, do let me know via a comment how you get on with the issue you're having - hope you'll be up and running soon!

Have a great day, David

MGDSoft commented 10 years ago

Finally this works!!

The problem was because of nelmio bundle, I dont know why but it has a redirect and using stateless header didnt send. If I use a normal url /api/v1/default.json works propertly :(. I dont know how postman was working.

Thanks!

djoos commented 10 years ago

I'm glad you got to the bottom of the redirect, thanks for your feedback @MGDSoft!