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"]); }
/** * Replace placeholders from config data (eg 3rd level domain) * * auth_url: https://%%domain%%.zendesk.com/...; token_url: https://%%something%%.zendesk.com/.. * in session: * {'domain': 'keboola', 'something': 'maybe-something-else'} * * @param array $api * @return array */ protected function buildAuthUrls(array $api, Session $session) { $userDataJson = $session->get('userData'); $userData = empty($userDataJson) ? [] : jsonDecode($userDataJson, true); array_walk($api, function (&$url, $apiKey) use($userData) { if (substr($apiKey, -4) != '_url') { return; } preg_match_all('/%%(.*?)%%/', $url, $matches); foreach ($matches[1] as $match) { if (in_array($match, ['app_key', 'client_id', 'redirect_uri', 'oauth_token'])) { continue; } if (!isset($userData[$match])) { throw new UserException("The URL requires 'userData' to contain '{$match}'"); } $url = str_replace('%%' . $match . '%%', $userData[$match], $url); } }); return $api; }