Debian Codegraph @

README

raw
1 NAME
2 Catalyst::Plugin::Authentication - Infrastructure plugin for the
3 Catalyst authentication framework.
4
5 SYNOPSIS
6 use Catalyst qw/
7 Authentication
8 /;
9
10 # later on ...
11 $c->authenticate({ username => 'myusername',
12 password => 'mypassword' });
13 my $age = $c->user->get('age');
14 $c->logout;
15
16 DESCRIPTION
17 The authentication plugin provides generic user support for Catalyst
18 apps. It is the basis for both authentication (checking the user is who
19 they claim to be), and authorization (allowing the user to do what the
20 system authorises them to do).
21
22 Using authentication is split into two parts. A Store is used to
23 actually store the user information, and can store any amount of data
24 related to the user. Credentials are used to verify users, using
25 information from the store, given data from the frontend. A Credential
26 and a Store are paired to form a 'Realm'. A Catalyst application using
27 the authentication framework must have at least one realm, and may have
28 several.
29
30 To implement authentication in a Catalyst application you need to add
31 this module, and specify at least one realm in the configuration.
32
33 Authentication data can also be stored in a session, if the application
34 is using the Catalyst::Plugin::Session module.
35
36 NOTE in version 0.10 of this module, the interface to this module
37 changed. Please see "COMPATIBILITY ROUTINES" for more information.
38
39 INTRODUCTION
40 The Authentication/Authorization Process
41 Web applications typically need to identify a user - to tell the user
42 apart from other users. This is usually done in order to display private
43 information that is only that user's business, or to limit access to the
44 application so that only certain entities can access certain parts.
45
46 This process is split up into several steps. First you ask the user to
47 identify themselves. At this point you can't be sure that the user is
48 really who they claim to be.
49
50 Then the user tells you who they are, and backs this claim with some
51 piece of information that only the real user could give you. For
52 example, a password is a secret that is known to both the user and you.
53 When the user tells you this password you can assume they're in on the
54 secret and can be trusted (ignore identity theft for now). Checking the
55 password, or any other proof is called credential verification.
56
57 By this time you know exactly who the user is - the user's identity is
58 authenticated. This is where this module's job stops, and your
59 application or other plugins step in.
60
61 The next logical step is authorization, the process of deciding what a
62 user is (or isn't) allowed to do. For example, say your users are split
63 into two main groups - regular users and administrators. You want to
64 verify that the currently logged in user is indeed an administrator
65 before performing the actions in an administrative part of your
66 application. These decisions may be made within your application code
67 using just the information available after authentication, or it may be
68 facilitated by a number of plugins.
69
70 The Components In This Framework
71 Realms
72 Configuration of the Catalyst::Plugin::Authentication framework is done
73 in terms of realms. In simplest terms, a realm is a pairing of a
74 Credential verifier and a User storage (Store) backend. As of version
75 0.10003, realms are now objects that you can create and customize.
76
77 An application can have any number of Realms, each of which operates
78 independent of the others. Each realm has a name, which is used to
79 identify it as the target of an authentication request. This name can be
80 anything, such as 'users' or 'members'. One realm must be defined as the
81 default_realm, which is used when no realm name is specified. More
82 information about configuring realms is available in the configuration
83 section.
84
85 Credential Verifiers
86 When user input is transferred to the Catalyst application (typically
87 via form inputs) the application may pass this information into the
88 authentication system through the "$c->authenticate()" method. From
89 there, it is passed to the appropriate Credential verifier.
90
91 These plugins check the data, and ensure that it really proves the user
92 is who they claim to be.
93
94 Credential verifiers compatible with versions of this module 0.10x and
95 upwards should be in the namespace
96 "Catalyst::Authentication::Credential".
97
98 Storage Backends
99 The authentication data also identifies a user, and the Storage backend
100 modules use this data to locate and return a standardized
101 object-oriented representation of a user.
102
103 When a user is retrieved from a store it is not necessarily
104 authenticated. Credential verifiers accept a set of authentication data
105 and use this information to retrieve the user from the store they are
106 paired with.
107
108 Storage backends compatible with versions of this module 0.10x and
109 upwards should be in the namespace "Catalyst::Authentication::Store".
110
111 The Core Plugin
112 This plugin on its own is the glue, providing realm configuration,
113 session integration, and other goodness for the other plugins.
114
115 Other Plugins
116 More layers of plugins can be stacked on top of the authentication code.
117 For example, Catalyst::Plugin::Session::PerUser provides an abstraction
118 of browser sessions that is more persistent per user.
119 Catalyst::Plugin::Authorization::Roles provides an accepted way to
120 separate and group users into categories, and then check which
121 categories the current user belongs to.
122
123 EXAMPLE
124 Let's say we were storing users in a simple Perl hash. Users are
125 verified by supplying a password which is matched within the hash.
126
127 This means that our application will begin like this:
128
129 package MyApp;
130
131 use Catalyst qw/
132 Authentication
133 /;
134
135 __PACKAGE__->config( 'Plugin::Authentication' =>
136 {
137 default => {
138 credential => {
139 class => 'Password',
140 password_field => 'password',
141 password_type => 'clear'
142 },
143 store => {
144 class => 'Minimal',
145 users => {
146 bob => {
147 password => "s00p3r",
148 editor => 'yes',
149 roles => [qw/edit delete/],
150 },
151 william => {
152 password => "s3cr3t",
153 roles => [qw/comment/],
154 }
155 }
156 }
157 }
158 }
159 );
160
161 This tells the authentication plugin what realms are available, which
162 credential and store modules are used, and the configuration of each.
163 With this code loaded, we can now attempt to authenticate users.
164
165 To show an example of this, let's create an authentication controller:
166
167 package MyApp::Controller::Auth;
168
169 sub login : Local {
170 my ( $self, $c ) = @_;
171
172 if ( my $user = $c->req->params->{user}
173 and my $password = $c->req->params->{password} )
174 {
175 if ( $c->authenticate( { username => $user,
176 password => $password } ) ) {
177 $c->res->body( "hello " . $c->user->get("name") );
178 } else {
179 # login incorrect
180 }
181 }
182 else {
183 # invalid form input
184 }
185 }
186
187 This code should be self-explanatory. If all the necessary fields are
188 supplied, call the "authenticate" method on the context object. If it
189 succeeds the user is logged in.
190
191 The credential verifier will attempt to retrieve the user whose details
192 match the authentication information provided to "$c->authenticate()".
193 Once it fetches the user the password is checked and if it matches the
194 user will be authenticated and "$c->user" will contain the user object
195 retrieved from the store.
196
197 In the above case, the default realm is checked, but we could just as
198 easily check an alternate realm. If this were an admin login, for
199 example, we could authenticate on the admin realm by simply changing the
200 "$c->authenticate()" call:
201
202 if ( $c->authenticate( { username => $user,
203 password => $password }, 'admin' ) ) {
204 $c->res->body( "hello " . $c->user->get("name") );
205 } ...
206
207 Now suppose we want to restrict the ability to edit to a user with an
208 'editor' value of yes.
209
210 The restricted action might look like this:
211
212 sub edit : Local {
213 my ( $self, $c ) = @_;
214
215 $c->detach("unauthorized")
216 unless $c->user_exists
217 and $c->user->get('editor') eq 'yes';
218
219 # do something restricted here
220 }
221
222 (Note that if you have multiple realms, you can use
223 "$c->user_in_realm('realmname')" in place of "$c->user_exists();" This
224 will essentially perform the same verification as user_exists, with the
225 added requirement that if there is a user, it must have come from the
226 realm specified.)
227
228 The above example is somewhat similar to role based access control.
229 Catalyst::Authentication::Store::Minimal treats the roles field as an
230 array of role names. Let's leverage this. Add the role authorization
231 plugin:
232
233 use Catalyst qw/
234 ...
235 Authorization::Roles
236 /;
237
238 sub edit : Local {
239 my ( $self, $c ) = @_;
240
241 $c->detach("unauthorized") unless $c->check_user_roles("edit");
242
243 # do something restricted here
244 }
245
246 This is somewhat simpler and will work if you change your store, too,
247 since the role interface is consistent.
248
249 Let's say your app grows, and you now have 10,000 users. It's no longer
250 efficient to maintain a hash of users, so you move this data to a
251 database. You can accomplish this simply by installing the DBIx::Class
252 Store and changing your config:
253
254 __PACKAGE__->config( 'Plugin::Authentication' =>
255 {
256 default_realm => 'members',
257 members => {
258 credential => {
259 class => 'Password',
260 password_field => 'password',
261 password_type => 'clear'
262 },
263 store => {
264 class => 'DBIx::Class',
265 user_model => 'MyApp::Users',
266 role_column => 'roles',
267 }
268 }
269 }
270 );
271
272 The authentication system works behind the scenes to load your data from
273 the new source. The rest of your application is completely unchanged.
274
275 CONFIGURATION
276 # example
277 __PACKAGE__->config( 'Plugin::Authentication' =>
278 {
279 default_realm => 'members',
280
281 members => {
282 credential => {
283 class => 'Password',
284 password_field => 'password',
285 password_type => 'clear'
286 },
287 store => {
288 class => 'DBIx::Class',
289 user_model => 'MyApp::Users',
290 role_column => 'roles',
291 }
292 },
293 admins => {
294 credential => {
295 class => 'Password',
296 password_field => 'password',
297 password_type => 'clear'
298 },
299 store => {
300 class => '+MyApp::Authentication::Store::NetAuth',
301 authserver => '192.168.10.17'
302 }
303 }
304 }
305 );
306
307 NOTE: Until version 0.10008 of this module, you would need to put all
308 the realms inside a "realms" key in the configuration. Please see
309 "COMPATIBILITY CONFIGURATION" for more information
310
311 use_session
312 Whether or not to store the user's logged in state in the session,
313 if the application is also using Catalyst::Plugin::Session. This
314 value is set to true per default.
315
316 However, even if use_session is disabled, if any code touches
317 $c->session, a session object will be auto-vivified and session
318 Cookies will be sent in the headers. To prevent accidental session
319 creation, check if a session already exists with if ($c->sessionid)
320 { ... }. If the session doesn't exist, then don't place anything in
321 the session to prevent an unecessary session from being created.
322
323 default_realm
324 This defines which realm should be used as when no realm is provided
325 to methods that require a realm such as authenticate or find_user.
326
327 realm refs
328 The Plugin::Authentication config hash contains the series of realm
329 configurations you want to use for your app. The only rule here is
330 that there must be at least one. A realm consists of a name, which
331 is used to reference the realm, a credential and a store. You may
332 also put your realm configurations within a subelement called
333 'realms' if you desire to separate them from the remainder of your
334 configuration. Note that if you use a 'realms' subelement, you must
335 put ALL of your realms within it.
336
337 You can also specify a realm class to instantiate instead of the
338 default Catalyst::Authentication::Realm class using the 'class'
339 element within the realm config.
340
341 Each realm config contains two hashes, one called 'credential' and
342 one called 'store', each of which provide configuration details to
343 the respective modules. The contents of these hashes is specific to
344 the module being used, with the exception of the 'class' element,
345 which tells the core Authentication module the classname to
346 instantiate.
347
348 The 'class' element follows the standard Catalyst mechanism of class
349 specification. If a class is prefixed with a +, it is assumed to be
350 a complete class name. Otherwise it is considered to be a portion of
351 the class name. For credentials, the classname 'Password', for
352 example, is expanded to
353 Catalyst::Authentication::Credential::Password. For stores, the
354 classname 'storename' is expanded to:
355 Catalyst::Authentication::Store::storename.
356
357 METHODS
358 $c->authenticate( $userinfo [, $realm ])
359 Attempts to authenticate the user using the information in the $userinfo
360 hash reference using the realm $realm. $realm may be omitted, in which
361 case the default realm is checked.
362
363 $c->user( )
364 Returns the currently logged in user, or undef if there is none.
365 Normally the user is re-retrieved from the store. For
366 Catalyst::Authentication::Store::DBIx::Class the user is re-restored
367 using the primary key of the user table. Thus user can throw an error
368 even though user_exists returned true.
369
370 $c->user_exists( )
371 Returns true if a user is logged in right now. The difference between
372 user_exists and user is that user_exists will return true if a user is
373 logged in, even if it has not been yet retrieved from the storage
374 backend. If you only need to know if the user is logged in, depending on
375 the storage mechanism this can be much more efficient. user_exists only
376 looks into the session while user is trying to restore the user.
377
378 $c->user_in_realm( $realm )
379 Works like user_exists, except that it only returns true if a user is
380 both logged in right now and was retrieved from the realm provided.
381
382 $c->logout( )
383 Logs the user out. Deletes the currently logged in user from "$c->user"
384 and the session. It does not delete the session.
385
386 $c->find_user( $userinfo, $realm )
387 Fetch a particular users details, matching the provided user info, from
388 the realm specified in $realm.
389
390 $user = $c->find_user({ id => $id });
391 $c->set_authenticated($user); # logs the user in and calls persist_user
392
393 persist_user()
394 Under normal circumstances the user data is only saved to the session
395 during initial authentication. This call causes the auth system to save
396 the currently authenticated user's data across requests. Useful if you
397 have changed the user data and want to ensure that future requests
398 reflect the most current data. Assumes that at the time of this call,
399 $c->user contains the most current data.
400
401 find_realm_for_persisted_user()
402 Private method, do not call from user code!
403
404 INTERNAL METHODS
405 These methods are for Catalyst::Plugin::Authentication INTERNAL USE
406 only. Please do not use them in your own code, whether application or
407 credential / store modules. If you do, you will very likely get the
408 nasty shock of having to fix / rewrite your code when things change.
409 They are documented here only for reference.
410
411 $c->set_authenticated( $user, $realmname )
412 Marks a user as authenticated. This is called from within the
413 authenticate routine when a credential returns a user. $realmname
414 defaults to 'default'. You can use find_user to get $user
415
416 $c->auth_restore_user( $user, $realmname )
417 Used to restore a user from the session. In most cases this is called
418 without arguments to restore the user via the session. Can be called
419 with arguments when restoring a user from some other method. Currently
420 not used in this way.
421
422 $c->auth_realms( )
423 Returns a hashref containing realmname -> realm instance pairs. Realm
424 instances contain an instantiated store and credential object as the
425 'store' and 'credential' elements, respectively
426
427 $c->get_auth_realm( $realmname )
428 Retrieves the realm instance for the realmname provided.
429
430 $c->update_user_in_session
431 This was a short-lived method to update user information - you should
432 use persist_user instead.
433
434 $c->setup_auth_realm( )
435 OVERRIDDEN METHODS
436 $c->setup( )
437 SEE ALSO
438 This list might not be up to date. Below are modules known to work with
439 the updated API of 0.10 and are therefore compatible with realms.
440
441 Realms
442 Catalyst::Authentication::Realm
443
444 User Storage Backends
445 Catalyst::Authentication::Store::Minimal
446 Catalyst::Authentication::Store::DBIx::Class
447 Catalyst::Authentication::Store::LDAP
448 Catalyst::Authentication::Store::RDBO
449 Catalyst::Authentication::Store::Model::KiokuDB
450 Catalyst::Authentication::Store::Jifty::DBI
451 Catalyst::Authentication::Store::Htpasswd
452
453 Credential verification
454 Catalyst::Authentication::Credential::Password
455 Catalyst::Authentication::Credential::HTTP
456 Catalyst::Authentication::Credential::OpenID
457 Catalyst::Authentication::Credential::Authen::Simple
458 Catalyst::Authentication::Credential::Flickr
459 Catalyst::Authentication::Credential::Testing
460 Catalyst::Authentication::Credential::AuthTkt
461 Catalyst::Authentication::Credential::Kerberos
462
463 Authorization
464 Catalyst::Plugin::Authorization::ACL,
465 Catalyst::Plugin::Authorization::Roles
466
467 Internals Documentation
468 Catalyst::Plugin::Authentication::Internals
469
470 Misc
471 Catalyst::Plugin::Session, Catalyst::Plugin::Session::PerUser
472
473 DON'T SEE ALSO
474 This module along with its sub plugins deprecate a great number of other
475 modules. These include Catalyst::Plugin::Authentication::Simple,
476 Catalyst::Plugin::Authentication::CDBI.
477
478 INCOMPATABILITIES
479 The realms-based configuration and functionality of the 0.10 update of
480 Catalyst::Plugin::Authentication required a change in the API used by
481 credentials and stores. It has a compatibility mode which allows use of
482 modules that have not yet been updated. This, however, completely mimics
483 the older api and disables the new realm-based features. In other words
484 you cannot mix the older credential and store modules with realms, or
485 realm-based configs. The changes required to update modules are
486 relatively minor and are covered in
487 Catalyst::Plugin::Authentication::Internals. We hope that most modules
488 will move to the compatible list above very quickly.
489
490 COMPATIBILITY CONFIGURATION
491 Until version 0.10008 of this module, you needed to put all the realms
492 inside a "realms" key in the configuration.
493
494 # example
495 __PACKAGE__->config( 'Plugin::Authentication' =>
496 {
497 default_realm => 'members',
498 realms => {
499 members => {
500 ...
501 },
502 },
503 }
504 );
505
506 If you use the old, deprecated "__PACKAGE__->config( 'authentication' )"
507 configuration key, then the realms key is still required.
508
509 COMPATIBILITY ROUTINES
510 In version 0.10 of Catalyst::Plugin::Authentication, the API changed.
511 For app developers, this change is fairly minor, but for Credential and
512 Store authors, the changes are significant.
513
514 Please see the documentation in version 0.09 of
515 Catalyst::Plugin::Authentication for a better understanding of how the
516 old API functioned.
517
518 The items below are still present in the plugin, though using them is
519 deprecated. They remain only as a transition tool, for those sites which
520 can not yet be upgraded to use the new system due to local
521 customizations or use of Credential / Store modules that have not yet
522 been updated to work with the new API.
523
524 These routines should not be used in any application using realms
525 functionality or any of the methods described above. These are for
526 reference purposes only.
527
528 $c->login( )
529 This method is used to initiate authentication and user retrieval.
530 Technically this is part of the old Password credential module and it
531 still resides in the Password class. It is included here for reference
532 only.
533
534 $c->default_auth_store( )
535 Return the store whose name is 'default'.
536
537 This is set to "$c->config( 'Plugin::Authentication' => { store => #
538 Store} )" if that value exists, or by using a Store plugin:
539
540 # load the Minimal authentication store.
541 use Catalyst qw/Authentication Authentication::Store::Minimal/;
542
543 Sets the default store to
544 Catalyst::Plugin::Authentication::Store::Minimal.
545
546 $c->get_auth_store( $name )
547 Return the store whose name is $name.
548
549 $c->get_auth_store_name( $store )
550 Return the name of the store $store.
551
552 $c->auth_stores( )
553 A hash keyed by name, with the stores registered in the app.
554
555 $c->register_auth_stores( %stores_by_name )
556 Register stores into the application.
557
558 $c->auth_store_names( )
559 $c->get_user( )
560 SUPPORT
561 Please use the rt.cpan.org bug tracker, and git patches are wecome.
562
563 Questions on usage should be directed to the Catalyst mailing list or
564 the #catalyst irc channel.
565
566 AUTHORS
567 Yuval Kogman, "[email protected]" - original author
568
569 Jay Kuri, "[email protected]" - Large rewrite
570
571 PRIMARY MAINTAINER
572 Tomas Doran (t0m), "[email protected]"
573
574 ADDITIONAL CONTRIBUTORS
575 Jess Robinson
576 David Kamholz
577 kmx
578 Nigel Metheringham
579 Florian Ragwitz "[email protected]"
580 Stephan Jauernick "[email protected]"
581 Oskari Ojala (Okko), "[email protected]"
582 John Napiorkowski (jnap) "[email protected]"
583
584 COPYRIGHT & LICENSE
585 Copyright (c) 2005 - 2012 the Catalyst::Plugin::Authentication
586 "AUTHORS", "PRIMARY MAINTAINER" and "ADDITIONAL CONTRIBUTORS" as listed
587 above.
588
589 This program is free software; you can redistribute it and/or modify it
590 under the same terms as Perl itself.
591