vendor/abap/login-gov-bundle/src/Service/LoginGovClient.php line 283

Open in your IDE?
  1. <?php
  2. namespace Abap\LoginGovBundle\Service;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\Process\Exception\ProcessFailedException;
  5. use Symfony\Component\Process\Process;
  6. class LoginGovClient
  7. {
  8.     /**
  9.      * @var array
  10.      */
  11.     private $config;
  12.     public function __construct(ParameterBagInterface $params)
  13.     {
  14.         $this->config $this->prepareConfig($params);
  15.     }
  16.     public function getSAMLRequest(): string
  17.     {
  18.         $process = new Process(explode(' '$this->prepareAuthnRequestCommand()));
  19.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  20.         $process->run();
  21.         if (!$process->isSuccessful()) {
  22.             throw new ProcessFailedException($process);
  23.         }
  24.         return $process->getOutput();
  25.     }
  26.     public function getSSOUrl()
  27.     {
  28.         return $this->config['sso_login_url'];
  29.     }
  30.     public function artifactResolve($artifact): array
  31.     {
  32.         $process = new Process(explode(' '$this->prepareArtifactResolveCommand($artifact)));
  33.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  34.         $process->run();
  35.         if (!$process->isSuccessful()) {
  36.             throw new ProcessFailedException($process);
  37.         }
  38.         $saml $process->getOutput();
  39.         $parser = new SimpleLoginGovParser($saml);
  40.         $error $parser->getResponseErrorForArtifactResponseIfPresent();
  41.         if (count($error)) {
  42.             return [
  43.                 'saml'  => $saml,
  44.                 'error' => $error,
  45.             ];
  46.         }
  47.         return [
  48.             'saml' => $saml,
  49.             'data' => array_merge(
  50.                 $parser->getAssertionAttributesForArtifactResponse(),
  51.                 $parser->getNameIdAndSessionIndexForArtifactResponse()
  52.             ),
  53.         ];
  54.     }
  55.     public function sendLogoutRequest($nameId$sessionIndex): bool
  56.     {
  57.         $process = new Process(explode(' '$this->prepareLogoutRequestCommand($nameId$sessionIndex)));
  58.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  59.         $process->run();
  60.         if (!$process->isSuccessful()) {
  61.             throw new ProcessFailedException($process);
  62.         }
  63.         $xmlContent $process->getOutput();
  64.         $xmlContent trim(preg_replace('/\s\s+/'' '$xmlContent));
  65.         return (bool) preg_match('/<saml2p:StatusCode.+Success"\/>/i'$xmlContent);
  66.     }
  67.     public function sendAddDocumentToSigningRequest($documentXmlPath$successUrl$failureUrl): string
  68.     {
  69.         $process = new Process(explode(' '$this->prepareAddDocumentToSigningRequestCommand($documentXmlPath$successUrl$failureUrl)));
  70.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  71.         $process->run();
  72.         if (!$process->isSuccessful()) {
  73.             throw new ProcessFailedException($process);
  74.         }
  75.         $xmlContent $process->getOutput();
  76.         $xmlContent trim(preg_replace('/\s\s+/'' '$xmlContent));
  77.         return $xmlContent;
  78.     }
  79.     public function sendGetSignedDocumentRequest($documentId$documentXmlPath): string
  80.     {
  81.         $process = new Process(explode(' '$this->prepareGetSignedDocumentRequestCommand($documentId$documentXmlPath)));
  82.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  83.         $process->run();
  84.         if (!$process->isSuccessful()) {
  85.             throw new ProcessFailedException($process);
  86.         }
  87.         $xmlContent $process->getOutput();
  88.         $xmlContent trim(preg_replace('/\s\s+/'' '$xmlContent));
  89.         return $xmlContent;
  90.     }
  91.     public function sendEpuapDocument(string $fileToSend, ?string $additionalData nullbool $isProbing falsestring $outputFormat 'xml', ?string $documentFileName null): string
  92.     {
  93.         $process = new Process(explode(' '$this->prepareEpuapDocumentSendCommand($fileToSend$additionalData$isProbing$outputFormat$documentFileName)));
  94.         $process->setWorkingDirectory($this->config['connector_app_dir']);
  95.         $process->run();
  96.         if (!$process->isSuccessful()) {
  97.             throw new ProcessFailedException($process);
  98.         }
  99.         return $process->getOutput();
  100.     }
  101.     private function prepareAuthnRequestCommand(): string
  102.     {
  103.         $command 'java -jar ' $this->config['connector_app_jar'];
  104.         $command .= ' -sks ' $this->config['sig_keystore_path'];
  105.         $command .= ' -sksp ' $this->config['sksp'];
  106.         $command .= ' -o AuthnRequest';
  107.         $command .= ' -acs ' $this->config['acs'];
  108.         $command .= ' -ds ' $this->config['authn_request_ds'];
  109.         $command .= ' -is ' $this->config['is'];
  110.         if(array_key_exists('provider_name'$this->config) && $this->config['provider_name']) {
  111.             $command .= ' -pn ' $this->config['provider_name'];
  112.         }
  113.         $command .= ' -of base64';
  114. //        $command .= ' -l';
  115.         return $command;
  116.     }
  117.     private function prepareArtifactResolveCommand($artifact): string
  118.     {
  119.         $command 'java -jar ' $this->config['connector_app_jar'];
  120.         $command .= ' -sks ' $this->config['sig_keystore_path'];
  121.         $command .= ' -sksp ' $this->config['sksp'];
  122.         $command .= ' -eks ' $this->config['enc_keystore_path'];
  123.         $command .= ' -eksp ' $this->config['eksp'];
  124.         $command .= ' -o ArtifactResolve';
  125.         $command .= ' -ds ' $this->config['artifact_resolve_ds'];
  126.         $command .= ' -is ' $this->config['is'];
  127.         $command .= ' -af ' $artifact;
  128.         $command .= ' -of xml';
  129. //        $command .= ' -l';
  130.         return $command;
  131.     }
  132.     private function prepareLogoutRequestCommand($nameId$sessionIndex): string
  133.     {
  134.         $command 'java -jar ' $this->config['connector_app_jar'];
  135.         $command .= ' -sks ' $this->config['sig_keystore_path'];
  136.         $command .= ' -sksp ' $this->config['sksp'];
  137.         $command .= ' -o LogoutRequest';
  138.         $command .= ' -is ' $this->config['is'];
  139.         $command .= ' -ds ' $this->config['logout_request_ds'];
  140.         $command .= ' -ni ' $nameId;
  141.         $command .= ' -si ' $sessionIndex;
  142.         $command .= ' -of xml';
  143. //        $command .= ' -l';
  144.         return $command;
  145.     }
  146.     private function prepareAddDocumentToSigningRequestCommand($documentXmlPath$successUrl$failureUrl): string
  147.     {
  148.         $javaOpts '--add-exports=java.xml.crypto/org.jcp.xml.dsig.internal.dom=ALL-UNNAMED -Dfile.encoding=utf-8';
  149.         $command 'java ' $javaOpts ' -jar ' $this->config['connector_app_jar'];
  150.         $command .= ' -sks ' $this->config['epuap_keystore_path'];
  151.         $command .= ' -sksp ' $this->config['epuap_keystore_password'];
  152.         $command .= ' -o AddDocumentToSigning';
  153.         $command .= ' -ds ' $this->config['adddocumenttosigning_request_ds'];
  154.         $command .= ' -fts ' $documentXmlPath;
  155.         $command .= ' -su ' $successUrl;
  156.         $command .= ' -fu ' $failureUrl;
  157.         $command .= ' -of xml';
  158.         //$command .= ' -l';
  159.         return $command;
  160.     }
  161.     private function prepareGetSignedDocumentRequestCommand($documentId$documentXmlPath): string
  162.     {
  163.         $javaOpts '--add-exports=java.xml.crypto/org.jcp.xml.dsig.internal.dom=ALL-UNNAMED -Dfile.encoding=utf-8';
  164.         $command 'java ' $javaOpts ' -jar ' $this->config['connector_app_jar'];
  165.         $command .= ' -sks ' $this->config['epuap_keystore_path'];
  166.         $command .= ' -sksp ' $this->config['epuap_keystore_password'];
  167.         $command .= ' -o GetSignedDocument';
  168.         $command .= ' -ds ' $this->config['getsigneddocument_request_ds'];
  169.         $command .= ' -di ' $documentId;
  170.         $command .= ' -wsf ' $documentXmlPath;
  171.         $command .= ' -of xml';
  172.         //$command .= ' -l';
  173.         return $command;
  174.     }
  175.     private function prepareAddDocumentToSigning(string $unsignedXmlDocumentPath): string
  176.     {
  177.         $javaOpts '--add-exports=java.xml.crypto/org.jcp.xml.dsig.internal.dom=ALL-UNNAMED -Dfile.encoding=utf-8';
  178.         $command 'java ' $javaOpts ' -jar ' $this->config['connector_app_jar'];
  179.         $command .= ' -ll ';
  180.         $command .= ' -sks ' $this->config['epuap_keystore_path'];
  181.         $command .= ' -sksp ' $this->config['epuap_keystore_password'];
  182.         $command .= ' -o AddDocumentToSigning';
  183.         $command .= ' -ds ' $this->config['authn_request_ds'];
  184.         $command .= ' -is ' $this->config['is'];
  185.         $command .= ' -id ' $unsignedXmlDocumentPath;
  186.         $command .= ' -of xml';
  187.         //$command .= ' -l';
  188.         return $command;
  189.     }
  190.     private function prepareEpuapDocumentSendCommand(string $fileToSend, ?string $additionalDatabool $isProbingstring $outputFormat, ?string $documentFileName null): string
  191.     {
  192.         $javaOpts '--add-exports=java.xml.crypto/org.jcp.xml.dsig.internal.dom=ALL-UNNAMED -Dfile.encoding=utf-8';
  193.         $command 'java ' $javaOpts ' -jar ' $this->config['connector_app_jar'];
  194.         $command .= ' -o EpuapDocumentSend';
  195.         // Użyj alternatywnej konfiguracji keystore jeśli jest zdefiniowana, w przeciwnym razie użyj standardowej
  196.         $keystorePath = !empty($this->config['epuapdocumentsend_keystore_path'])
  197.             ? $this->config['epuapdocumentsend_keystore_path']
  198.             : $this->config['epuap_keystore_path'];
  199.         $keystorePassword = !empty($this->config['epuapdocumentsend_keystore_password'])
  200.             ? $this->config['epuapdocumentsend_keystore_password']
  201.             : $this->config['epuap_keystore_password'];
  202.         $payload = [
  203.             'sig_keystore'          => $keystorePath,
  204.             'sig_keystore_password' => $keystorePassword,
  205.             'endpoint_url'          => $this->config['epuapdocumentsend_endpoint_url'],
  206.             'address_from'          => $this->config['epuapdocumentsend_address_from'],
  207.             'address_to'            => $this->config['epuapdocumentsend_address_to'],
  208.             'provider_id'           => $this->config['epuapdocumentsend_provider_id'],
  209.             'file_to_send'          => $fileToSend,
  210.             'output_format'         => $outputFormat,
  211.         ];
  212.         if ($additionalData !== null) {
  213.             $payload['additional_data'] = $additionalData;
  214.         }
  215.         if ($isProbing) {
  216.             $payload['is_probing'] = true;
  217.         }
  218.         if ($documentFileName !== null) {
  219.             $payload['file_name'] = $documentFileName;
  220.         }
  221.         $jsonPayload json_encode($payloadJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  222.         if ($jsonPayload === false) {
  223.             throw new \RuntimeException('Unable to encode ePUAP document send payload as JSON.');
  224.         }
  225.         $command .= ' -p ' base64_encode($jsonPayload);
  226.         return $command;
  227.     }
  228.     private function prepareConfig(ParameterBagInterface $params)
  229.     {
  230.         $projectDir $params->get('kernel.project_dir');
  231.         $config     $params->get('abap_login_gov.client_config', []);
  232.         array_walk($config, function (&$item) use ($projectDir) {
  233.             $item str_replace('%kernel.project_dir%'$projectDir$item);
  234.         });
  235.         return $config;
  236.     }
  237. }