MonsterInsights_GA_Lib_Signer_P12

Signs data.

Defined (1)

The class is defined in the following location(s).

/assets/lib/google/src/Google/Signer/P12.php  
  1. class MonsterInsights_GA_Lib_Signer_P12 extends MonsterInsights_GA_Lib_Signer_Abstract 
  2. // OpenSSL private key resource 
  3. private $privateKey; 
  4.  
  5. // Creates a new signer from a .p12 file. 
  6. public function __construct($p12, $password) 
  7. if (!function_exists('openssl_x509_read')) { 
  8. throw new MonsterInsights_GA_Lib_Exception( 
  9. 'The Google PHP API library needs the openssl PHP extension' 
  10. ); 
  11.  
  12. // If the private key is provided directly, then this isn't in the p12 
  13. // format. Different versions of openssl support different p12 formats 
  14. // and the key from google wasn't being accepted by the version available 
  15. // at the time. 
  16. if (!$password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) { 
  17. $this->privateKey = openssl_pkey_get_private($p12); 
  18. } elseif ($password === 'notasecret' && strpos($p12, "-----BEGIN PRIVATE KEY-----") !== false) { 
  19. $this->privateKey = openssl_pkey_get_private($p12); 
  20. } else { 
  21. // This throws on error 
  22. $certs = array(); 
  23. if (!openssl_pkcs12_read($p12, $certs, $password)) { 
  24. throw new MonsterInsights_GA_Lib_Auth_Exception( 
  25. "Unable to parse the p12 file. " . 
  26. "Is this a .p12 file? Is the password correct? OpenSSL error: " . 
  27. openssl_error_string() 
  28. ); 
  29. // TODO(beaton): is this part of the contract for the openssl_pkcs12_read 
  30. // method? What happens if there are multiple private keys? Do we care? 
  31. if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) { 
  32. throw new MonsterInsights_GA_Lib_Auth_Exception("No private key found in p12 file."); 
  33. $this->privateKey = openssl_pkey_get_private($certs['pkey']); 
  34.  
  35. if (!$this->privateKey) { 
  36. throw new MonsterInsights_GA_Lib_Auth_Exception("Unable to load private key"); 
  37.  
  38. public function __destruct() 
  39. if ($this->privateKey) { 
  40. openssl_pkey_free($this->privateKey); 
  41.  
  42. public function sign($data) 
  43. if (version_compare(PHP_VERSION, '5.3.0') < 0) { 
  44. throw new MonsterInsights_GA_Lib_Auth_Exception( 
  45. "PHP 5.3.0 or higher is required to use service accounts." 
  46. ); 
  47. $hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256"; 
  48. if (!openssl_sign($data, $signature, $this->privateKey, $hash)) { 
  49. throw new MonsterInsights_GA_Lib_Auth_Exception("Unable to sign data"); 
  50. return $signature;