You are here

function MiniorangeOAuthCustomer::verifyLicense in Drupal OAuth & OpenID Connect Login - OAuth2 Client SSO Login 7

File

includes/customer_setup.php, line 310
Contains miniOrange Customer class.

Class

MiniorangeOAuthCustomer
@file This class represents configuration for customer.

Code

function verifyLicense($code) {
  $url = MiniorangeOAuthConstants::BASE_URL . '/moas/api/backupcode/verify';
  $ch = curl_init($url);

  /* The customer Key provided to you */
  $customerKey = variable_get('miniorange_oauth_client_customer_id');

  /* The customer API Key provided to you */
  $apiKey = variable_get('miniorange_oauth_client_customer_api_key');
  global $base_url;

  /* Current time in milliseconds since midnight, January 1, 1970 UTC. */
  $currentTimeInMillis = round(microtime(TRUE) * 1000);

  /* Creating the Hash using SHA-512 algorithm */
  $stringToHash = $customerKey . number_format($currentTimeInMillis, 0, '', '') . $apiKey;
  $hashValue = hash("sha512", $stringToHash);
  $customerKeyHeader = "Customer-Key: " . $customerKey;
  $timestampHeader = "Timestamp: " . number_format($currentTimeInMillis, 0, '', '');
  $authorizationHeader = "Authorization: " . $hashValue;
  $fields = '';

  // *check for otp over sms/email
  $fields = array(
    'code' => $code,
    'customerKey' => $customerKey,
    'additionalFields' => array(
      'field1' => $base_url,
    ),
  );
  $field_string = json_encode($fields);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  // required for https urls
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    $customerKeyHeader,
    $timestampHeader,
    $authorizationHeader,
  ));
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $field_string);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  $content = curl_exec($ch);
  if (curl_errno($ch)) {
    echo 'Request Error:' . curl_error($ch);
    exit;
  }
  curl_close($ch);
  return $content;
}