public function generateTOTPCode()
{
$secret = $this->totpSecret; // Example, if not Base32 encoded
$base32Secret = Base32::encode($secret);
if (!$base32Secret) {
throw new Exception('TOTP Secret is not set in the environment file.');
}
$totp = TOTP::create($base32Secret);
return $totp->now(); // This will generate the current 6-digit TOTP code
}
protected function generateSessionToken()
{
$client = new Client();
try
{
$totpCode = $this->generateTOTPCode();
$response = $client->post('https://apiconnect.angelbroking.com/rest/auth/angelbroking/user/v1/loginByPassword', [
'json' => [
'clientcode' => env('ANGEL_BROKING_CLIENT_CODE'),
'password' => env('ANGEL_BROKING_PASSWORD'),
'totp' => $totpCode,
],
'headers' => [
'X-API-Key' => $this->apiKey,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-UserType' => 'USER',
'X-SourceID' => 'WEB',
'X-ClientLocalIP' => $_SERVER['SERVER_ADDR'],
'X-ClientPublicIP' => file_get_contents('https://api.ipify.org'),
'X-MACAddress' => 'f8:32:e4:9c:27:0d',
],
]);
$statusCode = $response->getStatusCode();
$responseBody = $response->getBody()->getContents();
$responseData = json_decode($responseBody, true);
Log::info('API Response:', ['status' => $statusCode, 'body' => $responseBody]);
if ($statusCode == 200) {
if (isset($responseData['data']['jwtToken'])) {
return $responseData['data']['jwtToken'];
} elseif (isset($responseData['success']) && !$responseData['success']) {
$errorMessage = isset($responseData['message']) ? $responseData['message'] : 'Unknown error occurred';
throw new Exception('Failed to generate session token: ' . $errorMessage);
}
throw new Exception('Failed to generate session token: Unexpected response');
} else {
throw new Exception('Failed to generate session token: Status Code ' . $statusCode);
}
} catch (Exception $e) {
dd($e);
Log::error('Error generating session token: ' . $e->getMessage());
throw $e;
}
}
env : -
ANGEL_BROKING_CLIENT_CODE=H56857442
ANGEL_BROKING_PASSWORD=5285
ANGEL_BROKING_TOTP=BDODBV4DALNVJGAPS4R7YS3J7A
plz give solution