You are here

public function Lockr::createCertClient in Lockr 7.3

File

vendor/lockr/lockr/src/Lockr.php, line 35

Class

Lockr

Namespace

Lockr

Code

public function createCertClient($client_token, array $dn) {
  $key = openssl_pkey_new([
    'private_key_bits' => 2048,
  ]);
  if ($key === false) {
    throw new RuntimeException('Could not create private key.');
  }
  if (!openssl_pkey_export($key, $key_text)) {
    throw new RuntimeException('Could not export private key.');
  }
  $csr = openssl_csr_new($dn, $key);
  if ($csr === false) {
    throw new RuntimeException('Could not create CSR.');
  }
  if (!openssl_csr_export($csr, $csr_text)) {
    throw new RuntimeException('Could not export CSR.');
  }
  $query = <<<'EOQ'
mutation CreateCertClient($input: CreateCertClient!) {
  createCertClient(input: $input) {
    env
    auth {
      ... on LockrCert {
        certText
      }
    }
  }
}
EOQ;
  $t0 = microtime(true);
  $data = $this->client
    ->query([
    'query' => $query,
    'variables' => [
      'input' => [
        'token' => $client_token,
        'csrText' => $csr_text,
      ],
    ],
  ]);
  $t1 = microtime(true);
  $this->client
    ->getStats()
    ->lockrCallCompleted('create_cert_client', $t1 - $t0);
  return [
    'key_text' => $key_text,
    'cert_text' => $data['createCertClient']['auth']['certText'],
    'env' => $data['createCertClient']['env'],
  ];
}