/AuthenticationDatabaseTiein/src/filters/openid/openid_db_store.php

https://github.com/F5/zetacomponents · PHP · 310 lines · 137 code · 35 blank · 138 comment · 6 complexity · 02198e84b36c7960ecf7c8288d402472 MD5 · raw file

  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. * Class providing database storage for OpenID authentication.
  29. *
  30. * This class requires that the database used contains two special tables. See
  31. * the tutorial for information on how to create those tables.
  32. *
  33. * Example of use:
  34. * <code>
  35. * // create an OpenID options object
  36. * $options = new ezcAuthenticationOpenidOptions();
  37. * $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
  38. *
  39. * // define a database store
  40. * $options->store = new ezcAuthenticationOpenidDbStore( ezcDbInstance::get() );
  41. *
  42. * // create an OpenID filter based on the options object
  43. * $filter = new ezcAuthenticationOpenidFilter( $options );
  44. * </code>
  45. *
  46. * @property ezcDbHandler $instance
  47. * The database instance to use for database storage.
  48. *
  49. * @package AuthenticationDatabaseTiein
  50. * @version //autogentag//
  51. */
  52. class ezcAuthenticationOpenidDbStore extends ezcAuthenticationOpenidStore
  53. {
  54. /**
  55. * Holds the properties of this class.
  56. *
  57. * @var array(string=>mixed)
  58. */
  59. private $properties = array();
  60. /**
  61. * Creates a new object of this class.
  62. *
  63. * @param ezcDbHandler $instance The database instance used for this store
  64. * @param ezcAuthenticationOpenidDbStoreOptions $options Options for this class
  65. */
  66. public function __construct( ezcDbHandler $instance, ezcAuthenticationOpenidDbStoreOptions $options = null )
  67. {
  68. $this->instance = $instance;
  69. $this->options = ( $options === null ) ? new ezcAuthenticationOpenidDbStoreOptions() : $options;
  70. }
  71. /**
  72. * Sets the property $name to $value.
  73. *
  74. * @throws ezcBasePropertyNotFoundException
  75. * if the property $name does not exist
  76. * @throws ezcBaseValueException
  77. * if $value is not correct for the property $name
  78. * @param string $name The name of the property to set
  79. * @param mixed $value The new value of the property
  80. * @ignore
  81. */
  82. public function __set( $name, $value )
  83. {
  84. switch ( $name )
  85. {
  86. case 'instance':
  87. if ( !( $value instanceof ezcDbHandler ) )
  88. {
  89. throw new ezcBaseValueException( $name, $value, 'ezcDbHandler' );
  90. }
  91. $this->properties[$name] = $value;
  92. break;
  93. default:
  94. throw new ezcBasePropertyNotFoundException( $name );
  95. }
  96. }
  97. /**
  98. * Returns the value of the property $name.
  99. *
  100. * @throws ezcBasePropertyNotFoundException
  101. * if the property $name does not exist
  102. * @param string $name The name of the property for which to return the value
  103. * @return mixed
  104. * @ignore
  105. */
  106. public function __get( $name )
  107. {
  108. switch ( $name )
  109. {
  110. case 'instance':
  111. return $this->properties[$name];
  112. default:
  113. throw new ezcBasePropertyNotFoundException( $name );
  114. }
  115. }
  116. /**
  117. * Returns true if the property $name is set, otherwise false.
  118. *
  119. * @param string $name The name of the property to test if it is set
  120. * @return bool
  121. * @ignore
  122. */
  123. public function __isset( $name )
  124. {
  125. switch ( $name )
  126. {
  127. case 'instance':
  128. return isset( $this->properties[$name] );
  129. default:
  130. return false;
  131. }
  132. }
  133. /**
  134. * Stores the nonce in the store.
  135. *
  136. * Returns true if the nonce was stored successfully, and false otherwise.
  137. *
  138. * @throws ezcBaseFilePermissionException
  139. * if the nonce cannot be written in the store
  140. * @param string $nonce The nonce value to store
  141. * @return bool
  142. */
  143. public function storeNonce( $nonce )
  144. {
  145. $table = $this->options->tableNonces;
  146. $query = new ezcQueryInsert( $this->instance );
  147. $query->insertInto( $this->instance->quoteIdentifier( $table['name'] ) )
  148. ->set( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
  149. ->set( $this->instance->quoteIdentifier( $table['fields']['timestamp'] ), $query->bindValue( time() ) );
  150. $stmt = $query->prepare();
  151. $stmt->execute();
  152. return true;
  153. }
  154. /**
  155. * Checks if the nonce exists and afterwards deletes it.
  156. *
  157. * Returns the timestamp of the nonce if it exists, and false otherwise.
  158. *
  159. * @param string $nonce The nonce value to check and delete
  160. * @return bool|int
  161. */
  162. public function useNonce( $nonce )
  163. {
  164. $table = $this->options->tableNonces;
  165. $query = new ezcQuerySelect( $this->instance );
  166. $e = $query->expr;
  167. $query->select( '*' )
  168. ->from( $this->instance->quoteIdentifier( $table['name'] ) )
  169. ->where(
  170. $e->eq( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
  171. );
  172. $query = $query->prepare();
  173. $query->execute();
  174. $rows = $query->fetchAll();
  175. if ( count( $rows ) > 0 )
  176. {
  177. $rows = $rows[0];
  178. $lastModified = (int) $rows[$table['fields']['timestamp']];
  179. $this->removeNonce( $nonce );
  180. return $lastModified;
  181. }
  182. // $nonce was not found in the database
  183. return false;
  184. }
  185. /**
  186. * Removes the nonce from the nonces table.
  187. *
  188. * @param string $nonce
  189. */
  190. protected function removeNonce( $nonce )
  191. {
  192. $table = $this->options->tableNonces;
  193. $query = new ezcQueryDelete( $this->instance );
  194. $e = $query->expr;
  195. $query->deleteFrom( $this->instance->quoteIdentifier( $table['name'] ) )
  196. ->where(
  197. $e->eq( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
  198. );
  199. $query = $query->prepare();
  200. $query->execute();
  201. }
  202. /**
  203. * Stores an association in the store linked to the OpenID provider URL.
  204. *
  205. * Returns true always.
  206. *
  207. * @param string $url The URL of the OpenID provider
  208. * @param ezcAuthenticationOpenidAssociation $association The association value to store
  209. * @return bool
  210. */
  211. public function storeAssociation( $url, $association )
  212. {
  213. $table = $this->options->tableAssociations;
  214. $data = serialize( $association );
  215. $query = new ezcQueryInsert( $this->instance );
  216. $query->insertInto( $this->instance->quoteIdentifier( $table['name'] ) )
  217. ->set( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
  218. ->set( $this->instance->quoteIdentifier( $table['fields']['association'] ), $query->bindValue( $data ) );
  219. $stmt = $query->prepare();
  220. $stmt->execute();
  221. return true;
  222. }
  223. /**
  224. * Returns the unserialized association linked to the OpenID provider URL.
  225. *
  226. * Returns false if the association could not be retrieved or if it expired.
  227. *
  228. * @param string $url The URL of the OpenID provider
  229. * @return ezcAuthenticationOpenidAssociation
  230. */
  231. public function getAssociation( $url )
  232. {
  233. $table = $this->options->tableAssociations;
  234. $query = new ezcQuerySelect( $this->instance );
  235. $e = $query->expr;
  236. $query->select( '*' )
  237. ->from( $this->instance->quoteIdentifier( $table['name'] ) )
  238. ->where(
  239. $e->eq( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
  240. );
  241. $query = $query->prepare();
  242. $query->execute();
  243. $rows = $query->fetchAll();
  244. if ( count( $rows ) > 0 )
  245. {
  246. $rows = $rows[0];
  247. $data = unserialize( $rows[$table['fields']['association']] );
  248. return $data;
  249. }
  250. // no association was found for $url
  251. return false;
  252. }
  253. /**
  254. * Removes the association linked to the OpenID provider URL.
  255. *
  256. * Returns true always.
  257. *
  258. * @param string $url The URL of the OpenID provider
  259. * @return bool
  260. */
  261. public function removeAssociation( $url )
  262. {
  263. $table = $this->options->tableAssociations;
  264. $query = new ezcQueryDelete( $this->instance );
  265. $e = $query->expr;
  266. $query->deleteFrom( $this->instance->quoteIdentifier( $table['name'] ) )
  267. ->where(
  268. $e->eq( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
  269. );
  270. $query = $query->prepare();
  271. $query->execute();
  272. return true;
  273. }
  274. }
  275. ?>