path(), 'api/v2') === 0 ? 'apiV2.' : null) ?? 'none'; return strpos($routeName, 'apiV2.') === 0; } /** * Tells if the request is within apiV4 routes. * * @param \Illuminate\Http\Request $request * @return boolean */ public static function isRequestForApiV4(Request $request): bool { // * Small hack to ensure to know if route is From APIv4. $routeName = static::getRouteName($request) ?? (strpos($request->path(), 'api/v4') === 0 ? 'apiV4.' : null) ?? 'none'; return strpos($routeName, 'apiV4.') === 0; } /** * Try to get current route name * * @param \Illuminate\Http\Request $request * @return string|null */ public static function getRouteName(Request $request): ?string { return Route::currentRouteName() ?? ($request->route() ? $request->route()->getName() : null); } /** * Try to get current route prefix. * * @param \Illuminate\Http\Request $request * @return string|null */ public static function getRoutePrefix(Request $request): ?string { return (Route::getCurrentRoute() ? Route::getCurrentRoute()->getPrefix() : null) ?? ($request->route() ? $request->route()->getPrefix() : null); } /** * Try to get route from request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Routing\Route|null */ public static function getRoute(Request $request): \Illuminate\Routing\Route|null { return Route::getRoutes()->getByName(static::getRouteName($request)); } /** * Get composer.json infos. * * @return array */ public static function getPackageInfo(): array { if (!static::$packageInfos) { static::$packageInfos = \json_decode(\file_get_contents(__DIR__ . '/../../composer.json'), true); } return static::$packageInfos; } /** * Calculate the request signature. * * @param string $secret * @param string $uri * @param string $method * @param array $parameters * @return string request signature * @throws \App\Exceptions\Api\V4\CrmException If an error occurs. */ public static function signRequest(string $secret, string $uri, string $method, array $parameters): string { try { $uri = collect(\parse_url($uri)); $method = \strtolower($method); // * Remove signature from parameters to calculate the hashmac. $signature = collect([ 'uri' => sprintf( '%s://%s%s', $uri->get('scheme'), $uri->get('host'), $uri->get('path') ), 'method' => $method ]); $flattenRequestArray = function (array $array) use (&$flattenRequestArray) { $out = []; foreach ($array as $key => $value) { if (is_array($value)) { $subArray = $flattenRequestArray($value); foreach ($subArray as $subKey => $subValue) { $out["$key.$subKey"] = $subValue; } continue; } $out[$key] = $value; } return $out; }; $parameters = $flattenRequestArray($parameters); $parameters = collect($parameters); // * Remove signature from parameters. $parameters->pull('signature'); // * Natural sort all parameters. $parameters = $parameters->sortKeys(\SORT_NATURAL) ->map(function ($value, $key) { return sprintf('%s=%s', \rawurlencode($key), \rawurlencode($value)); }); $signature->push($parameters->implode('&')); $calculatedHash = base64_encode(hash_hmac( 'sha256', $signature->implode('\n'), Crypt::decryptString($secret), true )); if (!$calculatedHash) { throw new CrmException('Failed to calculate requests hash'); } return $calculatedHash; } catch (DecryptException $e) { throw new CrmException('Failed to decrypt service credentials', 0, $e); } catch (\Exception $e) { throw new CrmException('An error occured when authenticating service', 0, $e); } //end try } }