1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.auth.adapter.http">
4 <title>HTTP Authentication Adapter</title>
6 <sect2 id="zend.auth.adapter.http.introduction">
7 <title>Introduction</title>
10 <classname>Zend_Auth_Adapter_Http</classname> provides a mostly-compliant
11 implementation of <ulink url="http://tools.ietf.org/html/rfc2617">RFC-2617</ulink>,
12 <ulink url="http://en.wikipedia.org/wiki/Basic_authentication_scheme">Basic</ulink>
14 url="http://en.wikipedia.org/wiki/Digest_access_authentication">Digest</ulink>
15 <acronym>HTTP</acronym> Authentication. Digest authentication is a method of
16 <acronym>HTTP</acronym> authentication that improves upon Basic authentication by
17 providing a way to authenticate without having to transmit the password in clear text
22 <emphasis>Major Features:</emphasis>
28 Supports both Basic and Digest authentication.
34 Issues challenges in all supported schemes, so client can respond with any
41 Supports proxy authentication.
47 Includes support for authenticating against text files and provides an
48 interface for authenticating against other sources, such as databases.
54 There are a few notable features of <acronym>RFC-2617</acronym> that are not
61 Nonce tracking, which would allow for "stale" support, and increased replay
68 Authentication with integrity checking, or "auth-int".
74 Authentication-Info <acronym>HTTP</acronym> header.
80 <sect2 id="zend.auth.adapter.design_overview">
81 <title>Design Overview</title>
84 This adapter consists of two sub-components, the <acronym>HTTP</acronym> authentication
85 class itself, and the so-called "Resolvers." The <acronym>HTTP</acronym> authentication
86 class encapsulates the logic for carrying out both Basic and Digest authentication. It
87 uses a Resolver to look up a client's identity in some data store (text file by
88 default), and retrieve the credentials from the data store. The "resolved" credentials
89 are then compared to the values submitted by the client to determine whether
90 authentication is successful.
94 <sect2 id="zend.auth.adapter.configuration_options">
95 <title>Configuration Options</title>
98 The <classname>Zend_Auth_Adapter_Http</classname> class requires a configuration array
99 passed to its constructor. There are several configuration options available, and some
103 <table id="zend.auth.adapter.configuration_options.table">
104 <title>Configuration Options</title>
109 <entry>Option Name</entry>
110 <entry>Required</entry>
111 <entry>Description</entry>
117 <entry><emphasis><property>accept_schemes</property></emphasis></entry>
121 Determines which authentication schemes the adapter will accept from
122 the client. Must be a space-separated list containing
123 <emphasis>'basic'</emphasis> and/or <emphasis>'digest'</emphasis>.
128 <entry><emphasis><property>realm</property></emphasis></entry>
132 Sets the authentication realm; usernames should be unique within a
138 <entry><emphasis><property>digest_domains</property></emphasis></entry>
141 Yes, when <property>accept_schemes</property> contains
142 <emphasis>digest</emphasis>
146 Space-separated list of <acronym>URI</acronym>s for which the same
147 authentication information is valid. The <acronym>URI</acronym>s need
148 not all point to the same server.
153 <entry><emphasis><property>nonce_timeout</property></emphasis></entry>
156 Yes, when <property>accept_schemes</property> contains
157 <emphasis>digest</emphasis>
161 Sets the number of seconds for which the nonce is valid. See notes
167 <entry><emphasis><property>proxy_auth</property></emphasis></entry>
171 Disabled by default. Enable to perform Proxy authentication, instead
172 of normal origin server authentication.
181 The current implementation of the <property>nonce_timeout</property> has some
182 interesting side effects. This setting is supposed to determine the valid lifetime
183 of a given nonce, or effectively how long a client's authentication information is
184 accepted. Currently, if it's set to 3600 (for example), it will cause the adapter
185 to prompt the client for new credentials every hour, on the hour. This will be
186 resolved in a future release, once nonce tracking and stale support are
192 <sect2 id="zend.auth.adapter.http.resolvers">
193 <title>Resolvers</title>
196 The resolver's job is to take a username and realm, and return some kind of credential
197 value. Basic authentication expects to receive the Base64 encoded version of the user's
198 password. Digest authentication expects to receive a hash of the user's username, the
199 realm, and their password (each separated by colons). Currently, the only supported
200 hash algorithm is <acronym>MD5</acronym>.
204 <classname>Zend_Auth_Adapter_Http</classname> relies on objects implementing
205 <classname>Zend_Auth_Adapter_Http_Resolver_Interface</classname>. A text file resolver
206 class is included with this adapter, but any other kind of resolver can be created
207 simply by implementing the resolver interface.
210 <sect3 id="zend.auth.adapter.http.resolvers.file">
211 <title>File Resolver</title>
214 The file resolver is a very simple class. It has a single property specifying a
215 filename, which can also be passed to the constructor. Its
216 <methodname>resolve()</methodname> method walks through the text file, searching
217 for a line with a matching username and realm. The text file format similar to
218 Apache htpasswd files:
221 <programlisting language="txt"><![CDATA[
222 <username>:<realm>:<credentials>\n
226 Each line consists of three fields - username, realm, and credentials - each
227 separated by a colon. The credentials field is opaque to the file resolver; it
228 simply returns that value as-is to the caller. Therefore, this same file format
229 serves both Basic and Digest authentication. In Basic authentication, the
230 credentials field should be written in clear text. In Digest authentication, it
231 should be the <acronym>MD5</acronym> hash described above.
235 There are two equally easy ways to create a File resolver:
238 <programlisting language="php"><![CDATA[
239 $path = 'files/passwd.txt';
240 $resolver = new Zend_Auth_Adapter_Http_Resolver_File($path);
247 <programlisting language="php"><![CDATA[
248 $path = 'files/passwd.txt';
249 $resolver = new Zend_Auth_Adapter_Http_Resolver_File();
250 $resolver->setFile($path);
254 If the given path is empty or not readable, an exception is thrown.
259 <sect2 id="zend.auth.adapter.http.basic_usage">
260 <title>Basic Usage</title>
263 First, set up an array with the required configuration values:
266 <programlisting language="php"><![CDATA[
268 'accept_schemes' => 'basic digest',
269 'realm' => 'My Web Site',
270 'digest_domains' => '/members_only /my_account',
271 'nonce_timeout' => 3600,
276 This array will cause the adapter to accept either Basic or Digest authentication, and
277 will require authenticated access to all the areas of the site under
278 <filename>/members_only</filename> and <filename>/my_account</filename>. The realm
279 value is usually displayed by the browser in the password dialog box. The
280 <property>nonce_timeout</property>, of course, behaves as described above.
284 Next, create the <classname>Zend_Auth_Adapter_Http</classname> object:
287 <programlisting language="php"><![CDATA[
288 $adapter = new Zend_Auth_Adapter_Http($config);
292 Since we're supporting both Basic and Digest authentication, we need two different
293 resolver objects. Note that this could just as easily be two different classes:
296 <programlisting language="php"><![CDATA[
297 $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
298 $basicResolver->setFile('files/basicPasswd.txt');
300 $digestResolver = new Zend_Auth_Adapter_Http_Resolver_File();
301 $digestResolver->setFile('files/digestPasswd.txt');
303 $adapter->setBasicResolver($basicResolver);
304 $adapter->setDigestResolver($digestResolver);
308 Finally, we perform the authentication. The adapter needs a reference to both the
309 Request and Response objects in order to do its job:
312 <programlisting language="php"><![CDATA[
313 assert($request instanceof Zend_Controller_Request_Http);
314 assert($response instanceof Zend_Controller_Response_Http);
316 $adapter->setRequest($request);
317 $adapter->setResponse($response);
319 $result = $adapter->authenticate();
320 if (!$result->isValid()) {
321 // Bad userame/password, or canceled password prompt