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