Re: 401 error using helloworld.php

Bruce420
New Contributor

401 error using helloworld.php

Using local Apache server to serve helloworld.php. Made the following changes to the file:

$username = ... changed this to the visa username used to sign into the developers page

$password = ... the password used for above

 

$cert = the path to cert.pem generated by Visa developer for this project

$key = the path to the private key also generated by the Visa developer

 

Result is a 401 error.

 

Note that the approach used in helloworld.php seems to violate basic cryptography security rules. 

My understanding is that two way SSL requires 4 keys. Party A (Alice) would have a public and private key. Party B (Bob) would also have a public and private key. When Alice sends a message to Bob, she uses Bob's public key for encryption. Bob would decipher the message with

his private key. To return a message, Bob would use Alice's public key. Alice would decipher the message using her private key. The two public keys are made public. The two private keys are kept secret. 

 

In this helloworld example, it appears that I am supplying a private key, which is a no-no. Perhaps this is just for test purposes and not how a production request is made.

 

2 REPLIES 2
SyedSa
Community Moderator

Re: 401 error using helloworld.php

Hi @Bruce420, Thank you for reaching out. An agent will get back to you as soon as possible. Until then, if any community member knows a solution, feel free to reply in this thread.

DianaVisaPM
Visa Developer Support Specialist

Re: 401 error using helloworld.php

Hey @Bruce420,

 

It looks like you're trying to make an API call using PHP and are running into some issues with a 401 Unauthorized error. This error typically indicates that the credentials provided are not correct, or the request is not properly authenticated. Let's address your concerns and the potential solutions.

 

Addressing the 401 Error:

1. Check Credentials:
- Ensure that the `$username` and `$password` are correct and match those used to sign into the Visa Developer Portal.
- Make sure that the credentials do not have any extra spaces or hidden characters.

2. Check Certificate and Key Paths:
- Verify that the paths to `cert.pem` and the private key are correct and that the files are accessible by the script.
- Ensure that the certificate and key files are properly generated and not corrupted.

3. Proper Two-Way SSL (Mutual SSL) Setup:
- In Mutual SSL, both the client and server authenticate each other using certificates. For Visa's implementation, you typically use the certificate and private key provided by Visa.

 

Here’s a basic example of how you might set up the PHP script for making a request to Visa’s API:

 

```php
// START 
<?php

// Visa API credentials
$username = 'your_visa_username';
$password = 'your_visa_password';

// Paths to certificate and private key
$cert = '/path/to/cert.pem';
$key = '/path/to/private_key.pem';

// API endpoint
$url = 'https://sandbox.api.visa.com/helloworld';

// Initialize cURL
$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLKEY, $key);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
echo 'Response: ' . $response;
}

// Close the cURL session
curl_close($ch);

?>
// END ```

 

Security Concerns and Best Practices:

- Private Key Handling: In a production environment, ensure that your private key is stored securely and access is restricted. Only the application requiring it should have access.
- Environment Variables: Consider storing sensitive information such as usernames, passwords, and paths to certificates in environment variables rather than hardcoding them in your script.
- Certificate Management: Regularly rotate your certificates and keys to minimize the risk of compromise.

 

Important Notes:

- Mutual SSL in Production: While using a private key in your script might seem insecure, in mutual SSL setups, the client (your application) indeed uses its private key to authenticate itself to the server (Visa API). The server’s public key (from the server certificate) is used by your client to verify the server’s identity.

- Visa Developer Documentation: Ensure you follow the Visa Developer documentation for Mutual SSL setup. They provide detailed instructions and best practices for securely implementing their APIs.




Thanks,

Diana



Was your question answered? Don't forget to click on "Accept as Solution" to help other devs find the answer to the same question.