/** * @param mixed $result * @param string $componentId * @param Session $session */ protected function storeResult($result, $componentId, Session $session) { $authorizedFor = $session->getBag()->has('authorizedFor') ? $session->get('authorizedFor') : ''; $token = $session->getEncrypted('token'); $tokenDetail = $this->getStorageApiToken($token); $creator = ['id' => $tokenDetail['id'], 'description' => $tokenDetail['description']]; $data = json_encode($result); $sapiUrl = $this->container->getParameter('storage_api.url'); try { $this->connection->insert('credentials', ['id' => $session->get('id'), 'component_id' => $componentId, 'project_id' => $tokenDetail['owner']['id'], 'creator' => json_encode($creator), 'data' => ByAppEncryption::encrypt($data, $componentId, $token, true, $sapiUrl), 'authorized_for' => $authorizedFor, 'created' => date("Y-m-d H:i:s")]); } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) { $id = $session->get('id'); throw new UserException("Credentials '{$id}' for component '{$componentId}' already exist!"); } }
/** * Add API to `consumers` and encrypt the secret */ public function addAction(Request $request) { $sapiToken = $this->container->get('syrup.storage_api')->getClient()->verifyToken(); $conn = $this->getConnection(); $api = $this->validateApiConfig(\Keboola\Utils\jsonDecode($request->getContent())); $sapiUrl = $this->container->getParameter('storage_api.url'); $api->app_secret_docker = ByAppEncryption::encrypt($api->app_secret, $api->component_id, $sapiToken['token'], false, $sapiUrl); $api->app_secret = $this->encryptBySelf($api->app_secret); try { $conn->insert('consumers', (array) $api); } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) { throw new UserException("Consumer '{$api->component_id}' already exists!"); } return new JsonResponse(['status' => 'created', 'component_id' => $api->component_id], 201, $this->defaultResponseHeaders); }
public function addAction($componentId, Request $request) { $token = $this->storageApi->verifyToken(); $credentials = $this->validateCredentials(jsonDecode($request->getContent())); $conn = $this->getConnection(); $consumer = $conn->fetchAssoc("SELECT `app_key`, `app_secret_docker`, `oauth_version` FROM `consumers` WHERE `component_id` = :componentId", ['componentId' => $componentId]); if (empty($consumer)) { throw new UserException("Component '{$componentId}' not found!"); } $creator = ['id' => $token['id'], 'description' => $token['description']]; $data = json_encode($credentials->data); $sapiUrl = $this->container->getParameter('storage_api.url'); $dataEncrypted = ByAppEncryption::encrypt($data, $componentId, $token['token'], true, $sapiUrl); $created = date("Y-m-d H:i:s"); try { $conn->insert('credentials', ['id' => $credentials->id, 'component_id' => $componentId, 'project_id' => $token['owner']['id'], 'creator' => json_encode($creator), 'data' => $dataEncrypted, 'authorized_for' => $credentials->authorizedFor, 'created' => $created]); } catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e) { throw new UserException("Credentials '{$credentials->id}' for component '{$componentId}' already exist!"); } return new JsonResponse(['id' => $credentials->id, 'authorizedFor' => $credentials->authorizedFor, 'creator' => $creator, 'created' => $created, '#data' => $dataEncrypted, 'oauthVersion' => $consumer['oauth_version'], 'appKey' => $consumer['app_key'], '#appSecret' => $consumer['app_secret_docker']], 201, ["Content-Type" => "application/json", "Access-Control-Allow-Origin" => "*", "Connection" => "close"]); }