Source for file Request.php

Documentation is available at Request.php

  1. <?php
  2. /**
  3.  * OpenID_Association_Request
  4.  * 
  5.  * PHP Version 5.2.0+
  6.  * 
  7.  * @uses      OpenID
  8.  * @category  Auth
  9.  * @package   OpenID
  10.  * @author    Bill Shupp <hostmaster@shupp.org>
  11.  * @copyright 2009 Bill Shupp
  12.  * @license   http://www.opensource.org/licenses/bsd-license.php FreeBSD
  13.  * @link      http://pearopenid.googlecode.com
  14.  */
  15.  
  16. /**
  17.  * Required files
  18.  */
  19. require_once 'OpenID.php';
  20. require_once 'OpenID/Association/Exception.php';
  21. require_once 'OpenID/Association.php';
  22. require_once 'OpenID/Message.php';
  23. require_once 'OpenID/Association/DiffieHellman.php';
  24.  
  25. /**
  26.  * OpenID_Association_Request
  27.  * 
  28.  * Request object for establishing OpenID Associations.
  29.  * 
  30.  * @uses      OpenID
  31.  * @category  Auth
  32.  * @package   OpenID
  33.  * @author    Bill Shupp <hostmaster@shupp.org>
  34.  * @copyright 2009 Bill Shupp
  35.  * @license   http://www.opensource.org/licenses/bsd-license.php FreeBSD
  36.  * @link      http://pearopenid.googlecode.com
  37.  */
  38. {
  39.     /**
  40.      * OpenID provider endpoint URL
  41.      * 
  42.      * @var string 
  43.      */
  44.     protected $opEndpointURL = null;
  45.  
  46.     /**
  47.      * Contains contents of the association request
  48.      * 
  49.      * @var OpenID_Message 
  50.      */
  51.     protected $message = null;
  52.  
  53.     /**
  54.      * Version of OpenID in use.  This determines which algorithms we can use.
  55.      * 
  56.      * @var string 
  57.      */
  58.     protected $version = null;
  59.  
  60.     /**
  61.      * The association request response in array format
  62.      * 
  63.      * @var array 
  64.      * @see getResponse()
  65.      */
  66.     protected $response = array();
  67.  
  68.     /**
  69.      * Optional instance of Crypt_DiffieHellman
  70.      * 
  71.      * @var Crypt_DiffieHellman 
  72.      */
  73.     protected $cdh = null;
  74.  
  75.     /**
  76.      * OpenID_Association_DiffieHellman instance
  77.      * 
  78.      * @var OpenID_Association_DiffieHellman 
  79.      */
  80.     protected $dh = null;
  81.  
  82.     /**
  83.      * Sets the arguments passed in, as well as creates the request message.
  84.      * 
  85.      * @param string              $opEndpointURL URL of OP Endpoint
  86.      * @param string              $version       Version of OpenID in use
  87.      * @param Crypt_DiffieHellman $cdh           Custom Crypt_DiffieHellman
  88.      *                                            instance
  89.      * 
  90.      * @return void 
  91.      */
  92.     public function __construct($opEndpointURL,
  93.                                 $version,
  94.                                 Crypt_DiffieHellman $cdh null)
  95.     {
  96.         if (!array_key_exists($versionOpenID::$versionMap)) {
  97.             throw new OpenID_Association_Exception(
  98.                 'Invalid version'
  99.             );
  100.         }
  101.         $this->version       = $version;
  102.         $this->opEndpointURL = $opEndpointURL;
  103.         $this->message       = new OpenID_Message;
  104.  
  105.         if ($cdh{
  106.             $this->cdh = $cdh;
  107.         }
  108.  
  109.         // Set defaults
  110.         $this->message->set('openid.mode'OpenID::MODE_ASSOCIATE);
  111.         if (OpenID::$versionMap[$version== OpenID::NS_2_0{
  112.             $this->message->set('openid.ns'OpenID::NS_2_0);
  113.             $this->message->set('openid.assoc_type'self::ASSOC_TYPE_HMAC_SHA256);
  114.             $this->message->set('openid.session_type'self::SESSION_TYPE_DH_SHA256);
  115.         else {
  116.             $this->message->set('openid.assoc_type'self::ASSOC_TYPE_HMAC_SHA1);
  117.             $this->message->set('openid.session_type'self::SESSION_TYPE_DH_SHA1);
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Sends the association request.  Loops over errors and adapts to
  123.      * 'unsupported-type' responses.
  124.      * 
  125.      * @return mixed OpenID_Association on success, false on failure
  126.      * @see buildAssociation()
  127.      * @see sendAssociationRequest()
  128.      */
  129.     public function associate()
  130.     {
  131.         $count 0;
  132.         while ($count 2{
  133.             // Easier to operate on array format here
  134.             $response $this->sendAssociationRequest()->getArrayFormat();
  135.  
  136.             if (isset($response['assoc_handle'])) {
  137.                 $this->response = $response;
  138.                 return $this->buildAssociation($response);
  139.             }
  140.  
  141.             if (isset($response['mode'])
  142.                 && $response['mode'== OpenID::MODE_ERROR
  143.                 && isset($response['error_code'])
  144.                 && $response['error_code'== 'unsupported-type'{
  145.     
  146.                 if (isset($response['assoc_type'])) {
  147.                     $this->setAssociationType($response['assoc_type']);
  148.                 }
  149.                 if (isset($response['session_type'])) {
  150.                     $this->setSessionType($response['session_type']);
  151.                 }
  152.             }
  153.             $count++;
  154.         }
  155.         return false;
  156.     }
  157.  
  158.     /**
  159.      * Build the OpenID_Association class based on the association response
  160.      * 
  161.      * @param array $response Association response in array format
  162.      * 
  163.      * @return OpenID_Association 
  164.      * @see associate()
  165.      */
  166.     protected function buildAssociation(array $response)
  167.     {
  168.         $params                array();
  169.         $params['created']     time();
  170.         $params['expiresIn']   $response['expires_in'];
  171.         $params['uri']         $this->opEndpointURL;
  172.         $params['assocType']   $this->getAssociationType();
  173.         $params['assocHandle'$response['assoc_handle'];
  174.  
  175.         if ($this->getSessionType(=== self::SESSION_TYPE_NO_ENCRYPTION{
  176.             if (!isset($response['mac_key'])) {
  177.                 throw new OpenID_Association_Exception(
  178.                     'Missing mac_key in association response'
  179.                 );
  180.             }
  181.             $params['sharedSecret'$response['mac_key'];
  182.         else {
  183.             $this->getDH()->getSharedSecret($response$params);
  184.         }
  185.  
  186.         return new OpenID_Association($params);
  187.     }
  188.  
  189.     /**
  190.      * Actually sends the assocition request to the OP Endpoing URL.
  191.      * 
  192.      * @return OpenID_Message 
  193.      * @see associate()
  194.      */
  195.     protected function sendAssociationRequest()
  196.     {
  197.         if ($this->message->get('openid.session_type')
  198.             == self::SESSION_TYPE_NO_ENCRYPTION{
  199.  
  200.             $this->message->delete('openid.dh_consumer_public');
  201.             $this->message->delete('openid.dh_modulus');
  202.             $this->message->delete('openid.dh_gen');
  203.         else {
  204.             $this->initDH();
  205.         }
  206.  
  207.         $response $this->directRequest($this->opEndpointURL$this->message);
  208.         $message  new OpenID_Message($response->getBody(),
  209.                                        OpenID_Message::FORMAT_KV);
  210.  
  211.         OpenID::setLastEvent(__METHOD__print_r($message->getArrayFormat()true));
  212.  
  213.         return $message;
  214.     }
  215.  
  216.     /**
  217.      * Gets the last association response
  218.      * 
  219.      * @return void 
  220.      */
  221.     public function getResponse()
  222.     {
  223.         return $this->response;
  224.     }
  225.  
  226.     /**
  227.      * Initialize the diffie-hellman parameters for the association request.
  228.      * 
  229.      * @return void 
  230.      */
  231.     protected function initDH()
  232.     {
  233.         $this->getDH()->init();
  234.     }
  235.  
  236.     /**
  237.      * Gets an instance of OpenID_Association_DiffieHellman.  If one is not already
  238.      * instanciated, a new one is returned.
  239.      * 
  240.      * @return OpenID_Association_DiffieHellman 
  241.      */
  242.     protected function getDH()
  243.     {
  244.         if (!$this->dh{
  245.             $this->dh new OpenID_Association_DiffieHellman($this->message,
  246.                                                              $this->cdh);
  247.         }
  248.         return $this->dh;
  249.     }
  250.  
  251.     /**
  252.      * Sets he association type for the request.  Can be sha1 or sha256.
  253.      * 
  254.      * @param string $type sha1 or sha256
  255.      * 
  256.      * @throws OpenID_Association_Exception on invalid type
  257.      * @return void 
  258.      */
  259.     public function setAssociationType($type)
  260.     {
  261.         switch ($type{
  262.         case self::ASSOC_TYPE_HMAC_SHA1:
  263.         case self::ASSOC_TYPE_HMAC_SHA256:
  264.             $this->message->set('openid.assoc_type'$type);
  265.             break;
  266.         default:
  267.             throw new OpenID_Association_Exception("Invalid assoc_type: $type");
  268.         }
  269.     }
  270.  
  271.     /**
  272.      * Gets the current association type
  273.      * 
  274.      * @return void 
  275.      */
  276.     public function getAssociationType()
  277.     {
  278.         return $this->message->get('openid.assoc_type');
  279.     }
  280.  
  281.     /**
  282.      * Sets the session type.  Can be sha1, sha256, or no-encryption
  283.      * 
  284.      * @param string $type sha1, sha256, or no-encryption
  285.      * 
  286.      * @throws OpenID_Association_Exception on invalid type, or if you set
  287.      *          no-encryption for an OP URL that doesn't support HTTPS
  288.      * @return void 
  289.      */
  290.     public function setSessionType($type)
  291.     {
  292.         switch ($type{
  293.         case self::SESSION_TYPE_NO_ENCRYPTION:
  294.             // Make sure we're using SSL
  295.             if (!preg_match('@^https://@i'$this->opEndpointURL)) {
  296.                 throw new OpenID_Association_Exception(
  297.                     'Un-encrypted sessions require HTTPS'
  298.                 );
  299.             }
  300.             $this->message->set('openid.session_type',
  301.                                 self::SESSION_TYPE_NO_ENCRYPTION);
  302.             break;
  303.         case self::SESSION_TYPE_DH_SHA1:
  304.         case self::SESSION_TYPE_DH_SHA256:
  305.             $this->message->set('openid.session_type'$type);
  306.             break;
  307.         default:
  308.             throw new OpenID_Association_Exception("Invalid session_type: $type");
  309.         }
  310.     }
  311.  
  312.     /**
  313.      * Gets the current session type
  314.      * 
  315.      * @return string Current session type (sha1, sha256, or no-encryption)
  316.      */
  317.     public function getSessionType()
  318.     {
  319.         return $this->message->get('openid.session_type');
  320.     }
  321.  
  322.     /**
  323.      * Gets the OP Endpoint URL
  324.      * 
  325.      * @return string OP Endpoint URL
  326.      */
  327.     public function getEndpointURL()
  328.     {
  329.         return $this->opEndpointURL;
  330.     }
  331. }
  332. ?>

Documentation generated on Tue, 15 Dec 2009 19:00:58 -0800 by phpDocumentor 1.4.3