PageRenderTime 85ms CodeModel.GetById 2ms app.highlight 71ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/Zend/OpenId/ProviderTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 1645 lines | 1344 code | 124 blank | 177 comment | 14 complexity | 7fc4d817b547af1206c708c7bf6bab3f MD5 | raw file
   1<?php
   2/**
   3 * Zend Framework
   4 *
   5 * LICENSE
   6 *
   7 * This source file is subject to the new BSD license that is bundled
   8 * with this package in the file LICENSE.txt.
   9 * It is also available through the world-wide-web at this URL:
  10 * http://framework.zend.com/license/new-bsd
  11 * If you did not receive a copy of the license and are unable to
  12 * obtain it through the world-wide-web, please send an email
  13 * to license@zend.com so we can send you a copy immediately.
  14 *
  15 * @category   Zend
  16 * @package    Zend_OpenId
  17 * @subpackage UnitTests
  18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20 * @version    $Id: ProviderTest.php 24593 2012-01-05 20:35:02Z matthew $
  21 */
  22
  23/**
  24 * Zend_OpenId
  25 */
  26require_once 'Zend/OpenId/Provider.php';
  27
  28/**
  29 * Zend_OpenId_ResponseHelper
  30 */
  31require_once 'Zend/OpenId/ResponseHelper.php';
  32
  33/**
  34 * @see Zend_OpenId_Provider_User_Session
  35 */
  36require_once 'Zend/OpenId/Provider/User/Session.php';
  37
  38/**
  39 * @category   Zend
  40 * @package    Zend_OpenId
  41 * @subpackage UnitTests
  42 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  43 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  44 * @group      Zend_OpenId
  45 */
  46class Zend_OpenId_ProviderTest extends PHPUnit_Framework_TestCase
  47{
  48    const USER     = "http://test_user.myopenid.com/";
  49    const PASSWORD = "01234567890abcdef";
  50
  51    const HANDLE   = "01234567890abcdef";
  52
  53    private $_user;
  54
  55    public function __construct($name = NULL, array $data = array(), $dataName = '')
  56    {
  57        parent::__construct($name, $data, $dataName);
  58        $this->_user = new Zend_OpenId_Provider_User_Session();
  59    }
  60
  61    /**
  62     * testing register
  63     *
  64     */
  65    public function testRegister()
  66    {
  67        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
  68        $storage->delUser(self::USER);
  69        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
  70        $this->assertFalse( $storage->checkUser(self::USER, self::PASSWORD) );
  71
  72        // wrong ID
  73        $this->assertFalse( $provider->register("", self::PASSWORD) );
  74        // registration of new user
  75        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
  76        // registration of existent user
  77        $this->assertFalse( $provider->register(self::USER, self::PASSWORD) );
  78
  79        $this->assertTrue( $storage->checkUser(self::USER, md5(self::USER . self::PASSWORD)) );
  80        $storage->delUser(self::USER);
  81    }
  82
  83    /**
  84     * testing hasUser
  85     *
  86     */
  87    public function testHasUser()
  88    {
  89        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
  90        $storage->delUser(self::USER);
  91        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
  92
  93        // wrong ID
  94        $this->assertFalse( $provider->hasUser("") );
  95        $this->assertFalse( $provider->hasUser("http://:80/test") );
  96
  97        // check for non existent
  98        $this->assertFalse( $provider->hasUser(self::USER) );
  99
 100        // check for existent user
 101        $this->assertTrue( $storage->addUser(self::USER, self::PASSWORD) );
 102        $this->assertTrue( $provider->hasUser(self::USER) );
 103
 104        $storage->delUser(self::USER);
 105    }
 106
 107    /**
 108     * testing login
 109     *
 110     */
 111    public function testLogin()
 112    {
 113        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 114        $storage->delUser(self::USER);
 115        $this->_user->delLoggedInUser();
 116        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 117
 118        // wrong ID
 119        $this->assertFalse( $provider->login("", self::PASSWORD) );
 120        $this->assertFalse( $this->_user->getLoggedInUser() );
 121        $this->assertFalse( $provider->login("http://:80/test", self::PASSWORD) );
 122        $this->assertFalse( $this->_user->getLoggedInUser() );
 123
 124        // login as non existent user
 125        $this->assertFalse( $provider->login(self::USER, self::PASSWORD) );
 126        $this->assertFalse( $this->_user->getLoggedInUser() );
 127        // login as existent user with wrong password
 128        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 129        $this->assertFalse( $provider->login(self::USER, self::PASSWORD . "x") );
 130        $this->assertFalse( $this->_user->getLoggedInUser() );
 131        // login as existent user with proper password
 132        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 133        $this->assertSame( self::USER, $this->_user->getLoggedInUser() );
 134
 135        $storage->delUser(self::USER);
 136        $this->_user->delLoggedInUser();
 137    }
 138
 139    /**
 140     * testing logout
 141     *
 142     */
 143    public function testLogout()
 144    {
 145        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 146        $storage->delUser(self::USER);
 147        $this->_user->delLoggedInUser();
 148        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 149
 150        $this->assertFalse( $this->_user->getLoggedInUser() );
 151        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 152        $this->assertFalse( $this->_user->getLoggedInUser() );
 153        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 154        $this->assertSame( self::USER, $this->_user->getLoggedInUser() );
 155        $this->assertTrue( $provider->logout() );
 156        $this->assertFalse( $this->_user->getLoggedInUser() );
 157
 158        $storage->delUser(self::USER);
 159        $this->_user->delLoggedInUser();
 160    }
 161
 162    /**
 163     * testing logout
 164     *
 165     */
 166    public function testLoggedInUser()
 167    {
 168        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 169        $storage->delUser(self::USER);
 170        $this->_user->delLoggedInUser();
 171        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 172
 173        $this->assertFalse( $provider->getLoggedInUser() );
 174        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 175        $this->assertFalse( $provider->getLoggedInUser() );
 176        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 177        $this->assertSame( self::USER, $this->_user->getLoggedInUser() );
 178        $this->assertTrue( $provider->logout() );
 179        $this->assertFalse( $provider->getLoggedInUser() );
 180
 181        $storage->delUser(self::USER);
 182        $this->_user->delLoggedInUser();
 183    }
 184
 185    /**
 186     * testing getSiteRoot
 187     *
 188     */
 189    public function testGetSiteRoot()
 190    {
 191        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 192        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 193
 194        $params = array(
 195            'openid_realm'      => "http://wrong/",
 196            'openid_trust_root' => "http://root/",
 197            'openid_return_to'  => "http://wrong/",
 198        );
 199        $this->assertSame( "http://root/", $provider->getSiteRoot($params) );
 200
 201        $params = array(
 202            'openid_realm'      => "http://wrong/",
 203            'openid_return_to'  => "http://root/",
 204        );
 205        $this->assertSame( "http://root/", $provider->getSiteRoot($params) );
 206
 207        $params = array(
 208            'openid_realm'      => "http://wrong/",
 209        );
 210        $this->assertFalse( $provider->getSiteRoot($params) );
 211
 212        $params = array(
 213            'openid_ns'         => Zend_OpenId::NS_2_0,
 214            'openid_realm'      => "http://root/",
 215            'openid_trust_root' => "http://wrong/",
 216            'openid_return_to'  => "http://wrong/",
 217        );
 218        $this->assertSame( "http://root/", $provider->getSiteRoot($params) );
 219
 220        $params = array(
 221            'openid_ns'         => Zend_OpenId::NS_2_0,
 222            'openid_trust_root' => "http://wrong/",
 223            'openid_return_to'  => "http://root/",
 224        );
 225        $this->assertSame( "http://root/", $provider->getSiteRoot($params) );
 226
 227        $params = array(
 228            'openid_ns'         => Zend_OpenId::NS_2_0,
 229            'openid_return_to'  => "http://root/",
 230        );
 231        $this->assertSame( "http://root/", $provider->getSiteRoot($params) );
 232
 233        $params = array(
 234            'openid_ns'         => Zend_OpenId::NS_2_0,
 235        );
 236        $this->assertFalse( $provider->getSiteRoot($params) );
 237
 238        $params = array(
 239            'openid_trust_root' => "",
 240        );
 241        $this->assertFalse( $provider->getSiteRoot($params) );
 242    }
 243
 244    /**
 245     * testing allowSite
 246     *
 247     */
 248    public function testAllowSite()
 249    {
 250        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 251        $storage->delUser(self::USER);
 252        $this->_user->delLoggedInUser();
 253        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 254
 255        // not logged in
 256        $this->assertFalse( $provider->allowSite("http://www.test.com/") );
 257        // logged in
 258        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 259        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 260        $this->assertTrue( $provider->allowSite("http://www.test.com/") );
 261
 262        $trusted = $storage->getTrustedSites(self::USER);
 263        $this->assertTrue( is_array($trusted) );
 264        $this->assertSame( 1, count($trusted) );
 265        reset($trusted);
 266        $this->assertSame( "http://www.test.com/", key($trusted) );
 267        $this->assertSame( true, current($trusted) );
 268
 269        // duplicate
 270        $this->assertTrue( $provider->allowSite("http://www.test.com/") );
 271
 272        $trusted = $storage->getTrustedSites(self::USER);
 273        $this->assertTrue( is_array($trusted) );
 274        $this->assertSame( 1, count($trusted) );
 275        reset($trusted);
 276        $this->assertSame( "http://www.test.com/", key($trusted) );
 277        $this->assertSame( true, current($trusted) );
 278
 279        // extensions
 280        $sreg = new Zend_OpenId_Extension_Sreg(array("nickname"=>"test_id"));
 281        $this->assertTrue( $provider->allowSite("http://www.test.com/", $sreg) );
 282
 283        $trusted = $storage->getTrustedSites(self::USER);
 284        $this->assertTrue( is_array($trusted) );
 285        $this->assertSame( 1, count($trusted) );
 286        reset($trusted);
 287        $this->assertSame( "http://www.test.com/", key($trusted) );
 288        $this->assertSame( array('Zend_OpenId_Extension_Sreg'=>array('nickname'=>'test_id')), current($trusted) );
 289
 290        $this->_user->delLoggedInUser();
 291        $storage->delUser(self::USER);
 292    }
 293
 294    /**
 295     * testing denySite
 296     *
 297     */
 298    public function testDenySite()
 299    {
 300        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 301        $storage->delUser(self::USER);
 302        $this->_user->delLoggedInUser();
 303        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 304        $sreg = new Zend_OpenId_Extension_Sreg(array("nickname"=>"test_id"));
 305
 306        // not logged in
 307        $this->assertFalse( $provider->denySite("http://www.test.com/") );
 308
 309        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 310        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 311        $this->assertTrue( $provider->allowSite("http://www.test1.com/") );
 312        $this->assertTrue( $provider->allowSite("http://www.test2.com/", $sreg) );
 313        $this->AssertSame( array(
 314                               'http://www.test1.com/' => true,
 315                               'http://www.test2.com/' => array(
 316                                   'Zend_OpenId_Extension_Sreg' => array(
 317                                       'nickname' => 'test_id'
 318                                   )
 319                               )
 320                           ),
 321                           $storage->getTrustedSites(self::USER) );
 322
 323        $this->assertTrue( $provider->denySite("http://www.test3.com/") );
 324        $this->AssertSame( array(
 325                               'http://www.test1.com/' => true,
 326                               'http://www.test2.com/' => array(
 327                                   'Zend_OpenId_Extension_Sreg' => array(
 328                                       'nickname' => 'test_id'
 329                                   )
 330                               ),
 331                               'http://www.test3.com/' => false
 332                           ),
 333                           $storage->getTrustedSites(self::USER) );
 334
 335        $this->assertTrue( $provider->denySite("http://www.test1.com/") );
 336        $this->AssertSame( array(
 337                               'http://www.test1.com/' => false,
 338                               'http://www.test2.com/' => array(
 339                                   'Zend_OpenId_Extension_Sreg' => array(
 340                                       'nickname' => 'test_id'
 341                                   )
 342                               ),
 343                               'http://www.test3.com/' => false
 344                           ),
 345                           $storage->getTrustedSites(self::USER) );
 346
 347        $this->assertTrue( $provider->denySite("http://www.test2.com/") );
 348        $this->AssertSame( array(
 349                               'http://www.test1.com/' => false,
 350                               'http://www.test2.com/' => false,
 351                               'http://www.test3.com/' => false
 352                           ),
 353                           $storage->getTrustedSites(self::USER) );
 354
 355        $this->_user->delLoggedInUser();
 356        $storage->delUser(self::USER);
 357    }
 358
 359    /**
 360     * testing delSite
 361     *
 362     */
 363    public function testDelSite()
 364    {
 365        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 366        $storage->delUser(self::USER);
 367        $this->_user->delLoggedInUser();
 368        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 369        $sreg = new Zend_OpenId_Extension_Sreg(array("nickname"=>"test_id"));
 370
 371        // not logged in
 372        $this->assertFalse( $provider->delSite("http://www.test.com/") );
 373
 374        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 375        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 376        $this->assertTrue( $provider->allowSite("http://www.test1.com/") );
 377        $this->assertTrue( $provider->allowSite("http://www.test2.com/", $sreg) );
 378        $this->AssertSame( array(
 379                               'http://www.test1.com/' => true,
 380                               'http://www.test2.com/' => array(
 381                                   'Zend_OpenId_Extension_Sreg' => array(
 382                                       'nickname' => 'test_id'
 383                                   )
 384                               )
 385                           ),
 386                           $storage->getTrustedSites(self::USER) );
 387
 388        $this->assertTrue( $provider->delSite("http://www.test3.com/") );
 389        $this->AssertSame( array(
 390                               'http://www.test1.com/' => true,
 391                               'http://www.test2.com/' => array(
 392                                   'Zend_OpenId_Extension_Sreg' => array(
 393                                       'nickname' => 'test_id'
 394                                   )
 395                               )
 396                           ),
 397                           $storage->getTrustedSites(self::USER) );
 398
 399        $this->assertTrue( $provider->delSite("http://www.test1.com/") );
 400        $this->AssertSame( array(
 401                               'http://www.test2.com/' => array(
 402                                   'Zend_OpenId_Extension_Sreg' => array(
 403                                       'nickname' => 'test_id'
 404                                   )
 405                               )
 406                           ),
 407                           $storage->getTrustedSites(self::USER) );
 408
 409        $this->assertTrue( $provider->delSite("http://www.test2.com/") );
 410        $this->AssertSame( array(
 411                           ),
 412                           $storage->getTrustedSites(self::USER) );
 413
 414        $this->_user->delLoggedInUser();
 415        $storage->delUser(self::USER);
 416    }
 417
 418    /**
 419     * testing getTrustedSites
 420     *
 421     */
 422    public function testGetTrustedSites()
 423    {
 424        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 425        $storage->delUser(self::USER);
 426        $this->_user->delLoggedInUser();
 427        $provider = new Zend_OpenId_Provider(null, null, $this->_user, $storage);
 428        $sreg = new Zend_OpenId_Extension_Sreg(array("nickname"=>"test_id"));
 429
 430        $this->assertTrue( $provider->register(self::USER, self::PASSWORD) );
 431        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 432        $this->assertTrue( $provider->allowSite("http://www.test1.com/") );
 433        $this->assertTrue( $provider->allowSite("http://www.test2.com/", $sreg) );
 434        $this->AssertSame( array(
 435                               'http://www.test1.com/' => true,
 436                               'http://www.test2.com/' => array(
 437                                   'Zend_OpenId_Extension_Sreg' => array(
 438                                       'nickname' => 'test_id'
 439                                   )
 440                               )
 441                           ),
 442                           $provider->getTrustedSites() );
 443
 444        $this->_user->delLoggedInUser();
 445        $this->AssertFalse( $provider->getTrustedSites() );
 446
 447        $storage->delUser(self::USER);
 448    }
 449
 450    /**
 451     * testing genSecret
 452     *
 453     */
 454    public function testGenSecret()
 455    {
 456        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider"));
 457
 458        // SHA1
 459        $x = $provider->genSecret("sha1");
 460        $this->assertTrue( is_string($x) );
 461        $this->assertSame( 20, strlen($x) );
 462
 463        // SHA256
 464        $x = $provider->genSecret("sha256");
 465        $this->assertTrue( is_string($x) );
 466        $this->assertSame( 32, strlen($x) );
 467
 468        // invalid function
 469        $this->assertFalse( $provider->genSecret("md5") );
 470    }
 471
 472    /**
 473     * testing _associate
 474     *
 475     */
 476    public function testAssociate()
 477    {
 478        try {
 479            $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 480            $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
 481
 482            // Wrong assoc_type
 483            $ret = $provider->handle(array('openid_mode'=>'associate'));
 484            $res = array();
 485            foreach (explode("\n", $ret) as $line) {
 486                if (!empty($line)) {
 487                    list($key, $val) = explode(":", $line, 2);
 488                    $res[$key] = $val;
 489                }
 490            }
 491            $this->assertSame( 'unsupported-type', $res['error-code'] );
 492
 493            // Wrong assoc_type (OpenID 2.0)
 494            $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 495                                           'openid_mode'=>'associate'));
 496            $res = array();
 497            foreach (explode("\n", $ret) as $line) {
 498                if (!empty($line)) {
 499                    list($key, $val) = explode(":", $line, 2);
 500                    $res[$key] = $val;
 501                }
 502            }
 503            $this->assertSame( Zend_OpenId::NS_2_0, $res['ns'] );
 504            $this->assertSame( 'unsupported-type', $res['error-code'] );
 505
 506            // Wrong session_type
 507            $ret = $provider->handle(array('openid_mode'=>'associate',
 508                                           'openid_assoc_type'=>'HMAC-SHA1',
 509                                           'openid_session_type'=>'DH-SHA257'));
 510            $res = array();
 511            foreach (explode("\n", $ret) as $line) {
 512                if (!empty($line)) {
 513                    list($key, $val) = explode(":", $line, 2);
 514                    $res[$key] = $val;
 515                }
 516            }
 517            $this->assertSame( 'unsupported-type', $res['error-code'] );
 518
 519            // Associaation without encryption
 520            $ret = $provider->handle(array('openid_assoc_type'=>'HMAC-SHA1',
 521                                           'openid_mode'=>'associate'));
 522            $res = array();
 523            foreach (explode("\n", $ret) as $line) {
 524                if (!empty($line)) {
 525                    list($key, $val) = explode(":", $line, 2);
 526                    $res[$key] = $val;
 527                }
 528            }
 529            $this->assertSame( 'HMAC-SHA1', $res['assoc_type'] );
 530            $this->assertTrue( isset($res['mac_key']) );
 531            $this->assertSame( 20, strlen(base64_decode($res['mac_key'])) );
 532            $this->assertTrue( isset($res['assoc_handle']) );
 533            $this->assertSame( '3600', $res['expires_in'] );
 534            $this->assertFalse( isset($res['session_type']) );
 535            $this->assertTrue( $storage->getAssociation($res['assoc_handle'], $macFunc, $secret, $expires) );
 536            $this->assertSame( 'sha1', $macFunc );
 537            $this->assertSame( bin2hex(base64_decode($res['mac_key'])), bin2hex($secret) );
 538
 539            // Associaation without encryption (OpenID 2.0)
 540            $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 541                                           'openid_assoc_type'=>'HMAC-SHA256',
 542                                           'openid_mode'=>'associate'));
 543            $res = array();
 544            foreach (explode("\n", $ret) as $line) {
 545                if (!empty($line)) {
 546                    list($key, $val) = explode(":", $line, 2);
 547                    $res[$key] = $val;
 548                }
 549            }
 550            $this->assertSame( Zend_OpenId::NS_2_0, $res['ns'] );
 551            $this->assertSame( 'HMAC-SHA256', $res['assoc_type'] );
 552            $this->assertTrue( isset($res['mac_key']) );
 553            $this->assertSame( 32, strlen(base64_decode($res['mac_key'])) );
 554            $this->assertTrue( isset($res['assoc_handle']) );
 555            $this->assertSame( '3600', $res['expires_in'] );
 556            $this->assertFalse( isset($res['session_type']) );
 557            $this->assertTrue( $storage->getAssociation($res['assoc_handle'], $macFunc, $secret, $expires) );
 558            $this->assertSame( 'sha256', $macFunc );
 559            $this->assertSame( bin2hex(base64_decode($res['mac_key'])), bin2hex($secret) );
 560
 561            // Associaation without encryption (OpenID 2.0)
 562            $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 563                                           'openid_assoc_type'=>'HMAC-SHA256',
 564                                           'openid_mode'=>'associate',
 565                                           'openid_session_type'=>'no-encryption'));
 566            $res = array();
 567            foreach (explode("\n", $ret) as $line) {
 568                if (!empty($line)) {
 569                    list($key, $val) = explode(":", $line, 2);
 570                    $res[$key] = $val;
 571                }
 572            }
 573            $this->assertSame( Zend_OpenId::NS_2_0, $res['ns'] );
 574            $this->assertSame( 'HMAC-SHA256', $res['assoc_type'] );
 575            $this->assertTrue( isset($res['mac_key']) );
 576            $this->assertSame( 32, strlen(base64_decode($res['mac_key'])) );
 577            $this->assertTrue( isset($res['assoc_handle']) );
 578            $this->assertSame( '3600', $res['expires_in'] );
 579            $this->assertSame( 'no-encryption', $res['session_type'] );
 580            $this->assertTrue( $storage->getAssociation($res['assoc_handle'], $macFunc, $secret, $expires) );
 581            $this->assertSame( 'sha256', $macFunc );
 582            $this->assertSame( bin2hex(base64_decode($res['mac_key'])), bin2hex($secret) );
 583
 584            // Associaation with DH-SHA1 encryption
 585            $ret = $provider->handle(array('openid_assoc_type'=>'HMAC-SHA1',
 586                                           'openid_mode'=>'associate',
 587                                           'openid_session_type'=>'DH-SHA1',
 588                                           'openid_dh_modulus'=>'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr',
 589                                           'openid_dh_gen'=>'Ag==',
 590                                           'openid_dh_consumer_public'=>'RqexRm+Zn5s3sXxFBjI9WfCOBwBDDQBKPzX4fjMGl3YEJh5tx8SVo7awgwuqsliR+nvjmRh5kSFIGv8YSCsy88v1CcAfWUGfjehO9euxQcXOYJnNGbl6GQrE2FYe2RCvML4Yi8eYCYtCQi0wlDE7BJXGSVPXFzj/ru0lR/voPpk=',
 591                                           ));
 592            $res = array();
 593            foreach (explode("\n", $ret) as $line) {
 594                if (!empty($line)) {
 595                    list($key, $val) = explode(":", $line, 2);
 596                    $res[$key] = $val;
 597                }
 598            }
 599            $this->assertSame( 'HMAC-SHA1', $res['assoc_type'] );
 600            $this->assertSame( 'DH-SHA1', $res['session_type'] );
 601            $this->assertTrue( isset($res['dh_server_public']) );
 602            $this->assertTrue( isset($res['enc_mac_key']) );
 603            $this->assertSame( 20, strlen(base64_decode($res['enc_mac_key'])) );
 604            $this->assertTrue( isset($res['assoc_handle']) );
 605            $this->assertSame( '3600', $res['expires_in'] );
 606            $this->assertTrue( $storage->getAssociation($res['assoc_handle'], $macFunc, $secret, $expires) );
 607            $this->assertSame( 'sha1', $macFunc );
 608
 609            // Associaation with DH-SHA256 encryption (OpenID 2.0)
 610            $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 611                                           'openid_assoc_type'=>'HMAC-SHA256',
 612                                           'openid_mode'=>'associate',
 613                                           'openid_session_type'=>'DH-SHA256',
 614                                           'openid_dh_modulus'=>'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr',
 615                                           'openid_dh_gen'=>'Ag==',
 616                                           'openid_dh_consumer_public'=>'RqexRm+Zn5s3sXxFBjI9WfCOBwBDDQBKPzX4fjMGl3YEJh5tx8SVo7awgwuqsliR+nvjmRh5kSFIGv8YSCsy88v1CcAfWUGfjehO9euxQcXOYJnNGbl6GQrE2FYe2RCvML4Yi8eYCYtCQi0wlDE7BJXGSVPXFzj/ru0lR/voPpk=',
 617                                           ));
 618            $res = array();
 619            foreach (explode("\n", $ret) as $line) {
 620                if (!empty($line)) {
 621                    list($key, $val) = explode(":", $line, 2);
 622                    $res[$key] = $val;
 623                }
 624            }
 625            $this->assertSame( 'HMAC-SHA256', $res['assoc_type'] );
 626            $this->assertSame( 'DH-SHA256', $res['session_type'] );
 627            $this->assertTrue( isset($res['dh_server_public']) );
 628            $this->assertTrue( isset($res['enc_mac_key']) );
 629            $this->assertSame( 32, strlen(base64_decode($res['enc_mac_key'])) );
 630            $this->assertTrue( isset($res['assoc_handle']) );
 631            $this->assertSame( '3600', $res['expires_in'] );
 632            $this->assertTrue( $storage->getAssociation($res['assoc_handle'], $macFunc, $secret, $expires) );
 633            $this->assertSame( 'sha256', $macFunc );
 634        } catch (Zend_OpenId_Exception $e) {
 635            $this->markTestSkipped($e->getMessage());
 636        }
 637    }
 638
 639    /**
 640     * testing _checkAuthentication
 641     *
 642     */
 643    public function testCheckAuthentication()
 644    {
 645        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 646        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
 647
 648        // Wrong arguments
 649        $ret = $provider->handle(array('openid_mode'=>'check_authentication'));
 650        $res = array();
 651        foreach (explode("\n", $ret) as $line) {
 652            if (!empty($line)) {
 653                list($key, $val) = explode(":", $line, 2);
 654                $res[$key] = $val;
 655            }
 656        }
 657        $this->assertSame( 'id_res', $res['openid.mode'] );
 658        $this->assertSame( 'false', $res['is_valid'] );
 659
 660        // Wrong arguments (OpenID 2.0)
 661        $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 662                                       'openid_mode'=>'check_authentication'));
 663        $res = array();
 664        foreach (explode("\n", $ret) as $line) {
 665            if (!empty($line)) {
 666                list($key, $val) = explode(":", $line, 2);
 667                $res[$key] = $val;
 668            }
 669        }
 670        $this->assertSame( Zend_OpenId::NS_2_0, $res['ns'] );
 671        $this->assertSame( 'id_res', $res['openid.mode'] );
 672        $this->assertSame( 'false', $res['is_valid'] );
 673
 674        // Wrong session id
 675        $storage->delAssociation(self::HANDLE);
 676        $ret = $provider->handle(array('openid_mode'=>'check_authentication',
 677                                       'openid_assoc_handle'=>self::HANDLE));
 678        $res = array();
 679        foreach (explode("\n", $ret) as $line) {
 680            if (!empty($line)) {
 681                list($key, $val) = explode(":", $line, 2);
 682                $res[$key] = $val;
 683            }
 684        }
 685        $this->assertSame( 'id_res', $res['openid.mode'] );
 686        $this->assertSame( 'false', $res['is_valid'] );
 687
 688        // Proper session signed with HAMC-SHA256
 689        $storage->addAssociation(self::HANDLE, "sha1", pack("H*", '0102030405060708091011121314151617181920'), time() + 3660);
 690        $ret = $provider->handle(array('openid_mode'=>'check_authentication',
 691                                       'openid_assoc_handle'=>self::HANDLE,
 692                                       'openid_signed'=>'mode,assoc_handle,signed',
 693                                       'openid_sig'=>'IgLZCOXmEPowYl6yyFZjYL4ZTtQ='));
 694        $res = array();
 695        foreach (explode("\n", $ret) as $line) {
 696            if (!empty($line)) {
 697                list($key, $val) = explode(":", $line, 2);
 698                $res[$key] = $val;
 699            }
 700        }
 701        $this->assertSame( 'id_res', $res['openid.mode'] );
 702        $this->assertSame( 'true', $res['is_valid'] );
 703
 704        // Proper session signed with HAMC-SHA256
 705        $storage->delAssociation(self::HANDLE);
 706        $storage->addAssociation(self::HANDLE, "sha256", pack("H*", '0102030405060708091011121314151617181920212223242526272829303132'), time() + 3660);
 707        $ret = $provider->handle(array('openid_mode'=>'check_authentication',
 708                                       'openid_assoc_handle'=>self::HANDLE,
 709                                       'openid_signed'=>'mode,assoc_handle,signed',
 710                                       'openid_sig'=>'xoJcXj30L1N7QRir7I2ovop1SaijXnAI97X/yH+kvck='));
 711        $res = array();
 712        foreach (explode("\n", $ret) as $line) {
 713            if (!empty($line)) {
 714                list($key, $val) = explode(":", $line, 2);
 715                $res[$key] = $val;
 716            }
 717        }
 718        $this->assertSame( 'id_res', $res['openid.mode'] );
 719        $this->assertSame( 'true', $res['is_valid'] );
 720
 721        // Wrong signature
 722        $storage->delAssociation(self::HANDLE);
 723        $storage->addAssociation(self::HANDLE, "sha256", pack("H*", '0102030405060708091011121314151617181920212223242526272829303132'), time() + 3660);
 724        $ret = $provider->handle(array('openid_ns'=>Zend_OpenId::NS_2_0,
 725                                       'openid_mode'=>'check_authentication',
 726                                       'openid_assoc_handle'=>self::HANDLE,
 727                                       'openid_signed'=>'ns,mode,assoc_handle,signed',
 728                                       'openid_sig'=>'xoJcXj30L1N7QRir7I2ovop1SaijXnAI97X/yH+kvck='));
 729        $res = array();
 730        foreach (explode("\n", $ret) as $line) {
 731            if (!empty($line)) {
 732                list($key, $val) = explode(":", $line, 2);
 733                $res[$key] = $val;
 734            }
 735        }
 736        $this->assertSame( 'id_res', $res['openid.mode'] );
 737        $this->assertSame( 'false', $res['is_valid'] );
 738
 739        $storage->delAssociation(self::HANDLE);
 740    }
 741
 742    /**
 743     * testing respondToConsumer
 744     *
 745     */
 746    public function testRespondToConsumer()
 747    {
 748        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 749        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
 750
 751        // dumb mode
 752        $response = new Zend_OpenId_ResponseHelper(true);
 753        $storage->delAssociation(self::HANDLE);
 754        $this->assertTrue( $provider->respondToConsumer(array(
 755                'openid_assoc_handle' => self::HANDLE,
 756                'openid_return_to' => 'http://www.test.com/test.php'
 757            ), null, $response) );
 758        $headers = $response->getHeaders();
 759        $this->assertSame( 'Location', $headers[0]['name'] );
 760        $url = parse_url($headers[0]['value']);
 761        $this->assertSame( 'www.test.com', $url['host'] );
 762        $this->assertSame( '/test.php', $url['path'] );
 763        $ret = array();
 764        foreach (explode('&', $url['query']) as $line) {
 765            list($key,$val) = explode('=', $line, 2);
 766            $ret[$key] = urldecode($val);
 767        }
 768        $this->assertSame( 'id_res', $ret['openid.mode'] );
 769        $this->assertSame( 'http://www.test.com/test.php', $ret['openid.return_to'] );
 770        $this->assertTrue( isset($ret['openid.assoc_handle']) );
 771        $this->assertTrue( isset($ret['openid.response_nonce']) );
 772        $this->assertTrue( isset($ret['openid.signed']) );
 773        $this->assertTrue( isset($ret['openid.sig']) );
 774        $this->assertTrue( $storage->getAssociation($ret['openid.assoc_handle'], $macFunc, $secret, $expires) );
 775        $this->assertSame( 'sha1', $macFunc );
 776
 777        // OpenID 2.0 with SHA256
 778        $_SERVER['SCRIPT_URI'] = "http://www.test.com/endpoint.php";
 779        $response = new Zend_OpenId_ResponseHelper(true);
 780        $storage->addAssociation(self::HANDLE, "sha256", pack("H*", '0102030405060708091011121314151617181920212223242526272829303132'), time() + 3660);
 781        $this->assertTrue( $provider->respondToConsumer(array(
 782                'openid_ns' => Zend_OpenId::NS_2_0,
 783                'openid_assoc_handle' => self::HANDLE,
 784                'openid_return_to' => 'http://www.test.com/test.php'
 785            ), null, $response) );
 786        $headers = $response->getHeaders();
 787        $this->assertSame( 'Location', $headers[0]['name'] );
 788        $url = parse_url($headers[0]['value']);
 789        $this->assertSame( 'www.test.com', $url['host'] );
 790        $this->assertSame( '/test.php', $url['path'] );
 791        $ret = array();
 792        foreach (explode('&', $url['query']) as $line) {
 793            list($key,$val) = explode('=', $line, 2);
 794            $ret[$key] = urldecode($val);
 795        }
 796        $this->assertSame( 'id_res', $ret['openid.mode'] );
 797        $this->assertSame( 'http://www.test.com/test.php', $ret['openid.return_to'] );
 798        $this->assertSame( self::HANDLE, $ret['openid.assoc_handle'] );
 799        $this->assertTrue( isset($ret['openid.response_nonce']) );
 800        $this->assertTrue( isset($ret['openid.signed']) );
 801        $this->assertTrue( isset($ret['openid.sig']) );
 802        $this->assertSame( Zend_OpenId::NS_2_0, $ret['openid.ns'] );
 803        $this->assertSame( "http://www.test.com/endpoint.php", $ret['openid.op_endpoint'] );
 804        $this->assertTrue( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
 805        $this->assertSame( 'sha256', $macFunc );
 806        $storage->delAssociation(self::HANDLE);
 807
 808        // OpenID 1.1 with SHA1
 809        $storage->addAssociation(self::HANDLE, "sha1", pack("H*", '0102030405060708091011121314151617181920'), time() + 3660);
 810        $response = new Zend_OpenId_ResponseHelper(true);
 811        $ret = $provider->respondToConsumer(array(
 812                'openid_assoc_handle' => self::HANDLE,
 813                'openid_return_to' => 'http://www.test.com/test.php',
 814                'openid_claimed_id' => 'http://claimed_id/',
 815                'openid_identity' => 'http://identity/',
 816                'openid_unknown' => 'http://www.test.com/test.php',
 817            ), null, $response);
 818        $headers = $response->getHeaders();
 819        $this->assertSame( 'Location', $headers[0]['name'] );
 820        $url = parse_url($headers[0]['value']);
 821        $this->assertSame( 'www.test.com', $url['host'] );
 822        $this->assertSame( '/test.php', $url['path'] );
 823        $ret = array();
 824        foreach (explode('&', $url['query']) as $line) {
 825            list($key,$val) = explode('=', $line, 2);
 826            $ret[$key] = urldecode($val);
 827        }
 828        $this->assertSame( 'id_res', $ret['openid.mode'] );
 829        $this->assertSame( 'http://www.test.com/test.php', $ret['openid.return_to'] );
 830        $this->assertSame( self::HANDLE, $ret['openid.assoc_handle'] );
 831        $this->assertTrue( isset($ret['openid.response_nonce']) );
 832        $this->assertTrue( isset($ret['openid.signed']) );
 833        $this->assertTrue( isset($ret['openid.sig']) );
 834        $this->assertFalse( isset($ret['openid.ns']) );
 835        $this->assertFalse( isset($ret['openid.op_endpoint']) );
 836        $this->assertSame( 'http://claimed_id/', $ret['openid.claimed_id'] );
 837        $this->assertSame( 'http://identity/', $ret['openid.identity'] );
 838        $this->assertFalse( isset($ret['openid.unknown']) );
 839        $this->assertTrue( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
 840        $this->assertSame( 'sha1', $macFunc );
 841        $storage->delAssociation(self::HANDLE);
 842
 843        // extensions
 844        $sreg = new Zend_OpenId_Extension_Sreg(array("nickname"=>"test_id"));
 845        $response = new Zend_OpenId_ResponseHelper(true);
 846        $this->assertTrue( $provider->respondToConsumer(array(
 847                'openid_return_to' => 'http://www.test.com/test.php',
 848            ), $sreg, $response) );
 849        $headers = $response->getHeaders();
 850        $this->assertSame( 'Location', $headers[0]['name'] );
 851        $url = parse_url($headers[0]['value']);
 852        $this->assertSame( 'www.test.com', $url['host'] );
 853        $this->assertSame( '/test.php', $url['path'] );
 854        $ret = array();
 855        foreach (explode('&', $url['query']) as $line) {
 856            list($key,$val) = explode('=', $line, 2);
 857            $ret[$key] = urldecode($val);
 858        }
 859        $this->assertSame( 'test_id', $ret['openid.sreg.nickname'] );
 860    }
 861
 862    /**
 863     * testing _checkId
 864     *
 865     */
 866    public function testCheckIdImmediate()
 867    {
 868        $_SERVER['SCRIPT_URI'] = "http://www.test.com/server.php";
 869        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
 870        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
 871        $provider->logout();
 872
 873        // Wrong arguments (no openid.return_to and openid.trust_root)
 874        $response = new Zend_OpenId_ResponseHelper(true);
 875        $this->assertFalse( $provider->handle(array(
 876            'openid_mode'=>'checkid_immediate'),
 877            null, $response) );
 878
 879        // Unexistent user
 880        $storage->delUser(self::USER);
 881        $response = new Zend_OpenId_ResponseHelper(true);
 882        $this->assertTrue($provider->handle(array(
 883            'openid_mode'=>'checkid_immediate',
 884            'openid_identity'=>self::USER,
 885            'openid_return_to'=>'http://www.test.com/test.php'),
 886            null, $response));
 887        $headers = $response->getHeaders();
 888        $this->assertSame( 'Location', $headers[0]['name'] );
 889        $this->assertSame( 'http://www.test.com/test.php?openid.mode=cancel', $headers[0]['value'] );
 890
 891        // No openid_identity
 892        $response = new Zend_OpenId_ResponseHelper(true);
 893        $this->assertTrue($provider->handle(array(
 894            'openid_mode'=>'checkid_immediate',
 895            'openid_return_to'=>'http://www.test.com/test.php'),
 896            null, $response));
 897        $headers = $response->getHeaders();
 898        $this->assertSame( 'Location', $headers[0]['name'] );
 899        $url = parse_url($headers[0]['value']);
 900        $this->assertSame( 'www.test.com', $url['host'] );
 901        $this->assertSame( '/test.php', $url['path'] );
 902        $query = array();
 903        foreach (explode('&', $url['query']) as $line) {
 904            list($key,$val) = explode('=', $line, 2);
 905            $query[$key] = urldecode($val);
 906        }
 907        $this->assertSame( 'id_res', $query['openid.mode'] );
 908        $url2 = parse_url($query['openid.user_setup_url']);
 909        $this->assertSame( 'www.test.com', $url2['host'] );
 910        $this->assertSame( '/server.php', $url2['path'] );
 911        $query2 = array();
 912        foreach (explode('&', $url2['query']) as $line) {
 913            list($key,$val) = explode('=', $line, 2);
 914            $query2[$key] = urldecode($val);
 915        }
 916        $this->assertSame( 'login', $query2['openid.action'] );
 917        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
 918        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
 919
 920        // Non logged in user
 921        $provider->register(self::USER, self::PASSWORD);
 922        $response = new Zend_OpenId_ResponseHelper(true);
 923        $this->assertTrue($provider->handle(array(
 924            'openid_mode'=>'checkid_immediate',
 925            'openid_identity'=>self::USER,
 926            'openid_return_to'=>'http://www.test.com/test.php'),
 927            null, $response));
 928        $headers = $response->getHeaders();
 929        $this->assertSame( 'Location', $headers[0]['name'] );
 930        $url = parse_url($headers[0]['value']);
 931        $this->assertSame( 'www.test.com', $url['host'] );
 932        $this->assertSame( '/test.php', $url['path'] );
 933        $query = array();
 934        foreach (explode('&', $url['query']) as $line) {
 935            list($key,$val) = explode('=', $line, 2);
 936            $query[$key] = urldecode($val);
 937        }
 938        $this->assertSame( 'id_res', $query['openid.mode'] );
 939        $url2 = parse_url($query['openid.user_setup_url']);
 940        $this->assertSame( 'www.test.com', $url2['host'] );
 941        $this->assertSame( '/server.php', $url2['path'] );
 942        $query2 = array();
 943        foreach (explode('&', $url2['query']) as $line) {
 944            list($key,$val) = explode('=', $line, 2);
 945            $query2[$key] = urldecode($val);
 946        }
 947        $this->assertSame( 'login', $query2['openid.action'] );
 948        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
 949        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
 950        $this->assertSame( self::USER, $query2['openid.identity'] );
 951
 952        // Non logged in user with SREG
 953        $provider->register(self::USER, self::PASSWORD);
 954        $response = new Zend_OpenId_ResponseHelper(true);
 955        $this->assertTrue($provider->handle(array(
 956            'openid_mode'=>'checkid_immediate',
 957            'openid_identity'=>self::USER,
 958            'openid_return_to'=>'http://www.test.com/test.php',
 959            'openid_ns_sreg'=>Zend_OpenId_Extension_Sreg::NAMESPACE_1_1,
 960            'openid_sreg_required'=>'nickname'),
 961            null, $response));
 962        $headers = $response->getHeaders();
 963        $this->assertSame( 'Location', $headers[0]['name'] );
 964        $url = parse_url($headers[0]['value']);
 965        $this->assertSame( 'www.test.com', $url['host'] );
 966        $this->assertSame( '/test.php', $url['path'] );
 967        $query = array();
 968        foreach (explode('&', $url['query']) as $line) {
 969            list($key,$val) = explode('=', $line, 2);
 970            $query[$key] = urldecode($val);
 971        }
 972        $this->assertSame( 'id_res', $query['openid.mode'] );
 973        $url2 = parse_url($query['openid.user_setup_url']);
 974        $this->assertSame( 'www.test.com', $url2['host'] );
 975        $this->assertSame( '/server.php', $url2['path'] );
 976        $query2 = array();
 977        foreach (explode('&', $url2['query']) as $line) {
 978            list($key,$val) = explode('=', $line, 2);
 979            $query2[$key] = urldecode($val);
 980        }
 981        $this->assertSame( 'login', $query2['openid.action'] );
 982        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
 983        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
 984        $this->assertSame( self::USER, $query2['openid.identity'] );
 985        $this->assertSame( Zend_OpenId_Extension_Sreg::NAMESPACE_1_1, $query2['openid.ns.sreg'] );
 986        $this->assertSame( "nickname", $query2['openid.sreg.required'] );
 987
 988        // Logged in user (unknown site)
 989        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
 990        $response = new Zend_OpenId_ResponseHelper(true);
 991        $this->assertTrue($provider->handle(array(
 992            'openid_mode'=>'checkid_immediate',
 993            'openid_identity'=>self::USER,
 994            'openid_return_to'=>'http://www.test.com/test.php'),
 995            null, $response));
 996        $headers = $response->getHeaders();
 997        $this->assertSame( 'Location', $headers[0]['name'] );
 998        $url = parse_url($headers[0]['value']);
 999        $this->assertSame( 'www.test.com', $url['host'] );
1000        $this->assertSame( '/test.php', $url['path'] );
1001        $query = array();
1002        foreach (explode('&', $url['query']) as $line) {
1003            list($key,$val) = explode('=', $line, 2);
1004            $query[$key] = urldecode($val);
1005        }
1006        $this->assertSame( 'id_res', $query['openid.mode'] );
1007        $url2 = parse_url($query['openid.user_setup_url']);
1008        $this->assertSame( 'www.test.com', $url2['host'] );
1009        $this->assertSame( '/server.php', $url2['path'] );
1010        $query2 = array();
1011        foreach (explode('&', $url2['query']) as $line) {
1012            list($key,$val) = explode('=', $line, 2);
1013            $query2[$key] = urldecode($val);
1014        }
1015        $this->assertSame( 'trust', $query2['openid.action'] );
1016        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
1017        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
1018        $this->assertSame( self::USER, $query2['openid.identity'] );
1019
1020        // Logged in user (unknown site 2)
1021        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
1022        $this->assertTrue( $provider->allowSite('http://www.test.com/test1.php') );
1023        $response = new Zend_OpenId_ResponseHelper(true);
1024        $this->assertTrue($provider->handle(array(
1025            'openid_mode'=>'checkid_immediate',
1026            'openid_identity'=>self::USER,
1027            'openid_return_to'=>'http://www.test.com/test.php'),
1028            null, $response));
1029        $headers = $response->getHeaders();
1030        $this->assertSame( 'Location', $headers[0]['name'] );
1031        $url = parse_url($headers[0]['value']);
1032        $this->assertSame( 'www.test.com', $url['host'] );
1033        $this->assertSame( '/test.php', $url['path'] );
1034        $query = array();
1035        foreach (explode('&', $url['query']) as $line) {
1036            list($key,$val) = explode('=', $line, 2);
1037            $query[$key] = urldecode($val);
1038        }
1039        $this->assertSame( 'id_res', $query['openid.mode'] );
1040        $url2 = parse_url($query['openid.user_setup_url']);
1041        $this->assertSame( 'www.test.com', $url2['host'] );
1042        $this->assertSame( '/server.php', $url2['path'] );
1043        $query2 = array();
1044        foreach (explode('&', $url2['query']) as $line) {
1045            list($key,$val) = explode('=', $line, 2);
1046            $query2[$key] = urldecode($val);
1047        }
1048        $this->assertSame( 'trust', $query2['openid.action'] );
1049        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
1050        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
1051        $this->assertSame( self::USER, $query2['openid.identity'] );
1052
1053        // Logged in user (unknown site + SREG)
1054        $response = new Zend_OpenId_ResponseHelper(true);
1055        $this->assertTrue( $provider->delSite('http://www.test.com/test1.php') );
1056        $this->assertTrue($provider->handle(array(
1057            'openid_mode'=>'checkid_immediate',
1058            'openid_identity'=>self::USER,
1059            'openid_return_to'=>'http://www.test.com/test.php',
1060            'openid_ns_sreg'=>Zend_OpenId_Extension_Sreg::NAMESPACE_1_1,
1061            'openid_sreg_required'=>'nickname'),
1062            null, $response));
1063        $headers = $response->getHeaders();
1064        $this->assertSame( 'Location', $headers[0]['name'] );
1065        $url = parse_url($headers[0]['value']);
1066        $this->assertSame( 'www.test.com', $url['host'] );
1067        $this->assertSame( '/test.php', $url['path'] );
1068        $query = array();
1069        foreach (explode('&', $url['query']) as $line) {
1070            list($key,$val) = explode('=', $line, 2);
1071            $query[$key] = urldecode($val);
1072        }
1073        $this->assertSame( 'id_res', $query['openid.mode'] );
1074        $url2 = parse_url($query['openid.user_setup_url']);
1075        $this->assertSame( 'www.test.com', $url2['host'] );
1076        $this->assertSame( '/server.php', $url2['path'] );
1077        $query2 = array();
1078        foreach (explode('&', $url2['query']) as $line) {
1079            list($key,$val) = explode('=', $line, 2);
1080            $query2[$key] = urldecode($val);
1081        }
1082        $this->assertSame( 'trust', $query2['openid.action'] );
1083        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
1084        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
1085        $this->assertSame( self::USER, $query2['openid.identity'] );
1086        $this->assertSame( Zend_OpenId_Extension_Sreg::NAMESPACE_1_1, $query2['openid.ns.sreg'] );
1087        $this->assertSame( "nickname", $query2['openid.sreg.required'] );
1088
1089        // Logged in user (untrusted site)
1090        $this->assertTrue( $provider->denySite('http://www.test.com') );
1091        $response = new Zend_OpenId_ResponseHelper(true);
1092        $this->assertTrue($provider->handle(array(
1093            'openid_mode'=>'checkid_immediate',
1094            'openid_identity'=>self::USER,
1095            'openid_return_to'=>'http://www.test.com/test.php'),
1096            null, $response));
1097        $headers = $response->getHeaders();
1098        $this->assertSame( 'Location', $headers[0]['name'] );
1099        $this->assertSame( 'http://www.test.com/test.php?openid.mode=cancel', $headers[0]['value'] );
1100
1101        // Logged in user (untrusted site with wildcard)
1102        $this->assertTrue( $provider->delSite('http://www.test.com') );
1103        $this->assertTrue( $provider->denySite('http://*.test.com') );
1104        $response = new Zend_OpenId_ResponseHelper(true);
1105        $this->assertTrue($provider->handle(array(
1106            'openid_mode'=>'checkid_immediate',
1107            'openid_identity'=>self::USER,
1108            'openid_return_to'=>'http://www.test.com/test.php'),
1109            null, $response));
1110        $headers = $response->getHeaders();
1111        $this->assertSame( 'Location', $headers[0]['name'] );
1112        $this->assertSame( 'http://www.test.com/test.php?openid.mode=cancel', $headers[0]['value'] );
1113
1114        // Logged in user (trusted site)
1115        $this->assertTrue( $provider->delSite('http://*.test.com') );
1116        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1117        $response = new Zend_OpenId_ResponseHelper(true);
1118        $this->assertTrue($provider->handle(array(
1119            'openid_mode'=>'checkid_immediate',
1120            'openid_identity'=>self::USER,
1121            'openid_return_to'=>'http://www.test.com/test.php'),
1122            null, $response));
1123        $headers = $response->getHeaders();
1124        $this->assertSame( 'Location', $headers[0]['name'] );
1125        $url = parse_url($headers[0]['value']);
1126        $this->assertSame( 'www.test.com', $url['host'] );
1127        $this->assertSame( '/test.php', $url['path'] );
1128        $query = array();
1129        foreach (explode('&', $url['query']) as $line) {
1130            list($key,$val) = explode('=', $line, 2);
1131            $query[$key] = urldecode($val);
1132        }
1133        $this->assertSame( 'id_res', $query['openid.mode'] );
1134        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1135        $this->assertSame( self::USER, $query['openid.identity'] );
1136        $this->assertTrue( isset($query['openid.assoc_handle']) );
1137        $this->assertTrue( isset($query['openid.response_nonce']) );
1138        $this->assertTrue( isset($query['openid.signed']) );
1139        $this->assertTrue( isset($query['openid.sig']) );
1140        $this->assertSame( 20, strlen(base64_decode($query['openid.sig'])) );
1141
1142        // Logged in user (trusted site without openid.return_to)
1143        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1144        $response = new Zend_OpenId_ResponseHelper(true);
1145        $this->assertTrue($provider->handle(array(
1146            'openid_mode'=>'checkid_immediate',
1147            'openid_identity'=>self::USER,
1148            'openid_trust_root'=>'http://www.test.com/test.php'),
1149            null, $response));
1150        $this->assertSame( array(), $response->getHeaders() );
1151        $this->assertSame( '', $response->getBody() );
1152
1153        // Logged in user (trusted site) & OpenID 2.0 & established session
1154        $storage->delAssociation(self::HANDLE);
1155        $storage->addAssociation(self::HANDLE, "sha1", pack("H*", '0102030405060708091011121314151617181920'), time() + 3660);
1156        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1157        $response = new Zend_OpenId_ResponseHelper(true);
1158        $this->assertTrue($provider->handle(array(
1159            'openid_ns'=>Zend_OpenId::NS_2_0,
1160            'openid_assoc_handle'=>self::HANDLE,
1161            'openid_mode'=>'checkid_immediate',
1162            'openid_identity'=>self::USER,
1163            'openid_return_to'=>'http://www.test.com/test.php'),
1164            null, $response));
1165        $headers = $response->getHeaders();
1166        $this->assertSame( 'Location', $headers[0]['name'] );
1167        $url = parse_url($headers[0]['value']);
1168        $this->assertSame( 'www.test.com', $url['host'] );
1169        $this->assertSame( '/test.php', $url['path'] );
1170        $query = array();
1171        foreach (explode('&', $url['query']) as $line) {
1172            list($key,$val) = explode('=', $line, 2);
1173            $query[$key] = urldecode($val);
1174        }
1175        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1176        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1177        $this->assertSame( 'id_res', $query['openid.mode'] );
1178        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1179        $this->assertSame( self::USER, $query['openid.identity'] );
1180        $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
1181        $this->assertTrue( isset($query['openid.response_nonce']) );
1182        $this->assertTrue( isset($query['openid.signed']) );
1183        $this->assertTrue( isset($query['openid.sig']) );
1184        $this->assertSame( 20, strlen(base64_decode($query['openid.sig'])) );
1185
1186        // Logged in user (trusted site) & invalid association handle
1187        $storage->delAssociation(self::HANDLE);
1188        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1189        $response = new Zend_OpenId_ResponseHelper(true);
1190        $this->assertTrue($provider->handle(array(
1191            'openid_ns'=>Zend_OpenId::NS_2_0,
1192            'openid_assoc_handle'=>self::HANDLE,
1193            'openid_mode'=>'checkid_immediate',
1194            'openid_identity'=>self::USER,
1195            'openid_return_to'=>'http://www.test.com/test.php'),
1196            null, $response));
1197        $headers = $response->getHeaders();
1198        $this->assertSame( 'Location', $headers[0]['name'] );
1199        $url = parse_url($headers[0]['value']);
1200        $this->assertSame( 'www.test.com', $url['host'] );
1201        $this->assertSame( '/test.php', $url['path'] );
1202        $query = array();
1203        foreach (explode('&', $url['query']) as $line) {
1204            list($key,$val) = explode('=', $line, 2);
1205            $query[$key] = urldecode($val);
1206        }
1207        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1208        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1209        $this->assertSame( 'id_res', $query['openid.mode'] );
1210        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1211        $this->assertSame( self::USER, $query['openid.identity'] );
1212        $this->assertSame( self::HANDLE, $query['openid.invalidate_handle'] );
1213        $this->assertTrue( isset($query['openid.assoc_handle']) );
1214        $this->assertTrue( isset($query['openid.response_nonce']) );
1215        $this->assertTrue( isset($query['openid.signed']) );
1216        $this->assertTrue( isset($query['openid.sig']) );
1217        $this->assertSame( 32, strlen(base64_decode($query['openid.sig'])) );
1218
1219        // SREG success
1220        $sreg = new Zend_OpenId_Extension_Sreg(array('nickname'=>'test','email'=>'test@test.com'));
1221        $this->assertTrue( $provider->allowSite('http://www.test.com/', $sreg) );
1222        $sreg = new Zend_OpenId_Extension_Sreg();
1223        $response = new Zend_OpenId_ResponseHelper(true);
1224        $this->assertTrue($provider->handle(array(
1225            'openid_ns'=>Zend_OpenId::NS_2_0,
1226            'openid_mode'=>'checkid_immediate',
1227            'openid_identity'=>self::USER,
1228            'openid_return_to'=>'http://www.test.com/test.php',
1229            'openid_ns_sreg'=>Zend_OpenId_Extension_Sreg::NAMESPACE_1_1,
1230            'openid_sreg_required'=>'nickname',
1231            'openid_sreg_optional'=>'email',
1232            ),
1233            $sreg, $response));
1234        $headers = $response->getHeaders();
1235        $this->assertSame( 'Location', $headers[0]['name'] );
1236        $url = parse_url($headers[0]['value']);
1237        $this->assertSame( 'www.test.com', $url['host'] );
1238        $this->assertSame( '/test.php', $url['path'] );
1239        $query = array();
1240        foreach (explode('&', $url['query']) as $line) {
1241            list($key,$val) = explode('=', $line, 2);
1242            $query[$key] = urldecode($val);
1243        }
1244        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1245        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1246        $this->assertSame( 'id_res', $query['openid.mode'] );
1247        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1248        $this->assertSame( self::USER, $query['openid.identity'] );
1249        $this->assertTrue( isset($query['openid.assoc_handle']) );
1250        $this->assertTrue( isset($query['openid.response_nonce']) );
1251        $this->assertTrue( isset($query['openid.signed']) );
1252        $this->assertTrue( isset($query['openid.sig']) );
1253        $this->assertSame( 32, strlen(base64_decode($query['openid.sig'])) );
1254        $this->assertSame( Zend_OpenId_Extension_Sreg::NAMESPACE_1_1, $query['openid.ns.sreg'] );
1255        $this->assertSame( 'test', $query['openid.sreg.nickname'] );
1256        $this->assertSame( 'test@test.com', $query['openid.sreg.email'] );
1257
1258        // SREG failed
1259        $sreg = new Zend_OpenId_Extension_Sreg(array('nickname'=>'test'));
1260        $this->assertTrue( $provider->allowSite('http://www.test.com/', $sreg) );
1261        $sreg = new Zend_OpenId_Extension_Sreg();
1262        $response = new Zend_OpenId_ResponseHelper(true);
1263        $this->assertTrue($provider->handle(array(
1264            'openid_mode'=>'checkid_immediate',
1265            'openid_identity'=>self::USER,
1266            'openid_return_to'=>'http://www.test.com/test.php',
1267            'openid_sreg_required'=>'nickname,email',
1268            ),
1269            $sreg, $response));
1270        $headers = $response->getHeaders();
1271        $this->assertSame( 'Location', $headers[0]['name'] );
1272        $url = parse_url($headers[0]['value']);
1273        $this->assertSame( 'www.test.com', $url['host'] );
1274        $this->assertSame( '/test.php', $url['path'] );
1275        $query = array();
1276        foreach (explode('&', $url['query']) as $line) {
1277            list($key,$val) = explode('=', $line, 2);
1278            $query[$key] = urldecode($val);
1279        }
1280        $this->assertSame( 'id_res', $query['openid.mode'] );
1281        $url2 = parse_url($query['openid.user_setup_url']);
1282        $this->assertSame( 'www.test.com', $url2['host'] );
1283        $this->assertSame( '/server.php', $url2['path'] );
1284        $query2 = array();
1285        foreach (explode('&', $url2['query']) as $line) {
1286            list($key,$val) = explode('=', $line, 2);
1287            $query2[$key] = urldecode($val);
1288        }
1289        $this->assertSame( 'trust', $query2['openid.action'] );
1290        $this->assertSame( 'checkid_setup', $query2['openid.mode'] );
1291        $this->assertSame( 'http://www.test.com/test.php', $query2['openid.return_to'] );
1292        $this->assertSame( self::USER, $query2['openid.identity'] );
1293        $this->assertSame( "nickname,email", $query2['openid.sreg.required'] );
1294
1295        $provider->logout();
1296        $storage->delUser(self::USER);
1297    }
1298
1299    /**
1300     * testing handle
1301     *
1302     */
1303    public function testCheckIdSetup()
1304    {
1305        $_SERVER['SCRIPT_URI'] = "http://www.test.com/server.php";
1306        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
1307        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
1308        $provider->logout();
1309
1310        // Wrong arguments (no openid.return_to and openid.trust_root)
1311        $response = new Zend_OpenId_ResponseHelper(true);
1312        $this->assertFalse( $provider->handle(array(
1313            'openid_mode'=>'checkid_setup'),
1314            null, $response) );
1315
1316        // Unexistent user
1317        $storage->delUser(self::USER);
1318        $response = new Zend_OpenId_ResponseHelper(true);
1319        $this->assertTrue($provider->handle(array(
1320            'openid_mode'=>'checkid_setup',
1321            'openid_identity'=>self::USER,
1322            'openid_return_to'=>'http://www.test.com/test.php'),
1323            null, $response));
1324        $headers = $response->getHeaders();
1325        $this->assertSame( 'Location', $headers[0]['name'] );
1326        $this->assertSame( 'http://www.test.com/test.php?openid.mode=cancel', $headers[0]['value'] );
1327
1328        // No openid_identity
1329        $response = new Zend_OpenId_ResponseHelper(true);
1330        $this->assertTrue($provider->handle(array(
1331            'openid_mode'=>'checkid_setup',
1332            'openid_return_to'=>'http://www.test.com/test.php'),
1333            null, $response));
1334        $headers = $response->getHeaders();
1335        $this->assertSame( 'Location', $headers[0]['name'] );
1336        $url = parse_url($headers[0]['value']);
1337        $this->assertSame( 'www.test.com', $url['host'] );
1338        $this->assertSame( '/server.php', $url['path'] );
1339        $query = array();
1340        foreach (explode('&', $url['query']) as $line) {
1341            list($key,$val) = explode('=', $line, 2);
1342            $query[$key] = urldecode($val);
1343        }
1344        $this->assertSame( 'login', $query['openid.action'] );
1345        $this->assertSame( 'checkid_setup', $query['openid.mode'] );
1346        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1347
1348        // Non logged in user
1349        $provider->register(self::USER, self::PASSWORD);
1350        $response = new Zend_OpenId_ResponseHelper(true);
1351        $this->assertTrue($provider->handle(array(
1352            'openid_mode'=>'checkid_setup',
1353            'openid_identity'=>self::USER,
1354            'openid_return_to'=>'http://www.test.com/test.php'),
1355            null, $response));
1356        $headers = $response->getHeaders();
1357        $this->assertSame( 'Location', $headers[0]['name'] );
1358        $url = parse_url($headers[0]['value']);
1359        $this->assertSame( 'www.test.com', $url['host'] );
1360        $this->assertSame( '/server.php', $url['path'] );
1361        $query = array();
1362        foreach (explode('&', $url['query']) as $line) {
1363            list($key,$val) = explode('=', $line, 2);
1364            $query[$key] = urldecode($val);
1365        }
1366        $this->assertSame( 'login', $query['openid.action'] );
1367        $this->assertSame( 'checkid_setup', $query['openid.mode'] );
1368        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1369        $this->assertSame( self::USER, $query['openid.identity'] );
1370
1371        // Logged in user (unknown site)
1372        $this->assertTrue( $provider->login(self::USER, self::PASSWORD) );
1373        $response = new Zend_OpenId_ResponseHelper(true);
1374        $this->assertTrue($provider->handle(array(
1375            'openid_mode'=>'checkid_setup',
1376            'openid_identity'=>self::USER,
1377            'openid_return_to'=>'http://www.test.com/test.php'),
1378            null, $response));
1379        $headers = $response->getHeaders();
1380        $this->assertSame( 'Location', $headers[0]['name'] );
1381        $url = parse_url($headers[0]['value']);
1382        $this->assertSame( 'www.test.com', $url['host'] );
1383        $this->assertSame( '/server.php', $url['path'] );
1384        $query = array();
1385        foreach (explode('&', $url['query']) as $line) {
1386            list($key,$val) = explode('=', $line, 2);
1387            $query[$key] = urldecode($val);
1388        }
1389        $this->assertSame( 'trust', $query['openid.action'] );
1390        $this->assertSame( 'checkid_setup', $query['openid.mode'] );
1391        $this->assertSame( self::USER, $query['openid.identity'] );
1392        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1393
1394        // Logged in user (untrusted site)
1395        $this->assertTrue( $provider->denySite('http://www.test.com/') );
1396        $response = new Zend_OpenId_ResponseHelper(true);
1397        $this->assertTrue($provider->handle(array(
1398            'openid_mode'=>'checkid_setup',
1399            'openid_identity'=>self::USER,
1400            'openid_return_to'=>'http://www.test.com/test.php'),
1401            null, $response));
1402        $headers = $response->getHeaders();
1403        $this->assertSame( 'Location', $headers[0]['name'] );
1404        $this->assertSame( 'http://www.test.com/test.php?openid.mode=cancel', $headers[0]['value'] );
1405
1406        // Logged in user (trusted site)
1407        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1408        $response = new Zend_OpenId_ResponseHelper(true);
1409        $this->assertTrue($provider->handle(array(
1410            'openid_mode'=>'checkid_setup',
1411            'openid_identity'=>self::USER,
1412            'openid_return_to'=>'http://www.test.com/test.php'),
1413            null, $response));
1414        $headers = $response->getHeaders();
1415        $this->assertSame( 'Location', $headers[0]['name'] );
1416        $url = parse_url($headers[0]['value']);
1417        $this->assertSame( 'www.test.com', $url['host'] );
1418        $this->assertSame( '/test.php', $url['path'] );
1419        $query = array();
1420        foreach (explode('&', $url['query']) as $line) {
1421            list($key,$val) = explode('=', $line, 2);
1422            $query[$key] = urldecode($val);
1423        }
1424        $this->assertSame( 'id_res', $query['openid.mode'] );
1425        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1426        $this->assertSame( self::USER, $query['openid.identity'] );
1427        $this->assertTrue( isset($query['openid.assoc_handle']) );
1428        $this->assertTrue( isset($query['openid.response_nonce']) );
1429        $this->assertTrue( isset($query['openid.signed']) );
1430        $this->assertTrue( isset($query['openid.sig']) );
1431        $this->assertSame( 20, strlen(base64_decode($query['openid.sig'])) );
1432
1433        // Logged in user (trusted site without openid.return_to)
1434        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1435        $response = new Zend_OpenId_ResponseHelper(true);
1436        $this->assertTrue($provider->handle(array(
1437            'openid_mode'=>'checkid_setup',
1438            'openid_identity'=>self::USER,
1439            'openid_trust_root'=>'http://www.test.com/test.php'),
1440            null, $response));
1441        $this->assertSame( array(), $response->getHeaders() );
1442        $this->assertSame( '', $response->getBody() );
1443
1444        // Logged in user (trusted site) & OpenID 2.0 & established session
1445        $storage->delAssociation(self::HANDLE);
1446        $storage->addAssociation(self::HANDLE, "sha1", pack("H*", '0102030405060708091011121314151617181920'), time() + 3660);
1447        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1448        $response = new Zend_OpenId_ResponseHelper(true);
1449        $this->assertTrue($provider->handle(array(
1450            'openid_ns'=>Zend_OpenId::NS_2_0,
1451            'openid_assoc_handle'=>self::HANDLE,
1452            'openid_mode'=>'checkid_setup',
1453            'openid_identity'=>self::USER,
1454            'openid_return_to'=>'http://www.test.com/test.php'),
1455            null, $response));
1456        $headers = $response->getHeaders();
1457        $this->assertSame( 'Location', $headers[0]['name'] );
1458        $url = parse_url($headers[0]['value']);
1459        $this->assertSame( 'www.test.com', $url['host'] );
1460        $this->assertSame( '/test.php', $url['path'] );
1461        $query = array();
1462        foreach (explode('&', $url['query']) as $line) {
1463            list($key,$val) = explode('=', $line, 2);
1464            $query[$key] = urldecode($val);
1465        }
1466        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1467        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1468        $this->assertSame( 'id_res', $query['openid.mode'] );
1469        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1470        $this->assertSame( self::USER, $query['openid.identity'] );
1471        $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
1472        $this->assertTrue( isset($query['openid.response_nonce']) );
1473        $this->assertTrue( isset($query['openid.signed']) );
1474        $this->assertTrue( isset($query['openid.sig']) );
1475        $this->assertSame( 20, strlen(base64_decode($query['openid.sig'])) );
1476
1477        // Logged in user (trusted site) & invalid association handle
1478        $storage->delAssociation(self::HANDLE);
1479        $this->assertTrue( $provider->allowSite('http://www.test.com/') );
1480        $response = new Zend_OpenId_ResponseHelper(true);
1481        $this->assertTrue($provider->handle(array(
1482            'openid_ns'=>Zend_OpenId::NS_2_0,
1483            'openid_assoc_handle'=>self::HANDLE,
1484            'openid_mode'=>'checkid_setup',
1485            'openid_identity'=>self::USER,
1486            'openid_return_to'=>'http://www.test.com/test.php'),
1487            null, $response));
1488        $headers = $response->getHeaders();
1489        $this->assertSame( 'Location', $headers[0]['name'] );
1490        $url = parse_url($headers[0]['value']);
1491        $this->assertSame( 'www.test.com', $url['host'] );
1492        $this->assertSame( '/test.php', $url['path'] );
1493        $query = array();
1494        foreach (explode('&', $url['query']) as $line) {
1495            list($key,$val) = explode('=', $line, 2);
1496            $query[$key] = urldecode($val);
1497        }
1498        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1499        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1500        $this->assertSame( 'id_res', $query['openid.mode'] );
1501        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1502        $this->assertSame( self::USER, $query['openid.identity'] );
1503        $this->assertSame( self::HANDLE, $query['openid.invalidate_handle'] );
1504        $this->assertTrue( isset($query['openid.assoc_handle']) );
1505        $this->assertTrue( isset($query['openid.response_nonce']) );
1506        $this->assertTrue( isset($query['openid.signed']) );
1507        $this->assertTrue( isset($query['openid.sig']) );
1508        $this->assertSame( 32, strlen(base64_decode($query['openid.sig'])) );
1509
1510        // SREG success
1511        $sreg = new Zend_OpenId_Extension_Sreg(array('nickname'=>'test','email'=>'test@test.com'));
1512        $this->assertTrue( $provider->allowSite('http://www.test.com/', $sreg) );
1513        $sreg = new Zend_OpenId_Extension_Sreg();
1514        $response = new Zend_OpenId_ResponseHelper(true);
1515        $this->assertTrue($provider->handle(array(
1516            'openid_ns'=>Zend_OpenId::NS_2_0,
1517            'openid_mode'=>'checkid_setup',
1518            'openid_identity'=>self::USER,
1519            'openid_return_to'=>'http://www.test.com/test.php',
1520            'openid_ns_sreg'=>Zend_OpenId_Extension_Sreg::NAMESPACE_1_1,
1521            'openid_sreg_required'=>'nickname',
1522            'openid_sreg_optional'=>'email',
1523            ),
1524            $sreg, $response));
1525        $headers = $response->getHeaders();
1526        $this->assertSame( 'Location', $headers[0]['name'] );
1527        $url = parse_url($headers[0]['value']);
1528        $this->assertSame( 'www.test.com', $url['host'] );
1529        $this->assertSame( '/test.php', $url['path'] );
1530        $query = array();
1531        foreach (explode('&', $url['query']) as $line) {
1532            list($key,$val) = explode('=', $line, 2);
1533            $query[$key] = urldecode($val);
1534        }
1535        $this->assertSame( Zend_OpenId::NS_2_0, $query['openid.ns'] );
1536        $this->assertSame( "http://www.test.com/server.php", $query['openid.op_endpoint'] );
1537        $this->assertSame( 'id_res', $query['openid.mode'] );
1538        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1539        $this->assertSame( self::USER, $query['openid.identity'] );
1540        $this->assertTrue( isset($query['openid.assoc_handle']) );
1541        $this->assertTrue( isset($query['openid.response_nonce']) );
1542        $this->assertTrue( isset($query['openid.signed']) );
1543        $this->assertTrue( isset($query['openid.sig']) );
1544        $this->assertSame( 32, strlen(base64_decode($query['openid.sig'])) );
1545        $this->assertSame( Zend_OpenId_Extension_Sreg::NAMESPACE_1_1, $query['openid.ns.sreg'] );
1546        $this->assertSame( 'test', $query['openid.sreg.nickname'] );
1547        $this->assertSame( 'test@test.com', $query['openid.sreg.email'] );
1548
1549        // SREG failed
1550        $sreg = new Zend_OpenId_Extension_Sreg(array('nickname'=>'test'));
1551        $this->assertTrue( $provider->allowSite('http://www.test.com/', $sreg) );
1552        $sreg = new Zend_OpenId_Extension_Sreg();
1553        $response = new Zend_OpenId_ResponseHelper(true);
1554        $this->assertTrue($provider->handle(array(
1555            'openid_mode'=>'checkid_setup',
1556            'openid_identity'=>self::USER,
1557            'openid_return_to'=>'http://www.test.com/test.php',
1558            'openid_sreg_required'=>'nickname,email',
1559            ),
1560            $sreg, $response));
1561        $headers = $response->getHeaders();
1562        $this->assertSame( 'Location', $headers[0]['name'] );
1563        $url = parse_url($headers[0]['value']);
1564        $this->assertSame( 'www.test.com', $url['host'] );
1565        $this->assertSame( '/server.php', $url['path'] );
1566        $query = array();
1567        foreach (explode('&', $url['query']) as $line) {
1568            list($key,$val) = explode('=', $line, 2);
1569            $query[$key] = urldecode($val);
1570        }
1571        $this->assertSame( 'trust', $query['openid.action'] );
1572        $this->assertSame( 'checkid_setup', $query['openid.mode'] );
1573        $this->assertSame( self::USER, $query['openid.identity'] );
1574        $this->assertSame( 'http://www.test.com/test.php', $query['openid.return_to'] );
1575        $this->assertSame( 'nickname,email', $query['openid.sreg.required'] );
1576
1577        $provider->logout();
1578        $storage->delUser(self::USER);
1579    }
1580
1581    /**
1582     * testing handle
1583     *
1584     */
1585    public function testHandle()
1586    {
1587        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider"));
1588
1589        // no openid_mode
1590        $this->assertFalse( $provider->handle(array()) );
1591
1592        // wrong openid_mode
1593        $this->assertFalse( $provider->handle(array('openid_mode'=>'wrong')) );
1594    }
1595
1596    /**
1597     * testing setOpEndpoint
1598     *
1599     */
1600    public function testSetOpEndpoint()
1601    {
1602        $storage = new Zend_OpenId_Provider_Storage_File(dirname(__FILE__)."/_files/provider");
1603        $provider = new Zend_OpenId_ProviderHelper(null, null, $this->_user, $storage);
1604        $provider->setOpEndpoint("http://www.test.com/real_endpoint.php");
1605
1606        // OpenID 2.0 with SHA256
1607        $_SERVER['SCRIPT_URI'] = "http://www.test.com/endpoint.php";
1608        $response = new Zend_OpenId_ResponseHelper(true);
1609        $storage->addAssociation(self::HANDLE, "sha256", pack("H*", '0102030405060708091011121314151617181920212223242526272829303132'), time() + 3660);
1610        $this->assertTrue( $provider->respondToConsumer(array(
1611                'openid_ns' => Zend_OpenId::NS_2_0,
1612                'openid_assoc_handle' => self::HANDLE,
1613                'openid_return_to' => 'http://www.test.com/test.php'
1614            ), null, $response) );
1615        $headers = $response->getHeaders();
1616        $this->assertSame( 'Location', $headers[0]['name'] );
1617        $url = parse_url($headers[0]['value']);
1618        $this->assertSame( 'www.test.com', $url['host'] );
1619        $this->assertSame( '/test.php', $url['path'] );
1620        $ret = array();
1621        foreach (explode('&', $url['query']) as $line) {
1622            list($key,$val) = explode('=', $line, 2);
1623            $ret[$key] = urldecode($val);
1624        }
1625        $this->assertSame( 'id_res', $ret['openid.mode'] );
1626        $this->assertSame( 'http://www.test.com/test.php', $ret['openid.return_to'] );
1627        $this->assertSame( self::HANDLE, $ret['openid.assoc_handle'] );
1628        $this->assertTrue( isset($ret['openid.response_nonce']) );
1629        $this->assertTrue( isset($ret['openid.signed']) );
1630        $this->assertTrue( isset($ret['openid.sig']) );
1631        $this->assertSame( Zend_OpenId::NS_2_0, $ret['openid.ns'] );
1632        $this->assertSame( "http://www.test.com/real_endpoint.php", $ret['openid.op_endpoint'] );
1633        $this->assertTrue( $storage->getAssociation(self::HANDLE, $macFunc, $secret, $expires) );
1634        $this->assertSame( 'sha256', $macFunc );
1635        $storage->delAssociation(self::HANDLE);
1636    }
1637}
1638
1639class Zend_OpenId_ProviderHelper extends Zend_OpenId_Provider
1640{
1641    public function genSecret($func)
1642    {
1643        return $this->_genSecret($func);
1644    }
1645}