X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F05_Authentication.pod;h=80339fe1f155cb72baf6b9674127f8e5d323a739;hp=c6f43dbe85f784fc77e5d4a43f2bfa62fe35a59a;hb=f4e9de4a3171fd75d04fa8e294fd9a0ae367bc18;hpb=477a6d5b13f55eb335979812080e4a11217f19d6 diff --git a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod index c6f43db..80339fe 100644 --- a/lib/Catalyst/Manual/Tutorial/05_Authentication.pod +++ b/lib/Catalyst/Manual/Tutorial/05_Authentication.pod @@ -80,7 +80,7 @@ application. First, we add both user and role information to the database (we will add the role information here although it will not be used until the authorization section, Chapter 6). Create a new SQL script file by -opening C in your editor and insert: +opening F in your editor and insert: -- -- Add users and role tables, along with a many-to-many join table @@ -117,7 +117,7 @@ opening C in your editor and insert: INSERT INTO user_role VALUES (2, 1); INSERT INTO user_role VALUES (3, 1); -Then load this into the C database with the following command: +Then load this into the F database with the following command: $ sqlite3 myapp.db < myapp02.sql @@ -142,7 +142,7 @@ for us: Author.pm BookAuthor.pm Book.pm Role.pm User.pm UserRole.pm Notice how the helper has added three new table-specific Result Source -files to the C directory. And, more +files to the F directory. And, more importantly, even if there were changes to the existing result source files, those changes would have only been written above the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and your hand-edited @@ -157,7 +157,7 @@ and C relationships for the new User, UserRole, and Role tables. However, as a convenience for mapping Users to their assigned roles (see L), we will also manually add a C relationship. Edit -C add the following information between +F add the following information between the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and the closing C<1;>: @@ -178,9 +178,9 @@ B Books to Authors, here we are only adding the convenience C in the Users to Roles direction. Note that we do not need to make any change to the -C schema file. It simply tells DBIC to load all of +F schema file. It simply tells DBIC to load all of the Result Class and ResultSet Class files it finds below the -C directory, so it will automatically pick up our new +F directory, so it will automatically pick up our new table information. @@ -217,7 +217,7 @@ Catalyst under C. =head2 Include Authentication and Session Plugins -Edit C and update it as follows (everything below +Edit F and update it as follows (everything below C is new): # Load plugins @@ -225,11 +225,11 @@ C is new): -Debug ConfigLoader Static::Simple - + StackTrace - + Authentication - + Session Session::Store::File Session::State::Cookie @@ -280,8 +280,8 @@ L because it automatically sets a reasonable set of defaults for us. (Note: the C here has nothing to do with the SimpleDB offered in Amazon's web services offerings -- here we are only talking about a "simple" way to use your -DB as an authentication backend.) Open C and place the -following text above the call to C<__PACKAGE__-Esetup();>: +DB as an authentication backend.) Open F and place the +following text above the call to C<< __PACKAGE__->setup(); >>: # Configure SimpleDB Authentication __PACKAGE__->config( @@ -294,15 +294,15 @@ following text above the call to C<__PACKAGE__-Esetup();>: }, ); -We could have placed this configuration in C, but placing it -in C is probably a better place since it's not likely +We could have placed this configuration in F, but placing it +in F is probably a better place since it's not likely something that users of your application will want to change during deployment (or you could use a mixture: leave C and C -defined in C as we show above, but place C -in C to allow the type of password to be easily modified +defined in F as we show above, but place C +in F to allow the type of password to be easily modified during deployment). We will stick with putting all of the -authentication-related configuration in C for the -tutorial, but if you wish to use C, just convert to the +authentication-related configuration in F for the +tutorial, but if you wish to use F, just convert to the following code: @@ -314,7 +314,7 @@ following code: B Here is a short script that will dump the contents of -Cconfig> to L format in C: +Cconfig> to L format in F: $ CATALYST_DEBUG=0 perl -Ilib -e 'use MyApp; use Config::General; Config::General->new->save_file("myapp.conf", MyApp->config);' @@ -345,22 +345,22 @@ have a C controller with both C and C actions. Remember, Catalyst is designed to be very flexible, and leaves such matters up to you, the designer and programmer. -Then open C, and update the definition of +Then open F, and update the definition of C to match: =head2 index - + Login logic - + =cut - + sub index :Path :Args(0) { my ($self, $c) = @_; - + # Get the username and password from form my $username = $c->request->params->{username}; my $password = $c->request->params->{password}; - + # If the username and password values were found in form if ($username && $password) { # Attempt to log the user in @@ -379,7 +379,7 @@ C to match: $c->stash(error_msg => "Empty username or password.") unless ($c->user_exists); } - + # If either of above don't work out, send to the login page $c->stash(template => 'login.tt2'); } @@ -408,20 +408,20 @@ specific with the C<:Args(0)> action modifier -- this forces the match on I C, not C. Next, update the corresponding method in -C to match: +F to match: =head2 index - + Logout logic - + =cut - + sub index :Path :Args(0) { my ($self, $c) = @_; - + # Clear the user's state $c->logout; - + # Send the user to the starting point $c->response->redirect($c->uri_for('/')); } @@ -429,10 +429,10 @@ C to match: =head2 Add a Login Form TT Template Page -Create a login form by opening C and inserting: +Create a login form by opening F and inserting: [% META title = 'Login' %] - +
@@ -457,23 +457,23 @@ We need something that provides enforcement for the authentication mechanism -- a I mechanism that prevents users who have not passed authentication from reaching any pages except the login page. This is generally done via an C action/method in -C. +F. -Edit the existing C class file and insert +Edit the existing F class file and insert the following method: =head2 auto - + Check if there is a user and, if not, forward to login page - + =cut - + # Note that 'auto' runs after 'begin' but before your actions and that # 'auto's "chain" (all from application path to most specific class are run) # See the 'Actions' section of 'Catalyst::Manual::Intro' for more info. sub auto :Private { my ($self, $c) = @_; - + # Allow unauthenticated users to reach the login page. This # allows unauthenticated users to reach any action in the Login # controller. To lock it down to a single action, we could use: @@ -483,7 +483,7 @@ the following method: if ($c->controller eq $c->controller('Login')) { return 1; } - + # If a user doesn't exist, force login if (!$c->user_exists) { # Dump a log message to the development server debug output @@ -493,7 +493,7 @@ the following method: # Return 0 to cancel 'post-auto' processing and prevent use of application return 0; } - + # User found, so return 1 to continue with processing after this 'auto' return 1; } @@ -503,7 +503,7 @@ L method from the application/root controller down to the most specific controller will be called. By placing the authentication enforcement code inside the C method of -C (or C), it will be called +F (or F), it will be called for I request that is received by the entire application. @@ -511,7 +511,7 @@ for I request that is received by the entire application. Let's say you want to provide some information on the login page that changes depending on whether the user has authenticated yet. To do -this, open C in your editor and add the following +this, open F in your editor and add the following lines to the bottom of the file: ... @@ -572,7 +572,7 @@ UDP vs. the more common TCP that you see with most Internet protocols. Worse case, you might have to manually set the time on your development box instead of using NTP. -Open C and add the following lines to the +Open F and add the following lines to the bottom (below the closing
tag): ... @@ -624,7 +624,7 @@ generates for us. Simply use the same command we saw in Chapters 3 and If you then open one of the Result Classes, you will see that it includes PassphraseColumn in the C line. Take a look -at C since that's the main class where +at F since that's the main class where we want to use hashed and salted passwords: __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "PassphraseColumn"); @@ -632,7 +632,7 @@ we want to use hashed and salted passwords: =head2 Modify the "password" Column to Use PassphraseColumn -Open the file C and enter the following +Open the file F and enter the following text below the "# DO NOT MODIFY THIS OR ANYTHING ABOVE!" line but above the closing "1;": @@ -644,7 +644,7 @@ the closing "1;": passphrase_class => 'SaltedDigest', passphrase_args => { algorithm => 'SHA-1', - salt_random => 20. + salt_random => 20, }, passphrase_check_method => 'check_password', }, @@ -667,20 +667,20 @@ class supports. Next, let's create a quick script to load some hashed and salted passwords into the C column of our C table. Open the -file C in your editor and enter the following +file F in your editor and enter the following text: #!/usr/bin/perl - + use strict; use warnings; - + use MyApp::Schema; - + my $schema = MyApp::Schema->connect('dbi:SQLite:myapp.db'); - + my @users = $schema->resultset('User')->all; - + foreach my $user (@users) { $user->password('mypass'); $user->update; @@ -696,7 +696,7 @@ Then run the following command: $ DBIC_TRACE=1 perl -Ilib set_hashed_passwords.pl We had to use the C<-Ilib> argument to tell Perl to look under the -C directory for our C model. +F directory for our C model. The DBIC_TRACE output should show that the update worked: @@ -726,7 +726,7 @@ your web application -- a very useful feature in many situations. =head2 Enable Hashed and Salted Passwords -Edit C and update the config() section for +Edit F and update the config() section for C it to match the following text (the only change is to the C field): @@ -742,14 +742,13 @@ change is to the C field): ); The use of C will cause -Catalyst::Plugin::Authentication::Store::DBIC to call the +Catalyst::Plugin::Authentication::Store::DBIx::Class to call the C method we enabled on our C columns. - =head2 Try Out the Hashed Passwords The development server should restart as soon as your save the -C file in the previous section. You should now be able to +F file in the previous section. You should now be able to go to L and login as before. When done, click the "logout" link on the login page (or point your browser at L). @@ -767,31 +766,31 @@ redirect with query parameters" code seen at the end of the L chapter of the tutorial to take advantage of C. -First, open C and modify C to +First, open F and modify C to match the following (everything after the model search line of code has changed): =head2 delete - + Delete a book - + =cut - + sub delete :Chained('object') :PathPart('delete') :Args(0) { my ($self, $c) = @_; - + # Use the book object saved by 'object' and delete it along # with related 'book_authors' entries $c->stash->{object}->delete; - + # Use 'flash' to save information across requests until it's read $c->flash->{status_msg} = "Book deleted"; - + # Redirect the user back to the list page $c->response->redirect($c->uri_for($self->action_for('list'))); } -Next, open C and update the TT code to pull from +Next, open F and update the TT code to pull from flash vs. the C query parameter: ... @@ -807,7 +806,7 @@ flash vs. the C query parameter: Although the sample above only shows the C div, leave the rest of the file intact -- the only change we made to replace "|| c.request.params.status_msg" with "c.flash.status_msg" in the -Cspan class="message"E> line. +C<< >> line. =head2 Try Out Flash @@ -833,7 +832,7 @@ we used above. Consult L for additional information. -=head2 Switch To Catalyst::Plugin::StatusMessages +=head2 Switch To Catalyst::Plugin::StatusMessages Although the query parameter technique we used in L and the C @@ -856,38 +855,38 @@ single time (even though the URL may continue to reference the token, it's only displayed the first time). The use of C or a similar mechanism is recommended for all Catalyst applications. -To enable C, first edit C and add +To enable C, first edit F and add C to the list of plugins: use Catalyst qw/ -Debug ConfigLoader Static::Simple - + StackTrace - + Authentication - + Session Session::Store::File Session::State::Cookie - + StatusMessage /; -Then edit C and modify the C +Then edit F and modify the C action to match the following: sub delete :Chained('object') :PathPart('delete') :Args(0) { my ($self, $c) = @_; - + # Saved the PK id for status_msg below my $id = $c->stash->{object}->id; - + # Use the book object saved by 'object' and delete it along # with related 'book_authors' entries $c->stash->{object}->delete; - + # Redirect the user back to the list page $c->response->redirect($c->uri_for($self->action_for('list'), {mid => $c->set_status_msg("Deleted book $id")})); @@ -904,18 +903,18 @@ Next, we need to make sure that the list page will load display the message. The easiest way to do this is to take advantage of the chained dispatch we implemented in L. Edit -C again and update the C action to +F again and update the C action to match: sub base :Chained('/') :PathPart('books') :CaptureArgs(0) { my ($self, $c) = @_; - + # Store the ResultSet in stash so it's available for other methods $c->stash(resultset => $c->model('DB::Book')); - + # Print a message to the debug log $c->log->debug('*** INSIDE BASE METHOD ***'); - + # Load status messages $c->load_status_msgs; } @@ -929,10 +928,10 @@ for C from: to: - sub list :Chained('base') :PathParth('list') :Args(0) { + sub list :Chained('base') :PathPart('list') :Args(0) { Finally, let's clean up the status/error message code in our wrapper -template. Edit C and change the "content" div +template. Edit F and change the "content" div to match the following: