<commit_before><?php

namespace CommerceSignals\Auth;

use CommerceSignals\Auth\JWT;
use CommerceSignals\Exceptions\AuthException;

use \DateTime;
use \DateTimeZone;
use \DateInterval;

class AuthToken {
  const API_SEGMENT = '/oauth/token';
  const ACCESS_TOKEN_MINUTES = 10;

  public $token;
  private $endpoint;
  private $apiKey;
  private $cert;

  public function __construct($apiKey, $cert, $apiBase) {
    $this->endpoint = $apiBase . SELF::API_SEGMENT;
    $this->cert = $cert;
    $this->apiKey = $apiKey;
  }

  /**
   * Make a request to get an access token using a signed JWT
   */
  public function request() {
    $signedJWT = $this->signJWT();

    $postParams = [
      'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      'assertion' => $signedJWT
    ];

    $curl = curl_init();

    curl_setopt_array($curl, [
      CURLOPT_URL => $this->endpoint,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => http_build_query($postParams),
      CURLOPT_USERPWD => '<PASSWORD>:',
      CURLOPT_RETURNTRANSFER => true
    ]);

    $result = curl_exec($curl);
    curl_close($curl);

    $this->token = json_decode($result);

    if (property_exists($this->token, 'error')) {
      $error = $this->accessToken->error . ': ' . $this->accessToken->error_description;
      throw new AuthException($error);
    }
  }

  /**
   * Create a signed JWT token
   */<commit_msg>added debugging info to curl calls<commit_after><?php

namespace CommerceSignals\Auth;

use CommerceSignals\Auth\JWT;
use CommerceSignals\Exceptions\AuthException;

use \DateTime;
use \DateTimeZone;
use \DateInterval;

class AuthToken {
  const API_SEGMENT = '/oauth/token';
  const ACCESS_TOKEN_MINUTES = 10;

  public $token;
  private $endpoint;
  private $apiKey;
  private $cert;

  public function __construct($apiKey, $cert, $apiBase) {
    $this->endpoint = $apiBase . SELF::API_SEGMENT;
    $this->cert = $cert;
    $this->apiKey = $apiKey;
  }

  /**
   * Make a request to get an access token using a signed JWT
   */
  public function request() {
    $signedJWT = $this->signJWT();

    $postParams = [
      'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      'assertion' => $signedJWT
    ];

    ob_start();
    $out = fopen('php://output', 'w');

    $curl = curl_init();

    curl_setopt_array($curl, [
      CURLOPT_URL => $this->endpoint,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => http_build_query($postParams),
      CURLOPT_USERPWD => '<PASSWORD>:',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_VERBOSE => true,
      CURLOPT_STDERR => $out,
    ]);

    $result = curl_exec($curl);
    curl_close($curl);

    fclose($out);
    $debug = ob_get_clean();

    $this->token = json_decode($result);

    if (property_exists($this->token, 'error')) {
      $error = $this->accessToken->error . ': ' . $this->accessToken->error_description;
      throw new AuthException($error);
    }
  }

  /**
   * Create a signed JWT token
   */
{'fraction_non_alphanumeric': 0.14697020421827922, 'fraction_numerical': 0.0013391362571141614, 'mean_word_length': 3.2624821683309557, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5511229', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1023, 'n_words': 206}