0

I am trying to execute a PHP code base for GET API request, but I am getting

"AAR: 002" "No JWT Found"

error. below is the code

I am executing a GET request with cURL commands in PHP . The same code base and keys are working fine in Java and C# but when trying in PHP got this error.

When I try executing the below query

function getOpenSSLErrors()

{
    $messages = [];

    while ($msg = openssl_error_string()) { 
    $messages[] = $msg;
    }
    return $messages;
}

//function to generate JW token

function
generateJWT($algo,$header,$payload) {

    $headerEncoded = str_replace(['+','/','='], ['-','_',''], base64_encode(json_encode($header)));
    $payloadEncoded =str_replace(['+','/','='], ['-','_',''], base64_encode(json_encode($payload)));

    // Delimit with period (.)

    $dataEncoded ="$headerEncoded.$payloadEncoded";

 $filename = "C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\scbapibankingprivatekey.pem";

 //$filename = "C:\\Ranjit\\xampp\\htdocs\\certs\\scb-api-banking-privatekey.pem";

 $file1 = fopen($filename,"r");
$privateKey = fread($file1,filesize($filename));
//This will be your private key.- scb-api-banking-privatekey.pem
 fclose($file1);
 //echo $privateKey;

  echo '************************************************************************************\n';

    $privateKeyResource = openssl_pkey_get_private($privateKey,'123456');//password to fetch private key
    $result = openssl_sign($dataEncoded,$signature,$privateKeyResource,$algo);
  if ($result ===false)
{
throw new RuntimeException("Failed to generate signature: ".implode("\n",getOpenSSLErrors()));

}
 $signatureEncoded =str_replace(['+','/','='], ['-','_',''],base64_encode($signature));
$jwt ="$dataEncoded.$signatureEncoded";
 return $jwt;
}
// JWT Header
$header = ["alg" => "RS256","typ" => "JWT" ];
$iat = time();

$exp=$iat+30;
// JWT Payload data

$payload =['jti'=>'CLIENT','exp'=>$exp, 'iss'=>"CLIENT",
                                'aud'=>"SCB-APIBanking",'iat'=>$iat 
                                ];
 

// Create the JWT

$jwt = generateJWT('sha256',$header,$payload);
$sslCertPass = '123456';                                        //This will be the passphrase for p12 certificate

//$sslCert='client.cer';

$filenameCert = "C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\client.pem";

//$filenameCert = "C:\\Ranjit\\xampp\\htdocs\\certs\\client.pem";

    $sslCert = fread( fopen($filenameCert,"r"),filesize($filenameCert));

    //This will be your private key.- scb-api-banking-privatekey.pem

 //This will be the client.cer provided as part of activation - CURLOPT_SSLCERT

 

 //echo $sslCert;


//$sslKey = 'scb-apibanking-client-cert-private-key.pem';           //This will be your private key.pem


$privatekey_sslkey=realpath("C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\clientcertsslprivatekey.pem");

//$privatekey_sslkey=realpath("C:\\Ranjit\\xampp\\htdocs\\certs\\scb-apibanking-client-cert-key.pem");

if(!$privatekey_sslkey || !is_readable($privatekey_sslkey)){

    die("error: client.pem is not readable! realpath:\"{$privatekey_sslkey}\"- working dir: \"".getcwd()."\"effective user: ".print_r(posix_getpwuid(posix_geteuid()),true));

}


// $url = "https://apitest.standardchartered.com/activate";

//$url = "https://apitest.standardchartered.com/uat2/openapi/subscriptions/v2/credit-debit-advice";

$url = "https://apitest.standardchartered.com/uat2/openapi/subscriptions/v2";

//Post the activation token

$ch = curl_init($url );
$variable = "sample";

$pem=realpath("C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\client.pem");

if(!$pem || !is_readable($pem)){

    die("error: client.pem is not readable! realpath:\"{$pem}\" - working dir: \"".getcwd()."\"effective user: ".print_r(posix_getpwuid(posix_geteuid()),true));

}//echo $jwt ;
        
    $options = array(
        //CURLOPT_FAILONERROR => false,
        CURLOPT_RETURNTRANSFER => true,
        // CURLOPT_AUTOREFERER => true,
        //  CURLOPT_HEADER => true,
        //  CURLOPT_NOBODY =>true,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_SSLKEY =>$privatekey_sslkey,
        CURLOPT_SSLCERT => $pem,
        CURLOPT_KEYPASSWD => $sslCertPass,
        CURLOPT_SSLCERTPASSWD =>$sslCertPass,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => true,
        // CURLOPT_POST => false,
        // CURLOPT_POSTFIELDS => $jwt,
        CURLOPT_HTTPHEADER => array(
            "Content-Type => application/json",
            "JWTToken => $jwt",
            "Pragma => akamai-x-get-extracted-values"
        ),
    );
    
    //print_r( $options);
    curl_setopt_array($ch,$options );
    $resp = curl_exec($ch);
    echo $resp;
    $ch_errno = curl_errno($ch);
    $ch_erro = curl_error($ch);
    curl_close($ch);
        
    if ($ch_errno)
    {
        echo "cURL Error #:". $ch_erro;
    } 
    else
    {
        echo "Response:".$resp;
    }
    ?>

enter image description here

JeyanthiRanjit
  • 161
  • 1
  • 12
  • its a small portion of the code, as restrictions are here, can't post the entire code base – JeyanthiRanjit Dec 14 '22 at 10:44
  • `"Content-Type => application/json",` can't possibly be right. HTTP does not use PHP array syntax. – Álvaro González Dec 14 '22 at 10:45
  • Why @ÁlvaroGonzález – JeyanthiRanjit Dec 14 '22 at 10:45
  • 1
    Syntax is `Header-Name: Header value`. The `=>` symbol is used in PHP to e.g. define arrays, but here you have it inside a string literal, where no PHP code is expected. – Álvaro González Dec 14 '22 at 10:56
  • `The same code base and keys are working fine in Java and C#`...this makes no sense. C# and Java are separate languages. You can't have used "the same code" in both of those as this, because PHP code doesn't work in C#, and vice-versa, and same for Java. However, you might have written code in PHP which you consider to be equivalent in terms of functionality to the C# and Java versions. If so, and you want a comaprison of your working code to this code, you should have posted the C# or Java code, and added a suitable tag. – ADyson Dec 14 '22 at 11:03
  • But as Alvaro already pointed out, you have a silly typo which is making your HTTP header invalid, and that may well be the root of your issue anyway. Use `CURLOPT_HTTPHEADER => array( "Content-Type" => "application/json", "JWTToken" => $jwt, "Pragma" => "akamai-x-get-extracted-values ),` instead, and see if that helps with the problem – ADyson Dec 14 '22 at 11:04
  • nope that is not a typo, i tried with ":" first then got - "************************************************************************************\n An error occurred while processing your request. Reference #166.62a336a.1671016889.844f31b1 Response: An error occurred while processing your request." this error – JeyanthiRanjit Dec 14 '22 at 11:21
  • Meaning its not even getting processed with : , then only i changed to an arrow (=>) @ADyson – JeyanthiRanjit Dec 14 '22 at 11:23
  • `nope that is not a typo`...yes it is. Alvaro explained why, already. You may have done it deliberately, but it's still a misunderstanding of the syntax. – ADyson Dec 14 '22 at 11:25
  • `An error occurred while processing your request. Reference #166.62a336a.1671016889.844f31b1 `....if that's the response from the remote server to your cURL request, then you should consult with the support personnel who maintain that service, pass them the reference and ask them to explain the problem...because it seems like their server crashed. – ADyson Dec 14 '22 at 11:26
  • Try the version I suggested above, and see what response you get. – ADyson Dec 14 '22 at 11:27
  • @ADyson nope i changed my header like this - CURLOPT_HTTPHEADER => array( "Content-Type" => "application/json", "JWTToken" => $jwt, "Pragma" => "akamai-x-get-extracted-values" ), but still same error – JeyanthiRanjit Dec 14 '22 at 12:03
  • Which same error? The `An error occurred while processing your request` one, or the one in your question about the JWT? – ADyson Dec 14 '22 at 12:10
  • @ADyson no JWT found error – JeyanthiRanjit Dec 14 '22 at 12:33
  • As I mentioned above, if you share the working C# code with us, we can try to compare it to what you've done here, and see if you should be doing something different again. Obviously we cannot know precisely what the server is expecting, so a working example will be useful. – ADyson Dec 14 '22 at 12:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250409/discussion-between-jeyanthiranjit-and-adyson). – JeyanthiRanjit Dec 14 '22 at 12:58

0 Answers0