root/cherokee/trunk/doc/cookbook_authentication.txt

Revision 2466, 6.1 kB (checked in by taher, 1 week ago)

--

Line 
1 == link:index.html[Index] -> link:cookbook.html[Cookbook]
2
3 Cookbook: Authentication
4 ------------------------
5
6 In this section you will find information useful to set up
7 authentication mechanisms with several of Cherokee's validators.
8
9 You can find information and basic examples in each validator's
10 documentation. This is the list of validator modules provided by
11 Cherokee:
12
13 * link:modules_validators_htdigest.html[htdigest]
14 * link:modules_validators_htpasswd.html[htpasswd]
15 * link:modules_validators_ldap.html[LDAP]
16 * link:modules_validators_mysql.html[MySQL]
17 * link:modules_validators_pam.html[PAM]
18 * link:modules_validators_plain.html[Plain]
19 * link:modules_validators_authlist.html[Fixed List]
20
21 You will also find interesting information in the
22 link:modules_validators_plain.html["Validator Modules Overview"] and
23 step by step examples for Plain and PAM mechanisms in the
24 link:config_quickstart.html["Quickstart"] section.
25
26 There are two types of authentication:
27
28 *basic*::
29     This method sends the username and password in clear text over the network.
30     It is not the most secure method.  If the connection to the web server is
31     through HTTPS then this method is as secure as the encryption used.  This
32     method is very easy to implement, so most clients support it.
33
34 *digest*::
35     This method is by far the most secure, but also more complex.  Most modern
36     web browsers support this method.
37
38
39 The details to set up the `htdigest` and `htpasswd` are exactly the
40 same as for `plain` validation. The only difference is the tools used
41 to create the passwords' file.
42
43 [[htdigest]]
44 === htdigest
45
46 To use this handler you will need a file created by the `htdigest`
47 command. It is a tool to manage user files for digest (and basic)
48 authentication.
49
50 ****
51 Syntax::
52   htdigest [ -c ] passwdfile realm username
53
54 The only optional parameter is `-c`, used to create the passwdfile or
55 overwrite it if it is present.
56 ****
57
58 To create a file for a `testuser` with `testpassword` you would have
59 to issue:
60
61 -----
62 $ htdigest -c passwords.digest secret testuser
63 Adding password for testuser in realm secret.
64 New password:
65 Re-type new password:
66
67 $ cat pass
68 testuser:secret:f24f76261bcd65780b33edde00855897
69 -----
70
71 [[htpasswd]]
72 === htpasswd
73
74 For this handle, the tool `htpasswd` is needed to create the
75 files. The basic usage information is this:
76
77 *****
78 Usage::
79         htpasswd [-cmdpsD] passwordfile username
80 +
81         htpasswd -b[cmdpsD] passwordfile username password
82 +
83         htpasswd -n[mdps] username
84 +
85         htpasswd -nb[mdps] username password
86 *****
87
88 Refer to its documentation for details about the parameters. For our example, this
89 will suffice:
90
91 ----
92 $ htpasswd -c /var/www/.htpasswd testuser
93 New password:
94 Re-type new password:
95 Adding password for user testuser
96
97 $ cat /var/www/.htpasswd
98 testuser:iqLGh2g/7bX7M
99 ----
100
101 Remember that it is never recommended to place the file with the
102 passwords in a location fetchable from the webserver. This is true for
103 plain validation, htdigest, htpasswd and whatever file based system
104 you cross paths with.
105
106
107 [[mysql]]
108 === MySQL
109
110 Lets set up a simple server requiring authentication against a MySQL
111 database to fetch any content.
112
113 First, lets define a unique rule in our virtual server managed by the
114 `List and Send` handler. Through the `Security` tab we can configure
115 it to use MySQL as authentication mechanism. Filling up just the
116 essential fields will be enough. Realm, database name, user, password
117 and an SQL query that must return one row with one column as password.
118
119 image::media/images/cookbook_mysql_validator.png[MySQL handler set up]
120
121 In this case, we have used:
122
123 ----
124 SELECT password FROM auth_users WHERE username = '${user}'
125 ----
126
127 And that is about it.
128 In this example you will need a MySQL server running (localhost in
129 this case, as it takes the default value), a database called
130 `cherokee` with `cherokee` as user and password, and a table that
131 suits the shown query.
132
133
134 Assuming you have a MySQL user with privileges granted to create
135 databases, a MySQL session simliar to this one would suffice:
136
137 ----
138 $ mysql -u cherokee -D cherokee -p
139
140 mysql> CREATE DATABASE cherokee;
141 Query OK, 1 row affected (0.00 sec)
142
143 mysql> CREATE TABLE auth_users(
144        username varchar(32),
145        password varchar(32),
146        PRIMARY KEY (username));
147 Query OK, 0 rows affected (0.00 sec)
148
149 mysql> INSERT INTO auth_users VALUES('cherokee','cherokee');
150 Query OK, 1 row affected (0.00 sec)
151
152 mysql> quit
153 ----
154
155 When we are done, our simple virtual server should look like this:
156
157 image::media/images/cookbook_mysql_rule.png[MySQL Authenticated rule]
158
159 And any content requested to Cherokee will require prior
160 authentication against the database.
161
162
163 [[example]]
164 Another usage example
165 ~~~~~~~~~~~~~~~~~~~~~
166
167 As you can see, getting the hang of how authentication works is pretty
168 easy. Let's ilustrate another easy example. How to serve PHP files,
169 both from a protected location and an unprotected one?
170
171 Let's assume our locations are targets are:
172 - /unprotected/*.php
173 - /protected/*.php
174
175 Well... this would be really easy. You just have to remember that
176 rules are evaluated from top to bottom, and the evaluation proceeds
177 until a final rule is matched.
178
179 This means that we would only have to be careful with the order of our
180 rules, and it would be as simple as setting a couple of rules:
181
182 - The first one, of type `Directory` applied to the path
183   `/protected`. This would be the top rule, should use one of the
184   authentication mechanisms mentioned above and should not be set as
185   _FINAL_.
186
187 - The second one, of type `Extension` would apply to `php` files and
188   should be configured as according to the link:cookbook_php.html[PHP
189   recipe]. This one should be a _FINAL_ rule.
190
191 And this would be more than enough. The files from the secure location
192 would match the first rule and the authentication would be
193 required. In case it was successful, not being a final rule, the
194 request would proceed to the second rule. Once there, the regular
195 processing of PHP files would take palce. This one is a _FINAL_ rule,
196 so the rule evaluation would stop.
197
198 In case the PHP files were not being requested from the secured
199 directory, just the second rule would apply.
200
201 ////
202 To be written
203 More details for ldap, with screenshots.
204 ////
205
206
Note: See TracBrowser for help on using the browser.