0

I ran

this curl in the command line in my Terminal

curl -c session_cookies.txt "https://login.uat.site.be/openid/oauth/authorize?client_id=site&state=e1968018-bb04-4475-86fa-b0728e9fb038&nonce=f7b76b29-2c42-423d-8d1c-5b1d0a8ce8bf&response_type=code&claims=%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D" 2>/dev/null| curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d '[email protected]&j_password=123' "https://login.uat.site.be/openid/login.do" 2>&1 >/dev/null | grep Location | grep code=

I got

this response back

< Location: https://testserver.sitenets.com/user/secure/dashboard?code=4z71kBZ4iYlqfDB7qOHvZgyXkqVEaL7v&state=e1968018-bb04-4475-86fa-b0728e9fb038


Note

code=4z71kBZ4iYlqfDB7qOHvZgyXkqVEaL7v

That is what I am after.


Then

I’ve tried converting my curl command lines to PHP curl

  $ch = curl_init( 'https://login.uat.site.be/openid/oauth/authorize' );
  curl_setopt($ch, CURLOPT_POST, true );
  curl_setopt($ch, CURLOPT_POSTFIELDS, array(

    'client_id' => 'site',
    'state' => $state,
    'nonce' => $nonce,
    'claims' => '%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D',
    'response_type' => 'code'

    ) );

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/session_cookies.txt');
  curl_setopt($ch, CURLOPT_VERBOSE, true);

  $first_curl = curl_exec($ch);

  $cookie_file = public_path(). '/session_cookies.txt';
  if (file_exists($cookie_file)) {

    $pem_file_path = '/Applications/MAMP/conf/apache/openid.site.com.cert.pem';

    $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_SSL_VERIFYPEER => true,
      CURLOPT_CAINFO=> $pem_file_path,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => http_build_query(array(
        'j_username' => urlencode($un),
        'j_password' => $pw,
        )),
      CURLOPT_COOKIEJAR => $cookie_file,
      CURLOPT_COOKIEFILE => $cookie_file,
      );
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    curl_setopt($ch, CURLOPT_URL, 'https://login.uat.site.be/openid/login.do');
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
      echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

    dd($result);
  }

I never get the same result as I would have execute that command on my Terminal.

I keep getting this junk on my browser, when I dd out my $result variable

   """
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->\r\n
    <!DOCTYPE html>\r\n
    <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->\r\n
    <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->\r\n
    <!--[if gt IE 8]><!-->\r\n
    <html class="no-js">\r\n
    <!--<![endif]-->\r\n
    <head>\r\n
        <title>site: Meld je aan</title>\r\n
        <meta content="IE=edge" http-equiv="X-UA-Compatible">\r\n
        <meta charset="utf-8">\r\n
        <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">\r\n
        <meta name="description" content="">\r\n
    \r\n
        <link rel="shortcut icon" href="//static.site.be/assets/favicon/favicon.ico" type="image/x-icon"/>\r\n
        <link rel="stylesheet" href="css/vendor/normalize.min.css">\r\n
        <link rel="stylesheet" href="css/login.css">\r\n
        <link rel="stylesheet" type="text/css" href="//static.site.be/assets/fonts/omnes-breuer-logo/stylesheet.css">\r\n
        \r\n
        <link rel="stylesheet" href="https://static.uat.site.be/oauth2/css/main.css">\r\n
    \r\n
        <!-- Adobe analytics -->\r\n
        <script src="https://www2.site.be/etc/designs/site-be/js/statistics/SiteCatalyst.js"></script>\r\n
    \r\n
        <script type="text/javascript">\r\n
            function hideHtmlElement(id) {\r\n
                document.getElementById(id).style.display = "none";\r\n
            }\r\n
    \r\n
            function showHtmlElement(id) {\r\n
                document.getElementById(id).style.display = "block";\r\n
            }\r\n
    \r\n
            function showPanel(id) {\r\n
                hideHtmlElement("forgotLogin");\r\n
                hideHtmlElement("showLogin");\r\n
                hideHtmlElement("forgotPassword");\r\n
                showHtmlElement(id);\r\n
            }\r\n
    \r\n
        </script>\r\n
    \r\n
        <!--[if lt IE 9]>\r\n
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>\r\n
        <![endif]-->\r\n
    </head>\r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    <div class="wrapper">\r\n
        <span id="showBackgroundForStatic" style="display: none">true</span>\r\n
        <!-- ERROR SCREEN -->\r\n
        <div class="container">\r\n
            <div id="loginContainer">\r\n
                <div id="showLogin">\r\n
                    <div class="options clearfix">\r\n
                        <div class="option-sites">\r\n
                            <ul>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://site.be/nl">Meer site</a></li>\r\n
                                <li><a target="_blank" href="http://site.be/nl"\r\n
                                       tabindex="-1">site.be</a></li>\r\n
                                <li><a target="_blank" href="http://business.site.be/"\r\n
                                       tabindex="-1">Business</a></li>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://corporate.site.be/" tabindex="-1">Over site</a></li>\r\n
                                <li><a target="_blank"\r\n
                                       href="http://klantenservice.site.be/"\r\n
                                       tabindex="-1">Klantenservice</a></li>\r\n
                            </ul>\r\n
                        </div>\r\n
                        <div class="option-language">\r\n
                            <ul>\r\n
                                <span id="languageForStatic" style="display: none">nl</span>\r\n
                                <!-- IT-4522 The design team uses languageForStatic to display text dynamically-->\r\n
                                \r\n
                                    \r\n
                                        <li class="active"><a href="?lang=nl" tabindex="-1">NL</a></li>\r\n
                                        <li><a href="?lang=fr" tabindex="-1">FR</a></li>\r\n
                                    \r\n
                                    \r\n
                                \r\n
                            </ul>\r\n
                        </div>\r\n
                    </div>\r\n
    \r\n
                    <div class="branding clearfix">\r\n
                        <div id="logo"><img src="https://static.site.be/oauth2/images/logosite.png"\r\n
                                            data-at2x="https://static.site.be/oauth2/images/[email protected]"\r\n
                                            alt="site"></div>\r\n
                        <h1><label>Fout bij aanmelden</label></h1>\r\n
                        <span id="languageForStatic" style="display: none">nl</span>\r\n
                        <!-- IT-5252 The design team uses languageForStatic to display text dynamically-->\r\n
                    </div>\r\n
                    <div class="contentContainer">\r\n
                        <p>Er ging iets mis. Gelieve de applicatie af te sluiten en daarna opnieuw op te starten.</p>\r\n
                    </div>\r\n
                </div>\r\n
    \r\n
    \r\n
            </div>\r\n
            <div class="copyright">&copy; site\r\n
                <script>document.write(new Date().getFullYear());</script>\r\n
                - <a href="http://klantenservice.site.be/content/juridische-informatie" target="_blank"\r\n
                     tabindex="-1">Juridische informatie</a>\r\n
                - <a href="https://www2.site.be/nl/privacy/"\r\n
                     href="https://www2.site.be/nl/privacy/" target="_blank"\r\n
                     tabindex="-1">Privacy</a><br>\r\n
                <a href="http://klantenservice.site.be/content/waar-vind-ik-de-algemene-voorwaarden-van-site" target="_blank"\r\n
                   tabindex="-1">Tariefinfo en algemene voorwaarden</a>\r\n
            </div>\r\n
        </div>\r\n
        <div class="bgContainer"></div>\r\n
    </div>\r\n
    \r\n
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>\r\n
    <script src="/openid/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>\r\n
    <script src="/openid/js/vendor/retina-1.1.0.min.js"></script>\r\n
    \r\n
    <!--[if lt IE 9]>!\r\n
    <script type="text/javascript" src="/openid/js/vendor/jquery.backgroundSize.js"></script>!\r\n
    <![endif]-->\r\n
    \r\n
    <script src="https://static.uat.site.be/oauth2/js/items.js"></script>\r\n
    <script src="https://static.uat.site.be/oauth2/js/main.js"></script>\r\n
    \r\n
    </body>\r\n
</html>\r\n
"""

I am stuck now, I am not sure what I did wrong, I am not sure what to do next.


Questions

How can I convert my curl to get the same result?

Is it something that I need to enable on my PHP curl settings?

Is it something that I need to update on my Apache settings?

What is wrong with my PHP CURLs?

How would one go about and debug this further?

4
  • The junk is the login page, and it's saying you're not logged in, I believe ("Er ging iets mis...."). That suggests to me your first OAuth cURL fails, or your second stage username/password fails. I recommend comparing the output of the command line first curl with the output of the PHP version of the first stage. Once both of those are identical, proceed to the second stage. Commented Jan 25, 2017 at 15:35
  • Also, you don't need to urlencode your username. Commented Jan 25, 2017 at 15:37
  • Oh. Really. I'll remove the urlencode and try again. Commented Jan 25, 2017 at 15:37
  • Yes, per the http_build_query docs: "By default, PHP_QUERY_RFC1738, which is encoding ... performed per RFC 1738 and the application/x-www-form-urlencoded media type, which implies that spaces are encoded as plus (+) signs." Commented Jan 25, 2017 at 15:39

1 Answer 1

1

Most likely the problem is that your first request fails cause you don't setup SSL verifying properly. You have to set the same CURL options for both requests. The difference is only URL and POST data.

So the code should look like this:

$cookie_file = public_path(). '/session_cookies.txt';
if (!file_exists($cookie_file)) return;
$pem_file_path = '/Applications/MAMP/conf/apache/openid.benu.com.cert.pem';
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_CAINFO=> $pem_file_path,
    CURLOPT_POST => true,
    CURLOPT_COOKIEJAR => $cookie_file,
    CURLOPT_COOKIEFILE => $cookie_file,
);
$ch = curl_init('https://login.uat.site.be/openid/oauth/authorize');
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
    'client_id' => 'benu',
    'state' => $state,
    'nonce' => $nonce,
    'claims' => '%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D',
    'response_type' => 'code'
)));
$result = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
if ($err)  {
  echo 'Error:' . curl_error($ch); return;
}
$ch = curl_init('https://login.uat.site.be/openid/login.do');
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
    'j_username' => '[email protected]',
    'j_password' => 'site1',
)));
$result = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
if ($err) {
  echo 'Error:' . curl_error($ch);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.