eZ components PHP Cross Reference Developer Tools

Source: /AuthenticationDatabaseTiein/tests/filters/openid/openid_db_store_test.php - 415 lines - 16497 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @filesource
   6   * @package AuthenticationDatabaseTiein
   7   * @version 1.1
   8   * @subpackage Tests
   9   */
  10  
  11  include_once ( 'AuthenticationDatabaseTiein/tests/test.php' );
  12  include_once ( 'Authentication/tests/filters/openid/data/openid_wrapper.php' );
  13  include_once ( 'data/openid_db_store_helper.php' );
  14  
  15  /**
  16   * @package AuthenticationDatabaseTiein
  17   * @version 1.1
  18   * @subpackage Tests
  19   */
  20  class ezcAuthenticationOpenidDbStoreTest extends ezcAuthenticationDatabaseTieinTest
  21  {
  22      protected static $tableNonces = 'openid_nonces';
  23      protected static $tableAssociations = 'openid_associations';
  24  
  25      protected static $nonce1 = '123456';
  26      protected static $nonce2 = '999999';
  27      protected static $urlServer = 'http://www.myopenid.com/server';
  28      protected static $url = 'http://ezc.myopenid.com/';
  29  
  30      public static $provider = "http://www.myopenid.com/server";
  31  
  32      public static $requestCheckAuthenticationGet = array(
  33          'openid_assoc_handle' => '{HMAC-SHA1}{4640581a}{3X/rrw==}',
  34          'openid_signed' => 'return_to,mode,identity',
  35          'openid_sig' => 'SkaCB2FA9EysKoDkybyBD46zb0E=',
  36          'openid_return_to' => 'http://localhost',
  37          'openid_identity' => 'http://ezc.myopenid.com',
  38          'openid_op_endpoint' => 'http://www.myopenid.com/server',
  39          'openid_mode' => 'check_authentication',
  40          );
  41  
  42      public static $requestSmartGet = array(
  43         'nonce' => '770890',
  44         'openid_assoc_handle' => '{HMAC-SHA1}{465d8eb9}{NQN84Q==}',
  45         'openid_signed' => 'assoc_handle,identity,mode,op_endpoint,response_nonce,return_to,signed',
  46         'openid_sig' => 'HkLMUymWy3/GmHWVuWYOs9IHkrs=',
  47         'openid_mode' => 'id_res',
  48         'openid_identity' => 'http://ezc.myopenid.com/',
  49         'openid_op_endpoint' => 'http://www.myopenid.com/server',
  50         'openid_response_nonce' => '2007-05-31T08:33:59ZLdyyJF',
  51         'openid_return_to' => 'http://localhost/openid.php?action=login&openid_identifier=http%3A%2F%2Fezc.myopenid.com&nonce=770890',
  52         );
  53  
  54      public static $association;
  55      public static $associationArray;
  56      
  57      public static $server = array(
  58          'HTTP_HOST' => 'localhost',
  59          'REQUEST_URI' => '/openid.php?action=login&openid_identifier=http%3A%2F%2Fezc.myopenid.com',
  60          );
  61  
  62      public static function suite()
  63      {
  64          self::$associationArray = array(
  65              'handle' => '{HMAC-SHA1}{465d66d3}{6K1aSw==}',
  66              'secret' => 'W0ixM9SYQviSG9TF6HrnXaxHudQ=',
  67              'issued' => time(),
  68              'validity' => 1209600,
  69              'type' => 'HMAC-SHA1',
  70              );
  71  
  72          self::$association = new ezcAuthenticationOpenidAssociation( '{HMAC-SHA1}{465d8eb9}{NQN84Q==}',
  73                                                                       'foz3UXCxQJ5lKvau78Oqen9dTUc=',
  74                                                                       1180536597,
  75                                                                       time() - 1180536597 + 604800, // valid 1 week from current time
  76                                                                       'HMAC-SHA1' );
  77  
  78          return new PHPUnit_Framework_TestSuite( __CLASS__ );
  79      }
  80  
  81      public function setUp()
  82      {
  83          $_GET = null;
  84          $_SERVER = self::$server;
  85  
  86          try
  87          {
  88              $this->db = ezcDbInstance::get();
  89  
  90              $schema = ezcDbSchema::createFromFile(
  91                                                     'array',
  92                                                     dirname( __FILE__ ) . '/../../../docs/tutorial/openid_db_store_schema.dba'
  93                                                   );
  94              $schema->writeToDb( $this->db );
  95          }
  96  
  97          catch ( Exception $e )
  98          {
  99              $this->markTestSkipped( "You must provide a database to runtests.php: " . $e->getMessage() );
 100          }
 101  
 102      }
 103  
 104      public function tearDown()
 105      {
 106          if ( $this->db !== null )
 107          {
 108              $this->cleanupTables();
 109          }
 110  
 111          $this->db = null;
 112      }
 113  
 114      public function cleanupTables()
 115      {
 116          $this->db->exec( 'DROP TABLE ' . $this->db->quoteIdentifier( self::$tableNonces ) );
 117          $this->db->exec( 'DROP TABLE ' . $this->db->quoteIdentifier( self::$tableAssociations ) );
 118      }
 119  
 120      public function testOpenidDbStoreStoreNonceNormal()
 121      {
 122          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 123  
 124          $nonce = self::$nonce1;
 125          $store->storeNonce( $nonce );
 126          $this->assertEquals( true, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 127      }
 128  
 129      public function testOpenidDbStoreStoreNonceExistent()
 130      {
 131          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 132  
 133          $nonce = self::$nonce1;
 134          $store->storeNonce( $nonce );
 135          $store->storeNonce( $nonce );
 136          $this->assertEquals( true, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 137      }
 138  
 139      public function testOpenidDbStoreUseNonceStillValid()
 140      {
 141          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 142  
 143          $nonce = self::$nonce1;
 144          $store->storeNonce( $nonce );
 145          $this->assertEquals( true, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 146  
 147          $ret = $store->useNonce( $nonce );
 148          $this->assertEquals( true, abs( time() - $ret ) < 10 ); // to allow for delays in the program
 149          $this->assertEquals( false, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 150      }
 151  
 152      public function testOpenidDbStoreUseNonceNonexistent()
 153      {
 154          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 155  
 156          $nonce = self::$nonce1;
 157          $store->storeNonce( $nonce );
 158  
 159          $this->assertEquals( true, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 160  
 161          ezcAuthenticationOpenidDbStoreHelper::deleteNonce( $this->db, $nonce );
 162  
 163          $this->assertEquals( false, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 164  
 165          $ret = $store->useNonce( $nonce );
 166          $this->assertEquals( false, $ret );
 167          $this->assertEquals( false, in_array( $nonce, ezcAuthenticationOpenidDbStoreHelper::getNonces( $this->db ) ) );
 168      }
 169  
 170      public function testOpenidDbStoreStoreAssociationNormal()
 171      {
 172          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 173  
 174          $association = ezcAuthenticationOpenidAssociation::__set_state( self::$associationArray );
 175          $url = self::$urlServer;
 176          $store->storeAssociation( $url, $association );
 177  
 178          $data = ezcAuthenticationOpenidDbStoreHelper::getAssociations( $this->db, $url );
 179  
 180          $this->assertEquals( unserialize( $data ), $store->getAssociation( $url ) );
 181      }
 182  
 183      public function testOpenidDbStoreStoreAssociationExistent()
 184      {
 185          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 186  
 187          $association = ezcAuthenticationOpenidAssociation::__set_state( self::$associationArray );
 188          $url = self::$urlServer;
 189          $store->storeAssociation( $url, $association );
 190          $store->storeAssociation( $url, $association );
 191          $data = ezcAuthenticationOpenidDbStoreHelper::getAssociations( $this->db, $url );
 192  
 193          $this->assertEquals( unserialize( $data ), $store->getAssociation( $url ) );
 194      }
 195  
 196      public function testOpenidDbStoreRemoveAssociationNormal()
 197      {
 198          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 199  
 200          $association = ezcAuthenticationOpenidAssociation::__set_state( self::$associationArray );
 201          $url = self::$urlServer;
 202          $store->storeAssociation( $url, $association );
 203          $data = ezcAuthenticationOpenidDbStoreHelper::getAssociations( $this->db, $url );
 204  
 205          $this->assertEquals( unserialize( $data ), $store->getAssociation( $url ) );
 206  
 207          $this->assertEquals( true, $store->removeAssociation( $url ) );
 208          $this->assertEquals( false, $store->getAssociation( $url ) );
 209      }
 210  
 211      public function testOpenidDbStoreRemoveAssociationNonexistent()
 212      {
 213          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 214  
 215          $association = ezcAuthenticationOpenidAssociation::__set_state( self::$associationArray );
 216          $url = self::$urlServer;
 217  
 218          // for DbStore, removeAssociation() returns always true, but for FileStore it could be false
 219          $this->assertEquals( true, $store->removeAssociation( $url ) );
 220  
 221          $this->assertEquals( false, $store->getAssociation( $url ) );
 222      }
 223  
 224      public function testOpenidWrapperRunModeIdResSmartModeDbStore()
 225      {
 226          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 227          {
 228              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 229          }
 230  
 231          $_GET = self::$requestCheckAuthenticationGet;
 232          $_GET['openid_mode'] = 'id_res';
 233          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 234  
 235          $options = new ezcAuthenticationOpenidOptions();
 236          $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
 237          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 238  
 239          $filter = new ezcAuthenticationOpenidWrapper( $options );
 240          $result = $filter->run( $credentials );
 241          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_SIGNATURE_INCORRECT, $result );
 242      }
 243  
 244      public function testOpenidWrapperRunModeIdResSmartModeDbStoreExistent()
 245      {
 246          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 247          {
 248              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 249          }
 250  
 251          $params = array(
 252              'openid.mode' => 'associate',
 253              'openid.assoc_type' => 'HMAC-SHA1',
 254              );
 255  
 256          $filter = new ezcAuthenticationOpenidWrapper();
 257          $res = $filter->associate( self::$provider, $params );
 258          $secret = isset( $res['enc_mac_key'] ) ? $res['enc_mac_key'] : $res['mac_key'];
 259          $association = new ezcAuthenticationOpenidAssociation( $res['assoc_handle'],
 260                                                                 $secret,
 261                                                                 time(),
 262                                                                 $res['expires_in'],
 263                                                                 $res['assoc_type'] );
 264  
 265          $_GET = self::$requestCheckAuthenticationGet;
 266          $_GET['openid_mode'] = 'id_res';
 267          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 268  
 269          $options = new ezcAuthenticationOpenidOptions();
 270          $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
 271          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 272          $options->store->storeAssociation( self::$provider, $association );
 273  
 274          $filter = new ezcAuthenticationOpenidWrapper( $options );
 275          $result = $filter->run( $credentials );
 276          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_SIGNATURE_INCORRECT, $result );
 277      }
 278  
 279      public function testOpenidWrapperRunModeIdResSmartModeDbStoreExistentCorrect()
 280      {
 281          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 282          {
 283              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 284          }
 285  
 286          $association = self::$association;
 287          $_GET = self::$requestSmartGet;
 288          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 289  
 290          $options = new ezcAuthenticationOpenidOptions();
 291          $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
 292          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 293          $options->store->storeAssociation( self::$provider, $association );
 294          $options->store->storeNonce( $_GET['nonce'] );
 295  
 296          $filter = new ezcAuthenticationOpenidWrapper( $options );
 297          $result = $filter->run( $credentials );
 298  
 299          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_OK, $result );
 300      }
 301  
 302      public function testOpenidWrapperRunModeIdResDbStore()
 303      {
 304          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 305          {
 306              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 307          }
 308  
 309          $_GET = self::$requestCheckAuthenticationGet;
 310          $_GET['openid_mode'] = 'id_res';
 311          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 312  
 313          $options = new ezcAuthenticationOpenidOptions();
 314          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 315  
 316          $filter = new ezcAuthenticationOpenidWrapper( $options );
 317          $result = $filter->run( $credentials );
 318          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_SIGNATURE_INCORRECT, $result );
 319      }
 320  
 321      public function testOpenidWrapperRunModeIdResDbStoreExistent()
 322      {
 323          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 324          {
 325              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 326          }
 327  
 328          $_GET = self::$requestCheckAuthenticationGet;
 329          $_GET['openid_mode'] = 'id_res';
 330          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 331  
 332          $options = new ezcAuthenticationOpenidOptions();
 333          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 334  
 335          $filter = new ezcAuthenticationOpenidWrapper( $options );
 336          $result = $filter->run( $credentials );
 337          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_SIGNATURE_INCORRECT, $result );
 338      }
 339  
 340      public function testOpenidWrapperRunModeIdResDbStoreNonceValid()
 341      {
 342          if ( !ezcBaseFeatures::hasExtensionSupport( 'openssl' ) )
 343          {
 344              $this->markTestSkipped( 'PHP must be compiled with --with-openssl.' );
 345          }
 346  
 347          $_GET = self::$requestCheckAuthenticationGet;
 348          $_GET['openid_mode'] = 'id_res';
 349          $nonce = '123456';
 350          $_GET['openid_return_to'] = ezcAuthenticationUrl::appendQuery( $_GET['openid_return_to'], 'nonce', $nonce );
 351  
 352          $options = new ezcAuthenticationOpenidOptions();
 353          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 354          $options->store->storeNonce( $nonce );
 355  
 356          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 357  
 358          $filter = new ezcAuthenticationOpenidWrapper( $options );
 359          $result = $filter->run( $credentials );
 360          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_SIGNATURE_INCORRECT, $result );
 361      }
 362  
 363      public function testOpenidWrapperRunModeIdResDbStoreNonceInvalid()
 364      {
 365          $_GET = self::$requestCheckAuthenticationGet;
 366          $_GET['openid_mode'] = 'id_res';
 367          $nonce = '123456';
 368          $_GET['openid_return_to'] = ezcAuthenticationUrl::appendQuery( $_GET['openid_return_to'], 'nonce', $nonce );
 369  
 370          $options = new ezcAuthenticationOpenidOptions();
 371          $options->store = new ezcAuthenticationOpenidDbStore( $this->db );
 372  
 373          $credentials = new ezcAuthenticationIdCredentials( self::$url );
 374  
 375          $filter = new ezcAuthenticationOpenidWrapper( $options );
 376          $result = $filter->run( $credentials );
 377  
 378          $this->assertEquals( ezcAuthenticationOpenidFilter::STATUS_NONCE_INCORRECT, $result );
 379      }
 380  
 381      public function testOpenidDbStoreOptions()
 382      {
 383          $options = new ezcAuthenticationOpenidDbStoreOptions();
 384  
 385          $this->invalidPropertyTest( $options, 'tableNonces', 'wrong value', 'array' );
 386          $this->invalidPropertyTest( $options, 'tableAssociations', 'wrong value', 'array' );
 387          $this->missingPropertyTest( $options, 'no_such_property' );
 388      }
 389  
 390      public function testOpenidDbStoreOptionsGetSet()
 391      {
 392          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 393  
 394          $options = new ezcAuthenticationOpenidDbStoreOptions();
 395          $store->setOptions( $options );
 396          $this->assertEquals( $options, $store->getOptions() );
 397      }
 398  
 399      public function testOpenidDbStoreProperties()
 400      {
 401          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 402  
 403          $this->invalidPropertyTest( $store, 'instance', 'wrong value', 'ezcDbHandler' );
 404          $this->missingPropertyTest( $store, 'no_such_property' );
 405      }
 406  
 407      public function testOpenidDbStorePropertiesIsSet()
 408      {
 409          $store = new ezcAuthenticationOpenidDbStore( $this->db );
 410  
 411          $this->issetPropertyTest( $store, 'instance', true );
 412          $this->issetPropertyTest( $store, 'no_such_property', false );
 413      }
 414  }
 415  ?>

title

Description

title

Description

title

Description

title

title

Body