Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: OpenID

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Auth/OpenID/Association.php

Documentation is available at Association.php

  1. <?php
  2.  
  3. /**
  4.  * This module contains code for dealing with associations between
  5.  * consumers and servers.
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: See the COPYING file included in this distribution.
  10.  *
  11.  * @package OpenID
  12.  * @author JanRain, Inc. <[email protected]>
  13.  * @copyright 2005 Janrain, Inc.
  14.  * @license http://www.gnu.org/copyleft/lesser.html LGPL
  15.  */
  16.  
  17. /**
  18.  * @access private
  19.  */
  20. require_once 'Auth/OpenID/CryptUtil.php';
  21.  
  22. /**
  23.  * @access private
  24.  */
  25. require_once 'Auth/OpenID/KVForm.php';
  26.  
  27. /**
  28.  * This class represents an association between a server and a
  29.  * consumer.  In general, users of this library will never see
  30.  * instances of this object.  The only exception is if you implement a
  31.  * custom {@link Auth_OpenID_OpenIDStore}.
  32.  *
  33.  * If you do implement such a store, it will need to store the values
  34.  * of the handle, secret, issued, lifetime, and assoc_type instance
  35.  * variables.
  36.  *
  37.  * @package OpenID
  38.  */
  39.  
  40.     /**
  41.      * This is a HMAC-SHA1 specific value.
  42.      *
  43.      * @access private
  44.      */
  45.     var $SIG_LENGTH 20;
  46.  
  47.     /**
  48.      * The ordering and name of keys as stored by serialize.
  49.      *
  50.      * @access private
  51.      */
  52.     var $assoc_keys array(
  53.                             'version',
  54.                             'handle',
  55.                             'secret',
  56.                             'issued',
  57.                             'lifetime',
  58.                             'assoc_type'
  59.                             );
  60.  
  61.     /**
  62.      * This is an alternate constructor (factory method) used by the
  63.      * OpenID consumer library to create associations.  OpenID store
  64.      * implementations shouldn't use this constructor.
  65.      *
  66.      * @access private
  67.      *
  68.      * @param integer $expires_in This is the amount of time this
  69.      *  association is good for, measured in seconds since the
  70.      *  association was issued.
  71.      *
  72.      * @param string $handle This is the handle the server gave this
  73.      *  association.
  74.      *
  75.      * @param string secret This is the shared secret the server
  76.      *  generated for this association.
  77.      *
  78.      * @param assoc_type This is the type of association this
  79.      *  instance represents.  The only valid value of this field at
  80.      *  this time is 'HMAC-SHA1', but new types may be defined in the
  81.      *  future.
  82.      *
  83.      * @return association An {@link Auth_OpenID_Association}
  84.      *  instance.
  85.      */
  86.     function fromExpiresIn($expires_in$handle$secret$assoc_type)
  87.     {
  88.         $issued time();
  89.         $lifetime $expires_in;
  90.         return new Auth_OpenID_Association($handle$secret,
  91.                                            $issued$lifetime$assoc_type);
  92.     }
  93.  
  94.     /**
  95.      * This is the standard constructor for creating an association.
  96.      * The library should create all of the necessary associations, so
  97.      * this constructor is not part of the external API.
  98.      *
  99.      * @access private
  100.      *
  101.      * @param string $handle This is the handle the server gave this
  102.      *  association.
  103.      *
  104.      * @param string $secret This is the shared secret the server
  105.      *  generated for this association.
  106.      *
  107.      * @param integer $issued This is the time this association was
  108.      *  issued, in seconds since 00:00 GMT, January 1, 1970.  (ie, a
  109.      *  unix timestamp)
  110.      *
  111.      * @param integer $lifetime This is the amount of time this
  112.      *  association is good for, measured in seconds since the
  113.      *  association was issued.
  114.      *
  115.      * @param string $assoc_type This is the type of association this
  116.      *  instance represents.  The only valid value of this field at
  117.      *  this time is 'HMAC-SHA1', but new types may be defined in the
  118.      *  future.
  119.      */
  120.     function Auth_OpenID_Association(
  121.         $handle$secret$issued$lifetime$assoc_type)
  122.     {
  123.         if ($assoc_type != 'HMAC-SHA1'{
  124.             $fmt 'HMAC-SHA1 is the only supported association type (got %s)';
  125.             trigger_error(sprintf($fmt$assoc_type)E_USER_ERROR);
  126.         }
  127.  
  128.         $this->handle $handle;
  129.         $this->secret $secret;
  130.         $this->issued $issued;
  131.         $this->lifetime $lifetime;
  132.         $this->assoc_type $assoc_type;
  133.     }
  134.  
  135.     /**
  136.      * This returns the number of seconds this association is still
  137.      * valid for, or 0 if the association is no longer valid.
  138.      *
  139.      * @return integer $seconds The number of seconds this association
  140.      *  is still valid for, or 0 if the association is no longer valid.
  141.      */
  142.     function getExpiresIn($now null)
  143.     {
  144.         if ($now == null{
  145.             $now time();
  146.         }
  147.  
  148.         return max(0$this->issued $this->lifetime $now);
  149.     }
  150.  
  151.     /**
  152.      * This checks to see if two {@link Auth_OpenID_Association}
  153.      * instances represent the same association.
  154.      *
  155.      * @return bool $result true if the two instances represent the
  156.      *  same association, false otherwise.
  157.      */
  158.     function equal($other)
  159.     {
  160.         return ((gettype($this== gettype($other))
  161.                 && ($this->handle == $other->handle)
  162.                 && ($this->secret == $other->secret)
  163.                 && ($this->issued == $other->issued)
  164.                 && ($this->lifetime == $other->lifetime)
  165.                 && ($this->assoc_type == $other->assoc_type));
  166.     }
  167.  
  168.     /**
  169.      * Convert an association to KV form.
  170.      *
  171.      * @return string $result String in KV form suitable for
  172.      *  deserialization by deserialize.
  173.      */
  174.     function serialize()
  175.     {
  176.         $data array(
  177.                      'version' => '2',
  178.                      'handle' => $this->handle,
  179.                      'secret' => base64_encode($this->secret),
  180.                      'issued' => strval(intval($this->issued)),
  181.                      'lifetime' => strval(intval($this->lifetime)),
  182.                      'assoc_type' => $this->assoc_type
  183.                      );
  184.  
  185.         assert(array_keys($data== $this->assoc_keys);
  186.  
  187.         return Auth_OpenID_KVForm::fromArray($data$strict true);
  188.     }
  189.  
  190.     /**
  191.      * Parse an association as stored by serialize().  This is the
  192.      * inverse of serialize.
  193.      *
  194.      * @param string $assoc_s Association as serialized by serialize()
  195.      * @return Auth_OpenID_Association $result instance of this class
  196.      */
  197.     function deserialize($class_name$assoc_s)
  198.     {
  199.         $pairs Auth_OpenID_KVForm::toArray($assoc_s$strict true);
  200.         $keys array();
  201.         $values array();
  202.         foreach ($pairs as $key => $value{
  203.             if (is_array($value)) {
  204.                 list($key$value$value;
  205.             }
  206.             $keys[$key;
  207.             $values[$value;
  208.         }
  209.  
  210.         $class_vars get_class_vars($class_name);
  211.         $class_assoc_keys $class_vars['assoc_keys'];
  212.  
  213.         sort($keys);
  214.         sort($class_assoc_keys);
  215.  
  216.         if ($keys != $class_assoc_keys{
  217.             trigger_error('Unexpected key values: ' strval($keys),
  218.                           E_USER_WARNING);
  219.             return null;
  220.         }
  221.  
  222.         $version $pairs['version'];
  223.         $handle $pairs['handle'];
  224.         $secret $pairs['secret'];
  225.         $issued $pairs['issued'];
  226.         $lifetime $pairs['lifetime'];
  227.         $assoc_type $pairs['assoc_type'];
  228.  
  229.         if ($version != '2'{
  230.             trigger_error('Unknown version: ' $versionE_USER_WARNING);
  231.             return null;
  232.         }
  233.  
  234.         $issued intval($issued);
  235.         $lifetime intval($lifetime);
  236.         $secret base64_decode($secret);
  237.  
  238.         return new $class_name(
  239.             $handle$secret$issued$lifetime$assoc_type);
  240.     }
  241.  
  242.     /**
  243.      * Generate a signature for a sequence of (key, value) pairs
  244.      *
  245.      * @access private
  246.      * @param array $pairs The pairs to sign, in order.  This is an
  247.      *  array of two-tuples.
  248.      * @return string $signature The binary signature of this sequence
  249.      *  of pairs
  250.      */
  251.     function sign($pairs)
  252.     {
  253.         $kv Auth_OpenID_KVForm::fromArray($pairs);
  254.         return Auth_OpenID_HMACSHA1($this->secret$kv);
  255.     }
  256.  
  257.     /**
  258.      * Generate a signature for some fields in a dictionary
  259.      *
  260.      * @access private
  261.      * @param array $fields The fields to sign, in order; this is an
  262.      *  array of strings.
  263.      * @param array $data Dictionary of values to sign (an array of
  264.      *  string => string pairs).
  265.      * @return string $signature The signature, base64 encoded
  266.      */
  267.     function signDict($fields$data$prefix 'openid.')
  268.     {
  269.         $pairs array();
  270.         foreach ($fields as $field{
  271.             $pairs[array($field$data[$prefix $field]);
  272.         }
  273.  
  274.         return base64_encode($this->sign($pairs));
  275.     }
  276.  
  277.     /**
  278.      * Add a signature to an array of fields
  279.      *
  280.      * @access private
  281.      */
  282.     function addSignature($fields&$data$prefix 'openid.')
  283.     {
  284.         $sig $this->signDict($fields$data$prefix);
  285.         $signed implode(","$fields);
  286.         $data[$prefix 'sig'$sig;
  287.         $data[$prefix 'signed'$signed;
  288.     }
  289.  
  290.     /**
  291.      * Confirm that the signature of these fields matches the
  292.      * signature contained in the data
  293.      *
  294.      * @access private
  295.      */
  296.     function checkSignature($data$prefix 'openid.')
  297.     {
  298.         $signed $data[$prefix 'signed'];
  299.         $fields explode(","$signed);
  300.         $expected_sig $this->signDict($fields$data$prefix);
  301.         $request_sig $data[$prefix 'sig'];
  302.  
  303.         return ($request_sig == $expected_sig);
  304.     }
  305. }
  306.  
  307. ?>

Documentation generated on Mon, 05 Mar 2007 20:52:35 +0000 by phpDocumentor 1.3.1