## Apache Zeta Components Manual :: File Source for token_filter.php

## Source for file token_filter.php

Documentation is available at token_filter.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationTokenFilter 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 Authentication
25. * @version //autogen//
26. */
28. /**
29. * Filter to authenticate against a server generated token.
30. *
31. * Some uses for this filter:
32. * - CAPTCHA tests
33. * - security token devices (as used by banks)
34. *
35. * The following example shows how to create a CAPTCHA test. The example is
36. * divided into 2 parts: the initial request (where the user sees the CAPTCHA
37. * image and enters the characters he sees in a form) and the follow-up
38. * request (after the user submits the form).
39. * - on the initial request:
40. * <code>
41. * // generate a token and save it in the session or in a file/database
42. * $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
43. * $token = "";
44. * for( $i = 1; $i <= 6 ; $i++ )
45. * {
46. * $token .= $pattern{rand( 0, 36 )};
47. * }
48. * $encryptedToken = sha1( $token );
49. *
50. * // save the $encryptedToken in the session
51. * session_start();
52. * $_SESSION['encryptedToken'] = $encryptedToken;
53. *
54. * // also generate a distorted image which contains the symbols from $token and use it
55. * </code>
56. *
57. * - on the follow-up request:
58. * <code>
59. * // load the $encryptedToken as it was generated on a previous request
60. * session_start();
61. * $encryptedToken = isset( $_SESSION['encryptedToken'] ) ? $_SESSION['encryptedToken'] : null;
62. *
63. * // also load the value entered by the user in response to the CAPTCHA image
64. * $captcha = isset( $_POST['captcha'] ) ? $_POST['captcha'] : null;
65. *
66. * $credentials = new ezcAuthenticationIdCredentials( $captcha );
67. * $authentication = new ezcAuthentication( $credentials );
68. * $authentication->addFilter( new ezcAuthenticationTokenFilter( $encryptedToken, 'sha1' ) );
69. * if ( !$authentication->run() )
70. * {
71. * // CAPTCHA was incorrect, so inform the user to try again, eventually
72. * // by generating another token and CAPTCHA image
73. * }
74. * else
75. * {
76. * // CAPTCHA was correct, so let the user send his spam or whatever
77. * }
78. * </code>
79. *
80. * @property string $token
81. * The token to check against.
82. * @property callback $function
83. * The encryption function to use on the user credentials in order to
84. * compare it with the stored token.
85. *
86. * @package Authentication
87. * @version //autogen//
88. * @mainclass
89. */
90. class ezcAuthenticationTokenFilter extends ezcAuthenticationFilter
91. {
92. /**
93. * Token is not the same as the provided one.
94. */
95. const STATUS_TOKEN_INCORRECT = 1;
97. /**
98. * Holds the properties of this class.
99. *
100. * @var array(string=>mixed)
101. */
102. private $properties = array();
104. /**
105. * Creates a new object of this class.
106. *
107. * @param string $token A string value generated by the server
108. * @param callback $function The encryption function to use when comparing tokens
109. * @param ezcAuthenticationTokenOptions $options Options for this class
110. */
111. public function __construct ( $token, $function, ezcAuthenticationTokenOptions $options = null )
112. {
113. $this->token = $token;
114. $this->function = $function;
115. $this-> options = ( $options === null ) ? new ezcAuthenticationTokenOptions () : $options;
116. }
118. /**
119. * Sets the property $name to $value.
120. *
121. * @throws ezcBasePropertyNotFoundException
122. * if the property $name does not exist
123. * @throws ezcBaseValueException
124. * if $value is not correct for the property $name
125. * @param string $name The name of the property to set
126. * @param mixed $value The new value of the property
127. * @ignore
128. */
129. public function __set( $name, $value )
130. {
131. switch ( $name )
132. {
133. case 'token':
134. if ( is_string ( $value ) || is_numeric ( $value ) )
135. {
136. $this->properties[$name] = $value;
137. }
138. else
139. {
140. throw new ezcBaseValueException ( $name, $value, 'string || int' );
141. }
142. break;
144. case 'function':
145. if ( is_callable ( $value ) )
146. {
147. $this->properties[$name] = $value;
148. }
149. else
150. {
151. throw new ezcBaseValueException ( $name, $value, 'callback' );
152. }
153. break;
155. default:
156. throw new ezcBasePropertyNotFoundException ( $name );
157. }
158. }
160. /**
161. * Returns the value of the property $name.
162. *
163. * @throws ezcBasePropertyNotFoundException
164. * if the property $name does not exist
165. * @param string $name The name of the property for which to return the value
166. * @return mixed
167. * @ignore
168. */
169. public function __get( $name )
170. {
171. switch ( $name )
172. {
173. case 'token':
174. case 'function':
175. return $this->properties[$name];
177. default:
178. throw new ezcBasePropertyNotFoundException ( $name );
179. }
180. }
182. /**
183. * Returns true if the property $name is set, otherwise false.
184. *
185. * @param string $name The name of the property to test if it is set
186. * @return bool
187. * @ignore
188. */
189. public function __isset( $name )
190. {
191. switch ( $name )
192. {
193. case 'token':
194. case 'function':
195. return isset( $this->properties[$name] );
197. default:
198. return false;
199. }
200. }
202. /**
203. * Runs the filter and returns a status code when finished.
204. *
205. * @param ezcAuthenticationCredentials $credentials Authentication credentials
206. * @return int
207. */
208. public function run ( $credentials )
209. {
210. $password = call_user_func ( $this->function, $credentials->id );
211. if ( $this->token === $password )
212. {
213. return self::STATUS_OK;
214. }
215. return self::STATUS_TOKEN_INCORRECT;
216. }
217. }
218. ?>

Documentation generated by phpDocumentor 1.4.3