1Catalyst::Plugin::AutheUnsteircaCtoinotnr:iC:baIutntateledyrsnPtae:lr:slP(l3Du)ogciunm:e:nAtuatthieonntication::Internals(3)
2
3
4
6 Catalyst::Plugin::Authentication::Internals - All about authentication
7 Stores and Credentials
8
10 Catalyst::Plugin::Authentication provides a standard authentication
11 interface to application developers using the Catalyst framework. It is
12 designed to allow application developers to use various methods of user
13 storage and credential verification. It is also designed to provide for
14 minimal change to the application when switching between different
15 storage and credential verification methods.
16
17 While Catalyst::Plugin::Authentication provides the interface to the
18 application developer, the actual work of verifying the credentials and
19 retrieving users is delegated to separate modules. These modules are
20 called Credentials and storage backends, or Stores, respectively. For
21 authentication to function there must be at least one credential and
22 one store. A pairing of a store and a credential is referred to as a
23 Realm. There may be any number of realms defined for an application,
24 though most applications will not require more than one or two.
25
26 The details of using this module can be found in the
27 Catalyst::Plugin::Authentication documentation.
28
29 What follows is an explanation of how the module functions internally
30 and what is required to implement a credential or a store.
31
33 There are two main entry points you need to be aware of when writing a
34 store or credential module. The first is initialization and the second
35 is during the actual call to the Catalyst application's authenticate
36 method.
37
38 A simplified description of the authentication process follows:
39
40 Initialization
41
42 Realm Setup - for each realm:
43
44 1) The Realm is instantiated using new() method
45
46 2) The Store is instantiated using new() method
47
48 3) The Credential Instantiated using new() method
49
50 4) Credential and Store objects tied to realm for use during
51 requests
52
53 Authentication
54
55 "$c->authenticate( $userinfo, $realm )" called
56
57 1) Credential object retrieved for realm provided
58
59 2) Credential's authenticate() method called with authinfo and
60 realm object for current realm
61
62 The realm object and the authinfo hash are provided to the
63 credential object's authenticate call. In most cases the
64 credential object will attempt to retrieve a user using the
65 realm's find_user() method, which by default relays the
66 call directly to the Store's find_user() method. It will
67 then usually compare the retrieved user's information with
68 the information provided in the $authinfo hash. This is how
69 the default 'Password' credential functions. If the
70 credentials match, the authenticate() method should return
71 a user object.
72
73 3) User object stored in session
74
75 If the user object supports session storage, the
76 successfully authenticated user will be placed in session
77 storage. This is done by calling the realm object's
78 persist_user() method. The persist_user() routine by
79 default calls the Store's for_session() method, which
80 should return serialized data (IE a scalar). This
81 serialized data is passed back to the store via the
82 from_session() method, so the data should contain enough
83 information for the store to recreate / reload the user.
84
85 Sessions - Per-Request operations
86
87 When any user-related activity occurs, and $c->authenticate has not
88 yet been called, the Catalyst::Plugin::Authentication module will
89 attempt to restore the persisted user (normally from the session if
90 one is available). There is only one step in this process:
91
92 1) Store object's from_session() is called
93
94 The serialized data previously returned by the store's
95 for_session() method is provided to the from_session() method. The
96 from_session() method should return a valid user object.
97
98 Note that the for_session() is only called during the original
99 $c->authenticate() call, so if changes are made to the user that
100 need to be reflected in your session data, you will want to call
101 the $c->persist_user() method - which will perform the session
102 storage process again (complete with call to for_session()).
103
104 More detailed information about these processes is below.
105
106 INITIALIZATION
107 When the authentication module is loaded, it reads it's configuration
108 to determine the realms to set up for the application and which realm
109 is to be the default. For each realm defined in the application's
110 config, Catalyst::Plugin::Authentication instantiates both a new
111 credential object and a new store object. See below for the details of
112 how credentials and stores are instantiated.
113
114 NOTE: The instances created will remain active throughout the entire
115 lifetime of the application, and so should be relatively lightweight.
116 Care should be taken to ensure that they do not grow, or retain
117 information per request, because they will be involved in each
118 authentication request and could therefore substantially hurt memory
119 consumption over time.
120
121 AUTHENTICATION
122 When "$c->authenticate()" is called from within an application, the
123 objects created in the initialization process come into play.
124 "$c->authenticate()" takes two arguments. The first is a hash reference
125 containing all the information available about the user. This will be
126 used to locate the user in the store and verify the user's credentials.
127 The second argument is the realm to authenticate against. If the second
128 argument is omitted, the default realm is assumed.
129
130 The main authentication module then locates the credential and store
131 objects for the realm specified and calls the credential object's
132 authenticate() method. It provides three arguments, first the
133 application object, or $c, then a reference to the store object, and
134 finally the hashref provided in the "$c->authenticate" call. The main
135 authentication module expects the return value to be a reference to a
136 user object upon successful authentication. If it receives anything
137 aside from a reference, it is considered to be an authentication
138 failure. Upon success, the returned user is marked as authenticated and
139 the application can act accordingly, using "$c->user" to access the
140 authenticated user, etc.
141
142 Astute readers will note that the main Catalyst::Plugin::Authentication
143 module does not interact with the store in any way, save for passing a
144 reference to it to the credential. This is correct. The credential
145 object is responsible for obtaining the user from the provided store
146 using information from the userinfo hashref and/or data obtained during
147 the credential verification process.
148
150 There are two parts to an authentication store, the store object and
151 the user object.
152
153 STORAGE BACKEND
154 Writing a store is actually quite simple. There are only five methods
155 that must be implemented. They are:
156
157 new() - instantiates the store object
158 find_user() - locates a user using data contained in the hashref
159 for_session() - prepares a user to be stored in the session
160 from_session() - does any restoration required when obtaining a user from the session
161 user_supports() - provides information about what the user object supports
162
163 STORE METHODS
164
165 new( $config, $app, $realm )
166 The new() method is called only once, during the setup process of
167 Catalyst::Plugin::Authentication. The first argument, $config, is a
168 hash reference containing the configuration information for the
169 store module. The second argument is a reference to the Catalyst
170 application.
171
172 Note that when new() is called, Catalyst has not yet loaded the
173 various controller and model classes, nor is it definite that other
174 plugins have been loaded, so your new() method must not rely on any
175 of those being present. If any of this is required for your store
176 to function, you should defer that part of initialization until the
177 first method call.
178
179 The new() method should return a blessed reference to your store
180 object.
181
182 find_user( $authinfo, $c )
183 This is the workhorse of any authentication store. It's job is to
184 take the information provided to it via the $authinfo hashref and
185 locate the user that matches it. It should return a reference to a
186 user object. A return value of anything else is considered to mean
187 no user was found that matched the information provided.
188
189 How find_user() accomplishes it's job is entirely up to you, the
190 author, as is what $authinfo is required to contain. Many stores
191 will simply use a username element in $authinfo to locate the user,
192 but more advanced functionality is possible and you may bend the
193 $authinfo to your needs. Be aware, however, that both Credentials
194 and Stores usually work with the same $authinfo hash, so take care
195 to avoid overlapping element names.
196
197 Please note that this routine may be called numerous times in
198 various circumstances, and that a successful match for a user here
199 does NOT necessarily constitute successful authentication. Your
200 store class should never assume this and in most cases $c should
201 not be modified by your store object.
202
203 for_session( $c, $user )
204 This method is responsible for preparing a user object for storage
205 in the session. It should return information that can be placed in
206 the session and later used to restore a user object (using the
207 from_session() method). It should therefore ensure that whatever
208 information provided can be used by the from_session() method to
209 locate the unique user being saved. Note that there is no
210 guarantee that the same Catalyst instance will receive both the
211 for_session() and from_session() calls. You should take care to
212 provide information that can be used to restore a user, regardless
213 of the current state of the application. A good rule of thumb is
214 that if from_session() can revive the user with the given
215 information even if the Catalyst application has just started up,
216 you are in good shape.
217
218 from_session( $c, $frozenuser )
219 This method is called whenever a user is being restored from the
220 session. $frozenuser contains the information that was stored in
221 the session for the user. This will under normal circumstances be
222 the exact data your store returned from the previous call to
223 for_session(). from_session() should return a valid user object.
224
225 user_supports( $feature, ... )
226 This method allows credentials and other objects to inquire as to
227 what the underlying user object is capable of. This is pretty-well
228 free-form and the main purpose is to allow graceful integration
229 with credentials and applications that may provide advanced
230 functionality based on whether the underlying user object can do
231 certain things. In most cases you will want to pass this directly
232 to the underlying user class' "supports" method. Note that this is
233 used as a class method against the user class and therefore must be
234 able to function without an instantiated user object.
235
236 OPTIONAL STORE METHODS
237
238 If you want your store to be able to auto- create users, then you can
239 implement these methods:
240
241 auto_update_user( $authinfo, $c, $res )
242
243 This method is called if the realm's auto_update_user setting is true.
244
245 auto_create_user( $authinfo, $c )
246
247 This method is called if the realm's auto_create_user setting is true.
248
249 USER OBJECT
250 The user object is an important piece of your store module. It will be
251 the part of the system that the application developer will interact
252 with most. As such, the API for the user object is very rigid. All user
253 objects MUST inherit from Catalyst::Authentication::User.
254
255 USER METHODS
256
257 The routines required by the Catalyst::Plugin::Authentication plugin
258 are below. Note that of these, only get_object is strictly required, as
259 the Catalyst::Authentication::User base class contains reasonable
260 implementations of the rest. If you do choose to implement only the
261 get_object() routine, please read the base class code and documentation
262 so that you fully understand how the other routines will be implemented
263 for you.
264
265 Also, your user object can implement whatever additional methods you
266 require to provide the functionality you need. So long as the below are
267 implemented, and you don't overlap the base class' methods with
268 incompatible routines, you should experience no problems.
269
270 id( )
271 The id() method should return a unique id (scalar) that can be used
272 to retreive this user from the store. Often this will be provided
273 to the store's find_user() routine as "id => $user->id" so you
274 should ensure that your store's find_user() can cope with that.
275
276 supports( $feature, $subfeature ... )
277 This method checks to see if the user class supports a particular
278 feature. It is implemented such that each argument provides a
279 subfeature of the previous argument. In other words, passing 'foo',
280 'bar' would return true if the user supported the 'foo' feature,
281 and the 'bar' feature of 'foo'. This is implemented in
282 Catalyst::Authentication::User, so if your class inherits from
283 that, you do not need to implement this and can instead implement
284 supported_features().
285
286 Note: If you want the authentication module to be able to save your
287 user in the session you must return true when presented with the
288 feature 'session'.
289
290 supported_features( )
291 This method should return a hashref of features supported by the
292 user class. This is for more flexible integration with some
293 Credentials / applications. It is not required that you support
294 anything, and returning "undef" is perfectly acceptable and in most
295 cases what you will do.
296
297 get( $fieldname )
298 This method should return the value of the field matching fieldname
299 provided, or undef if there is no field matching that fieldname. In
300 most cases this will access the underlying storage mechanism for
301 the user data and return the information. This is used as a
302 standard method of accessing an authenticated user's data, and MUST
303 be implemented by all user objects.
304
305 Note: There is no equivalent 'set' method. Each user class is
306 likely to vary greatly in how data must be saved and it is
307 therefore impractical to try to provide a standard way of
308 accomplishing it. When an application developer needs to save data,
309 they should obtain the underlying object / data by calling
310 get_object, and work with it directly.
311
312 get_object( )
313 This method returns the underlying user object. If your user object
314 is backed by another object class, this method should return that
315 underlying object. This allows the application developer to obtain
316 an editable object. Generally speaking this will only be done by
317 developers who know what they are doing and require advanced
318 functionality which is either unforeseen or inconsistent across
319 user classes. If your object is not backed by another class, or you
320 need to provide additional intermediate functionality, it is
321 perfectly reasonable to return $self.
322
324 Compared to writing a store, writing a credential is very simple.
325 There is only one class to implement, and it consists of only two
326 required routines. They are:
327
328 new() - instantiates the credential object
329 authenticate() - performs the authentication and returns a user object
330
331 CREDENTIAL METHODS
332 new( $config, $app, $realm )
333 Like the Store method of the same name, the new() method is called
334 only once, during the setup process of
335 Catalyst::Plugin::Authentication. The first argument, $config, is a
336 hash reference containing the configuration information for the
337 credential module. The second argument is a reference to the
338 Catalyst application. $realm is the instantiated Realm object,
339 which you may use to access realm routines - such as find_user.
340
341 Again, when the credential's new() method is called, Catalyst has
342 not yet loaded the various controller and model classes.
343
344 The new method should perform any necessary setup required and
345 instantiate your credential object. It should return your
346 instantiated credential.
347
348 authenticate( $c, $realm, $authinfo )
349 This is the workhorse of your credential. When $c->authenticate()
350 is called the Catalyst::Plugin::Authentication module retrieves the
351 realm object and passes it, along with the $authinfo hash to your
352 credential's authenticate method. Your module should use the
353 $authinfo hash to obtain the user from the realm passed, and then
354 perform any credential verification steps necessary to authenticate
355 the user. This method should return the user object returned by
356 the authentication store if credential verification succeeded. It
357 should return undef on failure.
358
359 How your credential module performs the credential verification is
360 entirely up to you. In most cases, the credential will retrieve a
361 user from the store first (using the stores find_user() method),
362 and then validate the user's information. However, this does not
363 have to be the case.
364
365 It is perfectly acceptable for your credential to perform other
366 tasks prior to attempting to retrieve the user from the store. It
367 may also make sense for your credential to perform activities which
368 help to locate the user in question, for example, finding a user id
369 based on an encrypted token. In these scenarios, the $authinfo
370 hash passed to find_user() can be different than that which is
371 passed in to $c->authenticate(). Once again this is perfectly
372 acceptable if it makes sense for your credential, though you are
373 strongly advised to note this behavior clearly in your credential's
374 documentation - as application authors are almost certainly
375 expecting the user to be found using the information provided to
376 $c->authenticate().
377
378 Look at the Catalyst::Authentication::Credential::Password module
379 source to see this in action. In order to avoid possible
380 mismatches between the encrypted and unencrypted passwords, the
381 password credential actually removes the provided password from the
382 authinfo array. It does this because, in many cases, the store's
383 password field will be encrypted in some way, and the password
384 passed to $c->authenticate is almost certainly in plaintext.
385
386 NOTE: You should always assume that a store is going to use all the
387 information passed to it to locate the user in question. If there
388 are fields in the $authinfo hash that you are sure are specific to
389 your credential, you may want to consider removing them before user
390 retrieval. A better solution is to place those arguments that are
391 specific to your credential within their own subhash named after
392 your module.
393
394 The Catalyst::Authentication::Store::DBIx::Class module does this
395 in order to encapsulate arguments intended specifically for that
396 module. See the Catalyst::Authentication::Store::DBIx::Class::User
397 source for details.
398
400 Jay Kuri, "jayk@cpan.org"
401
403 Copyright (c) 2005 the aforementioned authors. All rights reserved.
404 This program is free software; you can redistribute it and/or modify it
405 under the same terms as Perl itself.
406
407
408
409perl v5.36.0 Ca2t0a2l3y-s0t1:-:2P0lugin::Authentication::Internals(3)