From 5af7cddaf9637d28596dc04ff64c8fdbadf2eae9 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Fri, 26 Apr 2024 13:52:19 +0200 Subject: [PATCH] fix signature http/https --- app/Lib/Helpers/ToolboxHelper.php | 11 +++++------ app/Models/Service.php | 8 ++++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/Lib/Helpers/ToolboxHelper.php b/app/Lib/Helpers/ToolboxHelper.php index 8098645..4982d1e 100644 --- a/app/Lib/Helpers/ToolboxHelper.php +++ b/app/Lib/Helpers/ToolboxHelper.php @@ -2,7 +2,6 @@ namespace App\Lib\Helpers; -use Exception; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Crypt; @@ -246,7 +245,7 @@ class ToolboxHelper * @param string $method * @param array $parameters * @return string request signature - * @throws CrmException If an error occurs. + * @throws \RuntimeException If an error occurs. */ public static function signRequest(string $secret, string $uri, string $method, array $parameters): string { @@ -302,14 +301,14 @@ class ToolboxHelper )); if (!$calculatedHash) { - throw new Exception('Failed to calculate requests hash'); + throw new \RuntimeException('Failed to calculate requests hash'); } return $calculatedHash; } catch (DecryptException $e) { - throw new Exception('Failed to decrypt service credentials', 0, $e); + throw new \RuntimeException('Failed to decrypt service credentials', 0, $e); } catch (\Exception $e) { - throw new Exception('An error occured when authenticating service', 0, $e); - }//end try + throw new \RuntimeException('An error occured when authenticating service', 0, $e); + } //end try } } diff --git a/app/Models/Service.php b/app/Models/Service.php index 43d6c6f..9b0af9c 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -115,7 +115,7 @@ class Service extends Model * * @param \Illuminate\Http\Request $request * @return boolean - * @throws \App\Exceptions\Api\V2\CrmException If an error occurs. + * @throws \RuntimeException If an error occurs. */ public function authenticateApi(Request $request): bool { @@ -125,9 +125,13 @@ class Service extends Model $data = (\is_array($data) and \json_last_error() === \JSON_ERROR_NONE) ? array_replace_recursive($request->all(), $data) : $request->all(); + $uri = $request->getUri(); + if (app()->environment(['staging', 'production']) and Str::of($uri)->startsWith('http')) { + $uri = (string)Str::of($uri)->replaceFirst('http', 'https'); + } $calculatedHash = ToolboxHelper::signRequest( $this->bearer_pwd, - $request->getUri(), + $uri, $request->getMethod(), $data );