Zend_Gdata_HttpClient::setAuthSubPrivateKeyFile PHP Method

setAuthSubPrivateKeyFile() public method

This method reads the file and then calls setAuthSubPrivateKey() with the file contents.
public setAuthSubPrivateKeyFile ( string $file, string $passphrase = null, boolean $useIncludePath = false ) : void
$file string The location of the file containing the PEM key
$passphrase string The optional private key passphrase
$useIncludePath boolean Whether to search the include_path for the file
return void
    public function setAuthSubPrivateKeyFile($file, $passphrase = null, $useIncludePath = false)
    {
        $fp = @fopen($file, "r", $useIncludePath);
        if (!$fp) {
            require_once 'Zend/Gdata/App/InvalidArgumentException.php';
            throw new Zend_Gdata_App_InvalidArgumentException('Failed to open private key file for AuthSub.');
        }
        $key = '';
        while (!feof($fp)) {
            $key .= fread($fp, 8192);
        }
        $this->setAuthSubPrivateKey($key, $passphrase);
        fclose($fp);
    }

Usage Example

Example #1
0
 public function testSecureAuthSubSigning()
 {
     if (!extension_loaded('openssl')) {
         $this->markTestSkipped('The openssl extension is not available');
     } else {
         $c = new Zend_Gdata_HttpClient();
         $c->setAuthSubPrivateKeyFile("Zend/Gdata/_files/RsaKey.pem", null, true);
         $c->setAuthSubToken('abcdefg');
         $requestData = $c->filterHttpRequest('POST', 'http://www.example.com/feed', array(), 'foo bar', 'text/plain');
         $authHeaderCheckPassed = false;
         $headers = $requestData['headers'];
         foreach ($headers as $headerName => $headerValue) {
             if (strtolower($headerName) == 'authorization') {
                 preg_match('/data="([^"]*)"/', $headerValue, $matches);
                 $dataToSign = $matches[1];
                 preg_match('/sig="([^"]*)"/', $headerValue, $matches);
                 $sig = $matches[1];
                 if (function_exists('openssl_verify')) {
                     $fp = fopen('Zend/Gdata/_files/RsaCert.pem', 'r', true);
                     $cert = '';
                     while (!feof($fp)) {
                         $cert .= fread($fp, 8192);
                     }
                     fclose($fp);
                     $pubkeyid = openssl_get_publickey($cert);
                     $verified = openssl_verify($dataToSign, base64_decode($sig), $pubkeyid);
                     $this->assertEquals(1, $verified, 'The generated signature was unable ' . 'to be verified.');
                     $authHeaderCheckPassed = true;
                 }
             }
         }
         $this->assertEquals(true, $authHeaderCheckPassed, 'Auth header not found for sig verification.');
     }
 }
All Usage Examples Of Zend_Gdata_HttpClient::setAuthSubPrivateKeyFile