Apache Zeta Components Manual :: File Source for openid_db_store.php

Source for file openid_db_store.php

Documentation is available at openid_db_store.php

  1. <?php
  2. /**
  3.  * File containing the ezcAuthenticationOpenidDbStore class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @filesource
  24.  * @package AuthenticationDatabaseTiein
  25.  * @version //autogentag//
  26.  */
  27.  
  28. /**
  29.  * Class providing database storage for OpenID authentication.
  30.  *
  31.  * This class requires that the database used contains two special tables. See
  32.  * the tutorial for information on how to create those tables.
  33.  *
  34.  * Example of use:
  35.  * <code>
  36.  * // create an OpenID options object
  37.  * $options = new ezcAuthenticationOpenidOptions();
  38.  * $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
  39.  *
  40.  * // define a database store
  41.  * $options->store = new ezcAuthenticationOpenidDbStore( ezcDbInstance::get() );
  42.  *
  43.  * // create an OpenID filter based on the options object
  44.  * $filter = new ezcAuthenticationOpenidFilter( $options );
  45.  * </code>
  46.  *
  47.  * @property ezcDbHandler $instance 
  48.  *            The database instance to use for database storage.
  49.  *
  50.  * @package AuthenticationDatabaseTiein
  51.  * @version //autogentag//
  52.  */
  53. {
  54.     /**
  55.      * Holds the properties of this class.
  56.      *
  57.      * @var array(string=>mixed) 
  58.      */
  59.     private $properties array();
  60.  
  61.     /**
  62.      * Creates a new object of this class.
  63.      *
  64.      * @param ezcDbHandler $instance The database instance used for this store
  65.      * @param ezcAuthenticationOpenidDbStoreOptions $options Options for this class
  66.      */
  67.     public function __constructezcDbHandler $instanceezcAuthenticationOpenidDbStoreOptions $options null )
  68.     {
  69.         $this->instance $instance;
  70.         $this->options = $options === null new ezcAuthenticationOpenidDbStoreOptions($options;
  71.     }
  72.  
  73.     /**
  74.      * Sets the property $name to $value.
  75.      *
  76.      * @throws ezcBasePropertyNotFoundException
  77.      *          if the property $name does not exist
  78.      * @throws ezcBaseValueException
  79.      *          if $value is not correct for the property $name
  80.      * @param string $name The name of the property to set
  81.      * @param mixed $value The new value of the property
  82.      * @ignore
  83.      */
  84.     public function __set$name$value )
  85.     {
  86.         switch $name )
  87.         {
  88.             case 'instance':
  89.                 if !$value instanceof ezcDbHandler ) )
  90.                 {
  91.                     throw new ezcBaseValueException$name$value'ezcDbHandler' );
  92.                 }
  93.  
  94.                 $this->properties[$name$value;
  95.                 break;
  96.  
  97.             default:
  98.                 throw new ezcBasePropertyNotFoundException$name );
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Returns the value of the property $name.
  104.      *
  105.      * @throws ezcBasePropertyNotFoundException
  106.      *          if the property $name does not exist
  107.      * @param string $name The name of the property for which to return the value
  108.      * @return mixed 
  109.      * @ignore
  110.      */
  111.     public function __get$name )
  112.     {
  113.         switch $name )
  114.         {
  115.             case 'instance':
  116.                 return $this->properties[$name];
  117.  
  118.             default:
  119.                 throw new ezcBasePropertyNotFoundException$name );
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Returns true if the property $name is set, otherwise false.
  125.      *
  126.      * @param string $name The name of the property to test if it is set
  127.      * @return bool 
  128.      * @ignore
  129.      */
  130.     public function __isset$name )
  131.     {
  132.         switch $name )
  133.         {
  134.             case 'instance':
  135.                 return isset$this->properties[$name);
  136.  
  137.             default:
  138.                 return false;
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Stores the nonce in the store.
  144.      *
  145.      * Returns true if the nonce was stored successfully, and false otherwise.
  146.      *
  147.      * @throws ezcBaseFilePermissionException
  148.      *          if the nonce cannot be written in the store
  149.      * @param string $nonce The nonce value to store
  150.      * @return bool 
  151.      */
  152.     public function storeNonce$nonce )
  153.     {
  154.         $table $this->options->tableNonces;
  155.  
  156.         $query new ezcQueryInsert$this->instance );
  157.  
  158.         $query->insertInto$this->instance->quoteIdentifier$table['name') )
  159.               ->set$this->instance->quoteIdentifier$table['fields']['nonce')$query->bindValue$nonce ) )
  160.               ->set$this->instance->quoteIdentifier$table['fields']['timestamp')$query->bindValuetime() ) );
  161.  
  162.         $stmt $query->prepare();
  163.         $stmt->execute();
  164.  
  165.         return true;
  166.     }
  167.  
  168.     /**
  169.      * Checks if the nonce exists and afterwards deletes it.
  170.      *
  171.      * Returns the timestamp of the nonce if it exists, and false otherwise.
  172.      *
  173.      * @param string $nonce The nonce value to check and delete
  174.      * @return bool|int
  175.      */
  176.     public function useNonce$nonce )
  177.     {
  178.         $table $this->options->tableNonces;
  179.  
  180.         $query new ezcQuerySelect$this->instance );
  181.         $e $query->expr;
  182.         $query->select'*' )
  183.               ->from$this->instance->quoteIdentifier$table['name') )
  184.               ->where(
  185.                   $e->eq$this->instance->quoteIdentifier$table['fields']['nonce')$query->bindValue$nonce ) )
  186.                      );
  187.         $query $query->prepare();
  188.         $query->execute();
  189.         $rows $query->fetchAll();
  190.         if count$rows )
  191.         {
  192.             $rows $rows[0];
  193.             $lastModified = (int) $rows[$table['fields']['timestamp']];
  194.  
  195.             $this->removeNonce$nonce );
  196.  
  197.             return $lastModified;
  198.         }
  199.  
  200.         // $nonce was not found in the database
  201.         return false;
  202.     }
  203.  
  204.     /**
  205.      * Removes the nonce from the nonces table.
  206.      *
  207.      * @param string $nonce 
  208.      */
  209.     protected function removeNonce$nonce )
  210.     {
  211.         $table $this->options->tableNonces;
  212.  
  213.         $query new ezcQueryDelete$this->instance );
  214.         $e $query->expr;
  215.         $query->deleteFrom$this->instance->quoteIdentifier$table['name') )
  216.               ->where(
  217.                   $e->eq$this->instance->quoteIdentifier$table['fields']['nonce')$query->bindValue$nonce ) )
  218.                      );
  219.         $query $query->prepare();
  220.         $query->execute();
  221.     }
  222.  
  223.     /**
  224.      * Stores an association in the store linked to the OpenID provider URL.
  225.      *
  226.      * Returns true always.
  227.      *
  228.      * @param string $url The URL of the OpenID provider
  229.      * @param ezcAuthenticationOpenidAssociation $association The association value to store
  230.      * @return bool 
  231.      */
  232.     public function storeAssociation$url$association )
  233.     {
  234.         $table $this->options->tableAssociations;
  235.         $data serialize$association );
  236.  
  237.         $query new ezcQueryInsert$this->instance );
  238.  
  239.         $query->insertInto$this->instance->quoteIdentifier$table['name') )
  240.               ->set$this->instance->quoteIdentifier$table['fields']['url')$query->bindValue$url ) )
  241.               ->set$this->instance->quoteIdentifier$table['fields']['association')$query->bindValue$data ) );
  242.  
  243.         $stmt $query->prepare();
  244.         $stmt->execute();
  245.  
  246.         return true;
  247.     }
  248.  
  249.     /**
  250.      * Returns the unserialized association linked to the OpenID provider URL.
  251.      *
  252.      * Returns false if the association could not be retrieved or if it expired.
  253.      *
  254.      * @param string $url The URL of the OpenID provider
  255.      * @return ezcAuthenticationOpenidAssociation 
  256.      */
  257.     public function getAssociation$url )
  258.     {
  259.         $table $this->options->tableAssociations;
  260.  
  261.         $query new ezcQuerySelect$this->instance );
  262.         $e $query->expr;
  263.         $query->select'*' )
  264.               ->from$this->instance->quoteIdentifier$table['name') )
  265.               ->where(
  266.                   $e->eq$this->instance->quoteIdentifier$table['fields']['url')$query->bindValue$url ) )
  267.                      );
  268.  
  269.         $query $query->prepare();
  270.         $query->execute();
  271.         $rows $query->fetchAll();
  272.  
  273.         if count$rows )
  274.         {
  275.             $rows $rows[0];
  276.             $data unserialize$rows[$table['fields']['association']] );
  277.  
  278.             return $data;
  279.         }
  280.  
  281.         // no association was found for $url
  282.         return false;
  283.     }
  284.  
  285.     /**
  286.      * Removes the association linked to the OpenID provider URL.
  287.      *
  288.      * Returns true always.
  289.      *
  290.      * @param string $url The URL of the OpenID provider
  291.      * @return bool 
  292.      */
  293.     public function removeAssociation$url )
  294.     {
  295.         $table $this->options->tableAssociations;
  296.  
  297.         $query new ezcQueryDelete$this->instance );
  298.         $e $query->expr;
  299.         $query->deleteFrom$this->instance->quoteIdentifier$table['name') )
  300.               ->where(
  301.                   $e->eq$this->instance->quoteIdentifier$table['fields']['url')$query->bindValue$url ) )
  302.                      );
  303.         $query $query->prepare();
  304.         $query->execute();
  305.  
  306.         return true;
  307.     }
  308. }
  309. ?>
Documentation generated by phpDocumentor 1.4.3