Files
alfa-bank-sdk/examples/14_generate_client_secret.php
Anatoliy Poloz 2964c391b5 Add H2H authentication service and new example flows
- Implement H2H authentication service for secure client authentication
- Add new examples for authorization code flow and H2H auth service
- Include example for generating client secret
- Update core connector and token request functionality
- Enhance README with new features

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-01-23 20:58:30 +03:00

59 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Apoloz\AlfaBank\AlfaBank;
use Apoloz\AlfaBank\Requests\Auth\RefreshClientSecretRequest;
use Apoloz\AlfaBank\Enums\Environment;
/**
* UTILITY SCRIPT: Generate a Client Secret using your TLS certificates.
*
* Run this ONCE when setting up the project or if you lose your secret.
*
* Requirements:
* 1. You have the `client_id` (from the bank/portal).
* 2. You have the TLS certificates (.pem/.key) authorized by the bank.
*/
// 1. Configuration
$certPath = __DIR__ . '/certs/transport.crt'; // Your mTLS certificate
$keyPath = __DIR__ . '/certs/transport.key'; // Your mTLS private key
$clientId = 'YOUR_CLIENT_ID_UUID'; // E.g. '0cee0683-85ae-...'
// 2. Initialize Connector (No Login required yet)
$baas = AlfaBank::baas(
certificatePath: $certPath,
privateKeyPath: $keyPath,
environment: Environment::SANDBOX // Change to PRODUCTION for real usage
);
try {
echo "Requesting new Client Secret for Client ID: {$clientId}...\n";
// 3. Send Request
$request = new RefreshClientSecretRequest($clientId);
$response = $baas->send($request);
// 4. Output Result
$data = $response->dto(); // Returns ClientSecretData
echo "\n-------------------------------------------------------\n";
echo "SUCCESS! New Client Secret Generated:\n";
echo "-------------------------------------------------------\n";
echo "Client ID: " . $data->clientId . "\n";
echo "Client Secret: " . $data->clientSecret . "\n";
echo "-------------------------------------------------------\n";
echo "IMPORTANT: Save this to your .env file immediately.\n";
echo "It will not be shown again.\n";
} catch (\Exception $e) {
echo "\nERROR: Could not generate secret.\n";
echo "Message: " . $e->getMessage() . "\n";
if (method_exists($e, 'getResponse')) {
echo "Response: " . $e->getResponse()->body() . "\n";
}
}