Advertisement
nicolasb827

OpenNebula server_cipher example

Mar 26th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. require_once BASE_PATH . "lib/crypt/AES.php";
  2.  
  3. DEFINE ("AES_256_CBC_KEYSIZE", 32);
  4. DEFINE ("AES_256_CBC_IVSIZE", 16);
  5.  
  6. class CloudTest extends PHPUnit_Framework_TestCase {
  7.         protected $f3;
  8.         protected $oca_base_url;
  9.         protected $oca_username;
  10.         protected $oca_password;
  11.         protected $user_id;
  12.  
  13.         function setUp() {
  14.                 $this->f3 = base::instance();
  15.                 $this->oca_base_url = $this->f3->get('cloud_service');
  16.                 $this->oca_username = $this->f3->get('cloud_username');
  17.                 $this->oca_password = $this->f3->get('cloud_password');
  18.                 $this->user_id= "guest_user_id";
  19.         }
  20.  
  21.     /** return expiration time .. should be better */
  22.         function giveExpire() {
  23.                 return (string)(time()+3600);
  24.         }
  25.    
  26.     /** return a key from a truncated SHA1 hexhash */
  27.         function giveKey() {
  28.                 return substr(sha1($this->oca_password), 0, AES_256_CBC_KEYSIZE);
  29.         }
  30.  
  31.          /** return an Auth Token for OpenNebula
  32.          */
  33.         function getAuthToken() {
  34.                 $key = $this->giveKey();
  35.                 $data = $this->oca_username . ":" . $this->user_id. ":" . $this->giveExpire();
  36.                 $cipher = new Crypt_AES();
  37.                 $cipher->setKey($key);
  38.         // WARNING: OpenNebula does not set any IV
  39.                 return $this->oca_username . ":" . $this->user_id. ":" . base64_encode($cipher->encrypt($data));
  40.         }
  41.  
  42.         /**
  43.          * @group XMLRPC
  44.          */
  45.         function test_request_rpc_1() {
  46.                 // build userAuth
  47.                 $userAuth =     $this->getAuthToken();
  48.                 $request = xmlrpc_encode_request("one.vmpool.info", array($userAuth, -2, -1, -1 , -1));
  49.                 $content = stream_context_create(array(
  50.                                 "http" => array("method" => "POST",
  51.                                                 "header" => "Content-Type: text/xml",
  52.                                                 "content" => $request
  53.                                 )
  54.                 ));
  55.                 $file = file_get_contents($this->oca_base_url, false, $content);
  56.                 $response = xmlrpc_decode($file);
  57.                 $this->assertTrue(isset($response), "Unset reply from RPCServer");
  58.                 $this->assertTrue($response[0], "Invalid reply from RPCServer");
  59.         }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement