diff --git a/src/flextype/core/Endpoints/Utils.php b/src/flextype/core/Endpoints/Utils.php index 722429a4..4c095567 100644 --- a/src/flextype/core/Endpoints/Utils.php +++ b/src/flextype/core/Endpoints/Utils.php @@ -25,14 +25,14 @@ use function filesystem; class Utils extends Api { /** - * Clear cache + * Generate token * * @param ServerRequestInterface $request PSR7 request. * @param ResponseInterface $response PSR7 response. * * @return ResponseInterface Response. */ - public function clearCache(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + public function generateToken(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { // Validate Api Request if ( @@ -45,10 +45,72 @@ class Utils extends Api return $this->getApiResponse($response, $this->getStatusCodeMessage($result['http_status_code']), $result['http_status_code']); } - // Clear cache - filesystem()->directory(PATH['tmp'])->delete(); + // Generate token + $token = generateToken(); // Return success response - return $this->getApiResponse($response, [], 204); + return $this->getApiResponse($response, ['token' => $token], 200); + } + + /** + * Generate token hash + * + * @param ServerRequestInterface $request PSR7 request. + * @param ResponseInterface $response PSR7 response. + * + * @return ResponseInterface Response. + */ + public function generateTokenHash(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { + // Get Request Parsed Body + $requestParsedBody = $request->getParsedBody(); + + // Validate Api Request + if ( + count($result = $this->validateApiRequest([ + 'request' => $request, + 'api' => 'utils', + 'params' => ['token', 'access_token', 'string'], + ])) > 0 + ) { + return $this->getApiResponse($response, $this->getStatusCodeMessage($result['http_status_code']), $result['http_status_code']); + } + + // Generate token + $token = generateTokenHash($requestParsedBody['string']); + + // Return success response + return $this->getApiResponse($response, ['hashed_token' => $token], 200); + } + + /** + * Verify token hash + * + * @param ServerRequestInterface $request PSR7 request. + * @param ResponseInterface $response PSR7 response. + * + * @return ResponseInterface Response. + */ + public function verifyTokenHash(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { + // Get Request Parsed Body + $requestParsedBody = $request->getParsedBody(); + + // Validate Api Request + if ( + count($result = $this->validateApiRequest([ + 'request' => $request, + 'api' => 'utils', + 'params' => ['token', 'access_token', 'string', 'hash'], + ])) > 0 + ) { + return $this->getApiResponse($response, $this->getStatusCodeMessage($result['http_status_code']), $result['http_status_code']); + } + + // Verify + $verified = verifyTokenHash($requestParsedBody['string'], $requestParsedBody['hash']); + + // Return success response + return $this->getApiResponse($response, ['verified' => $verified], 200); } } diff --git a/src/flextype/routes/endpoints/utils.php b/src/flextype/routes/endpoints/utils.php index 57c04759..46405edc 100644 --- a/src/flextype/routes/endpoints/utils.php +++ b/src/flextype/routes/endpoints/utils.php @@ -21,15 +21,46 @@ use Flextype\Endpoints\Utils; use function app; /** - * Clear cache + * Generate token * - * endpoint: POST /api/v0/utils/cache/clear + * endpoint: POST /api/v0/utils/generate-token * * Body: * token - [REQUIRED] - Valid public token. * access_token - [REQUIRED] - Valid access token. - * + * * Returns: - * Returns an empty body with HTTP status 204 + * Generated token object. */ -app()->post('/api/v0/utils/cache/clear', [Utils::class, 'clearCache'])->setName('cache.clear'); +app()->post('/api/v0/utils/generate-token', [Utils::class, 'generateToken'])->setName('utils.generate-token'); + +/** + * Generate token hash + * + * endpoint: POST /api/v0/utils/generate-token-hash + * + * Body: + * token - [REQUIRED] - Valid public token. + * access_token - [REQUIRED] - Valid access token. + * string - [REQUIRED] - String to hash. + * + * Returns: + * Generated token hash object. + */ +app()->post('/api/v0/utils/generate-token-hash', [Utils::class, 'generateTokenHash'])->setName('utils.generate-token-hash'); + +/** + * Verify token hash + * + * endpoint: POST /api/v0/utils/verify-token-hash + * + * Body: + * token - [REQUIRED] - Valid public token. + * access_token - [REQUIRED] - Valid access token. + * string - [REQUIRED] - String to verify. + * hash - [REQUIRED] - Hash to verify. + * + * Returns: + * Token verification object. + */ +app()->post('/api/v0/utils/verify-token-hash', [Utils::class, 'verifyTokenHash'])->setName('utils.verify-token-hash');