withJson($response, []); } public function generate_key(Request $request, Response $response): Response { $server_addr = explode('.', $request->getServerParams()['SERVER_ADDR']); $remote_addr = explode('.', $request->getServerParams()['REMOTE_ADDR']); for ($i = 0; $i < 3; $i ++) { if ($server_addr[$i] != $remote_addr[$i]) { throw new \InvalidArgumentException('Invalid connection address.'); } } $salt = mt_rand(); $signature = hash_hmac('sha256', $salt, 'contabilidad', true); $key = urlencode(base64_encode($signature)); return $this->withJson($response, ['key' => $key]); } }