chiark / gitweb /
docs: more work
[cgi-auth-flexible.git] / caf.pod
1 # -*- fundamental -*-
2
3 =head1 NAME
4
5 CGI::Auth::Flexible - web authentication optionally using cookies
6
7 =head1 SYNOPSYS
8
9  my $verifier = CGI::Auth::Flexible->new_verifier(setting => value,...);
10  my $authreq = $verifier->new_request($cgi_query_object);
11
12  # simple applications
13  $authreq->check_ok() or return;
14
15  # sophisticated applications
16  my $divert_kind = $authreq->check_divert();
17  if ($divert_kind) { ... print diversion page and quit ... }
18
19  # while handling the request
20  $user = $authreq->get_username();
21  $authreq->check_mutate();
22
23 =head1 DESCRIPTION
24
25 CGI::Auth::Flexible is a library which you can use to add a
26 forms/cookie-based login facility to a Perl web application.
27
28 CGI::Auth::Flexible doesn't interfere with your application's URL path
29 namespace and just needs a few (configurable) form parameter and
30 cookie name(s) for its own use.  It tries to avoid making assumptions
31 about the implementation structure of your application.
32
33 Because CGI::Auth::Flexible is licenced under the AGPLv3, you will
34 probably need to provide a facility to allow users (even ones not
35 logged in) to download the source code for your web app.  Conveniently
36 by default CGI::Auth::Flexible provides (for pure Perl webapps) a
37 mechanism for users to get the source.
38
39 CGI::Auth::Flexible is designed to try to stop you accidentally
40 granting access by misunderstanding the API.  (Also it, of course,
41 guards against cross-site scripting.)  You do need to make sure to
42 call CGI::Auth::Flexible before answering AJAX requests as well as
43 before generating HTML pages, of course, and to call it in every
44 entrypoint to your system.
45
46 =head2 CHECKLIST
47
48 As a minimum you need to do all of the things on this checklist, where
49 applicable.  The items marked SECURITY are the ones that you might
50 forget: without them your application may appear to work, but will be
51 insecure.
52
53 =over
54
55 =item *
56
57 Call C<new_verifier> (once at application startup)
58
59 =item *
60
61 Call C<new_request> (once per request)
62
63 =item *
64
65 B<SECURITY>: Call C<check_ok> or C<check_divert> on every request, and
66 honour the return value.
67
68 =item *
69
70 If you're using C<check_ok>, implement either the
71 C<username_password_error> or C<login_ok> hook.
72
73 =item *
74
75 Call C<get_username> when you need to know who's logged in.
76
77 =item *
78
79 B<SECURITY>: Call C<check_mutate> or C<mutate_ok>, if you specified
80 C<promise_check_mutate>.
81
82 =item *
83
84 B<SECURITY>: Call C<check_nonpage> for every request which is not a page load
85 (if your application has any of those).
86
87 =item *
88
89 When generating URLs and forms (including AJAX requests), include the
90 hidden form parameter using C<secret_hidden_val> or
91 C<secret_hidden_html> when appropriate (see below).
92
93 =item *
94
95 B<SECURITY>: If you do not override the source provision facility (see
96 L</SOURCE CODE DOWNLOAD>), check that the assumptions it makes aren't
97 going to leak security-critical data.
98
99 =back
100
101 These points will now be covered in more detail.
102
103 =head2 INITIALISATION
104
105 Your application should, on startup (eg, when it is loaded by
106 mod_perl) do
107 C<< $verifier = CGI::Auth::Flexible->new_verifier(settings...) >>.
108 This call can be expensive and is best amortised.
109
110 The resulting verifier object can be used to process individual
111 requests, in each case with
112 C<< $authreq = CGI::Auth::Flexible->new_request($cgi_query) >>.
113
114 =head2 CHECKING AND RESPONSE GENERATION
115
116 If the user is logged in, your application is to handle the request.
117 Otherwise, the user needs to be presented with a login form or error
118 message, as appropriate.  CGI::Auth::Flexible provides two alternative
119 interfaces for this:
120
121 =head3 Simple applications
122
123 The simplist usage is to call C<< $request->check_ok() >> which will
124 check the user's authentication.  If the user is not logged in it will
125 generate a login form (or redirection or other appropriate page) and
126 return false; your application should not then processing that request
127 any further.  If the user is logged in it will return true.
128
129 Various hooks are provided to customise the responses generated by
130 C<check_ok>.
131
132 After C<check_ok> returns true you should go ahead and process the
133 request; you can use C<< $request->get_username >> to find out which
134 user the request came from.
135
136 =head2 Sophisticated applications
137
138 If you want to handle the control flow and to generate login forms,
139 redirections, etc., yourself, you can say
140 C<< $divert = $request->check_divert >>.  This returns undef if
141 the user is logged in, or I<divert spec> if some kind of login
142 page or diversion should be generated.  See L</DIVERT SPEC> below for
143 details of how to deal with the return value.
144
145 =head2 MUTATING OPERATIONS AND EXTERNAL LINKS
146
147 =head3 Mutation-ignorant applications
148
149 By default CGI::Auth::Flexible does not permit external links into
150 your site.  All GET requests give a "click to continue" page which
151 submits a form which loads your app's main page.  In this
152 configuration all your application's forms and AJAX requests should
153 use C<POST>.  This restriction arises from complicated deficiencies
154 in the web's security architecture.
155
156 Such applications are also not able to provide user-specific CSS
157 stylesheets, javascript, favicons, etc.
158
159 =head3 Mutation-aware applications
160
161 The alternative is for your application to always make a special check
162 when the incoming request is going to do some kind of action (such as
163 modifying the user's setup, purchasing goods, or whatever) rather than
164 just retrieve and/or display information.  We term such requests
165 "mutating" requests.
166
167 Then non-mutating pages can be linked to from other, untrustworthy,
168 websites.
169
170 To support external links, and C<GET> requests, pass
171 C<< promise_check_mutate => 1 >> in I<settings>, and then call
172 C<< $authreq->check_mutate() >> before taking any actions.  If the
173 incoming request is not suitable then C<< $authreq->check_mutate() >>
174 will call C<die>.
175
176 There have to be no mutating C<GET> requests in your application (but
177 you shouldn't have any of those anyway); if there are, they won't
178 work.  (CGI::Auth::Flexible will spot them and cause them to fail,
179 rather than allow them to be insecure.)
180
181 =head2 GENERATING URLS, FORMS AND AJAX QUERIES
182
183 When you generate a URL, C<POST> form or AJAX request you may need to
184 include a secret hidden form parameter for the benefit of
185 CGI::Auth::Generic.  This form parameter will be checked by
186 C<check_ok>/C<check_divert> and should be ignored by your application.
187
188 By default the hidden parameter is called C<caf_assochash>.
189
190 After calling C<check_ok> or C<check_divert> the value to put in your
191 form can be obtained from C<secret_hidden_val>; C<secret_hidden_html>
192 will generate the whole HTML C<< <input...> >> element.
193
194 =head3 Mutation-ignorant applications
195
196 For mutation-ignorant applications (see above), all forms etc. should
197 include the hidden parameter (and as discussed, they must all use
198 POST rather than GET).
199
200 =head3 Mutation-aware applications
201
202 For mutation-aware applications, whether to include the secret
203 parameter depends on the kind of request.  CGI::Auth::Flexible knows
204 when it is necessary.  You should find out by calling
205 C<need_add_hidden>.
206
207 If it is inconvenient to call C<need_add_hidden> at runtime, you can
208 rely instead on the following promises:  All POST requests (which
209 includes all mutating requests) need the parameter.  The return value
210 of need_add_hidden depends only on the $method and $reqtype
211 parameters, so you can query it once and remember the answer.
212 HTML page load GETs do not need the parameter.  It is better to
213 err on the side of including the parameter.
214
215 If you really must, you can call C<need_add_hidden> "on the bench"
216 during development and bake the answer into your application code
217 structure.  However, if you do that and a new vulnerability was
218 discovered which is fixed by changing the answer, updating
219 CGI::Auth::Flexible wouldn't be sufficient to fix it.
220
221 =head3 Mutation-aware applications - non-page requests
222
223 If your mutation-aware application supports non-page resources (AJAX
224 and JSON requests, stylesheets, favicons, etc.) it must inform
225 CGI::Auth::Flexible when it is handling such a request, by calling
226 C<check_nonpage>.
227
228 Normally C<check_nonpage> will simply return (and you can ignore the
229 return value).  However, if there is an attack (or, perhaps, a bug) it
230 will die, stopping the attack.
231
232 (You do not need to call C<check_nonpage> for POST requests, but it is
233 harmless to do so.)
234
235 =head3 Mutation-aware applications - novel kinds of request
236
237 If you want to support a kind of request perhaps not yet known about
238 by CGI::Auth::Flexible, you can provide information about that new
239 request kind using C<update_get_need_add_hidden>.
240
241 =head2 DATA STORAGE
242
243 CGI::Auth::Flexible needs to store various information in plain files;
244 it does this in the directory specified by the C<dir> parameter.
245
246 =head1 SOURCE CODE DOWNLOAD
247
248 By default, CGI::Auth::Flexible provides a facility for users to
249 download the source code for the running version of your web
250 application.
251
252 This facility makes a number of important assumptions which you need
253 to check.  Note that if the provided facility is not sufficient
254 because your application is more sophisticated than it copes with (or
255 if you disable the builtin facility), you may need to implement a
256 functioning alternative to avoid violating the AGPLv3 licence.
257
258 Here are the most important (default) assumptions:
259
260 =over
261
262 =item *
263
264 Your app's source code is available by looking at @INC, $0 and
265 S<$ENV{'SCRIPT_FILENAME'}> (the B<source items>).  See
266 C<srcdump_listitems>.  Where these point to files or directories under
267 revision control, the source item is the whole containing vcs tree.
268
269 =item *
270
271 Specifically, there are no compiled or autogenerated Perl
272 files, Javascript resources, etc., which are not contained in one of
273 the source item directories.  (Files which came with your operating
274 system install don't need to be shipped as they fall under the system
275 library exception.)
276
277 =item *
278
279 You have not installed any modified versions of system
280 libraries (including system-supplied Perl modules) in C</usr> outside
281 C</usr/local>.  See C<srcdump_system_dir>.
282
283 =item *
284
285 For each source item in a dvcs, the entire dvcs history does
286 not contain anything confidential (or libellous).  Also, all files which
287 contain secrets are in the dvcs's I<.ignore> file.  See
288 C<srcdump_vcsscript_git> et al.
289
290 =item *
291
292 For each source item NOT in a dvcs, there are no confidential
293 files with the world-readable bit set (being in a world-inaccessible
294 directory is not sufficient).  See C<srcdump_excludes>.
295
296 =item *
297
298 You have none of your app's source code in C</etc>.
299
300 =item *
301
302 You don't regard pathnames on your server as secret.
303
304 =item *
305
306 You don't intentionally load Perl code by virtue of C<.>
307 being in C<@INC> by default.  (See C<srcdump_filter_cwd>.)
308
309 =back
310
311 =head1 MAIN FUNCTIONS AND METHODS
312
313 =over
314
315 =item C<< CGI::Auth::Flexible->new_verifier(setting => value, ...) >>
316
317 Initialises an instance and returns a verifier object.
318 The arguments are setting pairs like a hash initialiser.
319 See L</SETTINGS> below.
320
321 =item C<< $verifier->new_request($cgi_query) >>
322
323 Prepares to process a request.  I<$cgi_query> should normally
324 be the query object from L<CGI(3perl)>.  Most of the default
325 hook methods assume that it is; however if you replace enough of
326 the hook methods then you can pass any value you like and it
327 will be passed to your hooks.
328
329 The return value is the authentication request object (I<$authreq>)
330 which is used to check the incoming request and will contain
331 information about its credentials.
332
333 =item C<< $authreq->check_divert() >>
334
335 Checks whether the user is logged in.  Returns undef if the user is
336 logged in and we should service the request.  Otherwise returns a
337 divert spec (see L</DIVERT SPEC>) saying what should happen instead.
338
339 This method may die if it doesn't like the request, in which case
340 the request needs to be rejected.
341
342 =item C<< $authreq->check_ok() >>
343
344 Checks whether the user is logged in.  Returns true if the user is
345 logged in and we should service the request.
346
347 Otherwise it handles the request itself, generating any appropriate
348 redirect, login form, or continuation page.  It then returns false and
349 the application should not process the request further.
350
351 =item C<< $verifier->disconnect() >>
352
353 Discards the resources (open files, etc.) in the verifier object.
354
355 =back
356
357 =head REQUEST-RELATED FUNCTIONS AND METHODS
358
359 All of these are only valid after C<check_divert> or C<check_ok> has
360 been called.  (In the case of C<check_ok> it won't normally be sensible
361 to call these functions unless C<check_ok> returned true.)
362
363 =item C<< $authreq->get_divert() >>
364
365 Returns the value previously returned by C<check_divert>.
366
367 =item C<< $authreq->get_username() >>
368
369 Returns the name of the logged-in user.  If the user was not logged
370 in (or their session had timed out, or something), returns undef.
371
372 =item C<< $authreq->check_mutate() >>
373
374 Declares to CGI::Auth::Generic that the request being handled will
375 "mutate".  That is, it will modify some server-side state (eg, adding
376 items to shopping baskets, posting messages to blogs, sending emails,
377 or whatever).
378
379 If you have set the setting C<promise_check_mutate> you must call
380 C<check_mutate> whenever appropriate.  If you haven't then it's
381 irrelevant.  See L<MUTATING OPERATIONS AND EXTERNAL LINKS>.
382
383 C<check_mutate> will either return successfully, indicating that all
384 is well and the request should proceed, or it will die.  If it dies
385 that means that the request was improper, which can only result from a
386 bug or an attack.  So an "internal server error" is a suitable
387 response.
388
389 =item C<< $authreq->check_nonpage($method, $reqtype) >>
390
391 Declares to CGI::Auth::Generic that the request is not a page request,
392 but rather a request of type I<$reqtype>.
393
394 If your application has set the setting C<promise_check_mutate>,
395 whenever it is handling anything except an HTML page loads, it must
396 call this function.  See L</REQUEST TYPES>, and
397 L<GENERATING URLS, FORMS AND AJAX QUERIES>.
398
399 C<check_mutate> will either return successfully, indicating that all
400 is well and the request should proceed, or it will die, like
401 C<check_mutate>.
402
403 =head RESPONSE-RELATED FUNCTIONS AND METHODS
404
405 =item C<< $authreq->url_with_query_params($params, [$nonpagetype]) >>
406
407 Convenience function which returns a url for a GET request to this
408 application.
409
410 I<$params> is a hashref specifying the parameters and the PATH_INFO.
411 The keys are the parameter names, and the values are array refs with
412 the parameter value(s) (as strings, as yet unquoted).  (They are array
413 refs because it is possible to pass multiple values for the same
414 parameter in a single request; normally each arrayref would be a
415 singleton.)
416
417 The request path will be the path to the application.  If a parameter
418 with name C<< '' >> is supplied, it is taken as the PATH_INFO - its
419 value will be appended to the application path.  (It should normally
420 start with C<< / >>, and only one value should be supplied.)
421
422 =item C<< something->need_add_hidden($method, $reqtype) >>
423
424 Enquires whether a request of type I<$reqtype> using HTTP method
425 I<$method> needs the hidden form parameter.  See L</REQUEST TYPES>.
426
427 =item C<< something->secret_hidden_val() >>
428
429 Returns the value of the hidden form parameter.  This should be
430 included in all POST requests to your application (and thus be a
431 hidden form parameter in all forms).
432
433 It should also be in some (maybe all) GET requests.  If your
434 application is mutation-ignorant, it should be in all GET requests.
435 If you are mutation-aware, you need to consult C<need_add_hidden>.
436
437 The name of the hidden parameter is the setting C<assoc_param_name>,
438 C<caf_hassochash> by default.  xxx rename param and setting
439
440 =item C<< something->secret_hidden_html() >>
441
442 Returns the HTML for an C<INPUT> element specifying the hidden form
443 parameter.
444
445 =item C<< something->secret_cookie_val() >>
446
447 Returns the value of the secret cookie.  CGI::Auth::Flexible sets this
448 cookie in the forms generated by C<check_ok>.  You may also set it
449 yourself (and indeed you must do so if you use C<check_divert>).
450
451 =item C<< $authreq->chain_params() >>
452
453 Returns a hash of the "relevant" parameters to this request, in a form
454 used by XXX.  This is all of the query parameters which are not
455 related to CGI::Auth::Flexible.  The PATH_INFO from the request is
456 returned as the parameter C<< '' >>.
457
458 xxx why use this function
459
460 =back
461
462 =head OTHER FUNCTIONS AND METHODS
463
464 =over
465
466 =item C<< $verifier_or_authreq->hash($data) >>
467
468 Hashes the supplied data using the hash function specified by the
469 C<hash_algorithm> setting, and converts the result to a string of hex
470 digits.
471
472 =item C<< something->update_get_need_add_hidden($reqtype, $value, [$force]) >>
473
474 Updates CGI::Auth::Generic's knowledge about the various kinds of
475 request, and whether they need the hidden form parameter.  This
476 function applies only to GET requests - POST requests always use the
477 parameter.
478
479 I<$reqtype> is the request type (the value which will be passed to
480 C<check_nonpage> and C<need_add_hidden>.  If you are supporting a new
481 I<$reqtype> you shouuld coordinate with CGI::Auth::Flexible upstrea,
482 or other users, to assign a unique request type name.
483
484 This method may be called on an authreq or a verifier, in which case
485 it will affect all authreqs using the same verifier.  Or it may be
486 called on the class as a whole, in which case it will affect the
487 global default list for all verifiers.
488
489 If I<$force> is supplied and true, this will override
490 CGI::Auth::Flexible's existing knowledge.  Otherwise this new setting
491 will be ignored if CGI::Auth::Flexible already knows about the request
492 type.  (When called on a verifier or authreq, it will ignore the
493 update in favour of existing knowledge recorded both globally in the
494 class or due to previous requests on the same verifier.)
495
496 See L</REQUEST TYPES>.
497
498 =item C<< $verifier_or_authreq->($data) | CGI::Auth::Flexible->>>
499
500 Hashes the supplied data using the hash function specified by the
501 C<hash_algorithm> setting, and converts the result to a string of hex
502 digits.
503
504 =back
505
506
507
508 xxx divert spec
509 xxx reqtype
510 xxx settings
511 xxx html generators
512 xxx document cookie