various format code fixups
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 05_Authentication.pod
index 9a87fae..1b6078f 100644 (file)
@@ -2,7 +2,6 @@
 
 Catalyst::Manual::Tutorial::05_Authentication - Catalyst Tutorial - Chapter 5: Authentication
 
-
 =head1 OVERVIEW
 
 This is B<Chapter 5 of 10> for the Catalyst tutorial.
@@ -68,7 +67,6 @@ of the Tutorial Virtual machine (one subdirectory per chapter).  There
 are also instructions for downloading the code in
 L<Catalyst::Manual::Tutorial::01_Intro>.
 
-
 =head1 BASIC AUTHENTICATION
 
 This section explores how to add authentication logic to a Catalyst
@@ -80,7 +78,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<myapp02.sql> in your editor and insert:
+opening F<myapp02.sql> in your editor and insert:
 
     --
     -- Add users and role tables, along with a many-to-many join table
@@ -117,7 +115,7 @@ opening C<myapp02.sql> 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<myapp.db> database with the following command:
+Then load this into the F<myapp.db> database with the following command:
 
     $ sqlite3 myapp.db < myapp02.sql
 
@@ -130,7 +128,7 @@ C<create=static> option on the DBIC model helper to do most of the work
 for us:
 
     $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
-        create=static component=TimeStamp dbi:SQLite:myapp.db \
+        create=static components=TimeStamp dbi:SQLite:myapp.db \
         on_connect_do="PRAGMA foreign_keys = ON"
      exists "/home/catalyst/dev/MyApp/script/../lib/MyApp/Model"
      exists "/home/catalyst/dev/MyApp/script/../t"
@@ -142,7 +140,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<lib/MyApp/Schema/Result> directory.  And, more
+files to the F<lib/MyApp/Schema/Result> 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 +155,7 @@ and C<belongs_to> relationships for the new User, UserRole, and Role
 tables. However, as a convenience for mapping Users to their assigned
 roles (see L<Chapter 6|Catalyst::Manual::Tutorial::06_Authorization>),
 we will also manually add a C<many_to_many> relationship. Edit
-C<lib/MyApp/Schema/Result/User.pm> add the following information between
+F<lib/MyApp/Schema/Result/User.pm> add the following information between
 the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment and the closing
 C<1;>:
 
@@ -178,9 +176,9 @@ B<AND> Books to Authors, here we are only adding the convenience
 C<many_to_many> in the Users to Roles direction.
 
 Note that we do not need to make any change to the
-C<lib/MyApp/Schema.pm> schema file.  It simply tells DBIC to load all of
+F<lib/MyApp/Schema.pm> schema file.  It simply tells DBIC to load all of
 the Result Class and ResultSet Class files it finds below the
-C<lib/MyApp/Schema> directory, so it will automatically pick up our new
+F<lib/MyApp/Schema> directory, so it will automatically pick up our new
 table information.
 
 
@@ -188,14 +186,14 @@ table information.
 
 We aren't ready to try out the authentication just yet; we only want to
 do a quick check to be sure our model loads correctly. Assuming that you
-are following along and using the "-r" option on C<myapp_server.pl>,
+are following along and using the "-r" option on F<myapp_server.pl>,
 then the development server should automatically reload (if not, press
 C<Ctrl-C> to break out of the server if it's running and then enter
-C<script/myapp_server.pl> to start it). Look for the three new model
+F<script/myapp_server.pl> to start it). Look for the three new model
 objects in the startup debug output:
 
     ...
-     .-------------------------------------------------------------------+----------.
+    .-------------------------------------------------------------------+----------.
     | Class                                                             | Type     |
     +-------------------------------------------------------------------+----------+
     | MyApp::Controller::Books                                          | instance |
@@ -217,7 +215,7 @@ Catalyst under C<MyApp::Model>.
 
 =head2 Include Authentication and Session Plugins
 
-Edit C<lib/MyApp.pm> and update it as follows (everything below
+Edit F<lib/MyApp.pm> and update it as follows (everything below
 C<StackTrace> is new):
 
     # Load plugins
@@ -225,11 +223,11 @@ C<StackTrace> is new):
         -Debug
         ConfigLoader
         Static::Simple
-    
+
         StackTrace
-    
+
         Authentication
-    
+
         Session
         Session::Store::File
         Session::State::Cookie
@@ -280,8 +278,8 @@ L<Catalyst::Authentication::Realm::SimpleDB> because it automatically
 sets a reasonable set of defaults for us.  (Note: the C<SimpleDB> 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<lib/MyApp.pm> and place the
-following text above the call to C<__PACKAGE__-E<gt>setup();>:
+DB as an authentication backend.)  Open F<lib/MyApp.pm> and place the
+following text above the call to C<< __PACKAGE__->setup(); >>:
 
     # Configure SimpleDB Authentication
     __PACKAGE__->config(
@@ -294,15 +292,15 @@ following text above the call to C<__PACKAGE__-E<gt>setup();>:
         },
     );
 
-We could have placed this configuration in C<myapp.conf>, but placing it
-in C<lib/MyApp.pm> is probably a better place since it's not likely
+We could have placed this configuration in F<myapp.conf>, but placing it
+in F<lib/MyApp.pm> 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<class> and C<user_model>
-defined in C<lib/MyApp.pm> as we show above, but place C<password_type>
-in C<myapp.conf> to allow the type of password to be easily modified
+defined in F<lib/MyApp.pm> as we show above, but place C<password_type>
+in F<myapp.conf> 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<lib/MyApp.pm> for the
-tutorial, but if you wish to use C<myapp.conf>, just convert to the
+authentication-related configuration in F<lib/MyApp.pm> for the
+tutorial, but if you wish to use F<myapp.conf>, just convert to the
 following code:
 
     <Plugin::Authentication>
@@ -314,7 +312,7 @@ following code:
     </Plugin::Authentication>
 
 B<TIP:> Here is a short script that will dump the contents of
-C<MyApp->config> to L<Config::General> format in C<myapp.conf>:
+C<< MyApp->config >> to L<Config::General> format in F<myapp.conf>:
 
     $ CATALYST_DEBUG=0 perl -Ilib -e 'use MyApp; use Config::General;
         Config::General->new->save_file("myapp.conf", MyApp->config);'
@@ -345,22 +343,22 @@ have a C<User> controller with both C<login> and C<logout> actions.
 Remember, Catalyst is designed to be very flexible, and leaves such
 matters up to you, the designer and programmer.
 
-Then open C<lib/MyApp/Controller/Login.pm>, and update the definition of
+Then open F<lib/MyApp/Controller/Login.pm>, and update the definition of
 C<sub index> 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 +377,7 @@ C<sub index> 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 +406,20 @@ specific with the C<:Args(0)> action modifier -- this forces the match
 on I<only> C</login>, not C</login/somethingelse>.
 
 Next, update the corresponding method in
-C<lib/MyApp/Controller/Logout.pm> to match:
+F<lib/MyApp/Controller/Logout.pm> 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 +427,10 @@ C<lib/MyApp/Controller/Logout.pm> to match:
 
 =head2 Add a Login Form TT Template Page
 
-Create a login form by opening C<root/src/login.tt2> and inserting:
+Create a login form by opening F<root/src/login.tt2> and inserting:
 
     [% META title = 'Login' %]
-    
+
     <!-- Login form -->
     <form method="post" action="[% c.uri_for('/login') %]">
       <table>
@@ -457,23 +455,23 @@ We need something that provides enforcement for the authentication
 mechanism -- a I<global> mechanism that prevents users who have not
 passed authentication from reaching any pages except the login page.
 This is generally done via an C<auto> action/method in
-C<lib/MyApp/Controller/Root.pm>.
+F<lib/MyApp/Controller/Root.pm>.
 
-Edit the existing C<lib/MyApp/Controller/Root.pm> class file and insert
+Edit the existing F<lib/MyApp/Controller/Root.pm> 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 +481,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 +491,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 +501,7 @@ L<Catalyst::Manual::Tutorial::03_MoreCatalystBasics/CREATE A CATALYST CONTROLLER
 every C<auto> method from the application/root controller down to the
 most specific controller will be called.  By placing the authentication
 enforcement code inside the C<auto> method of
-C<lib/MyApp/Controller/Root.pm> (or C<lib/MyApp.pm>), it will be called
+F<lib/MyApp/Controller/Root.pm> (or F<lib/MyApp.pm>), it will be called
 for I<every> request that is received by the entire application.
 
 
@@ -511,7 +509,7 @@ for I<every> 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<root/src/login.tt2> in your editor and add the following
+this, open F<root/src/login.tt2> in your editor and add the following
 lines to the bottom of the file:
 
     ...
@@ -572,7 +570,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<root/src/books/list.tt2> and add the following lines to the
+Open F<root/src/books/list.tt2> and add the following lines to the
 bottom (below the closing </table> tag):
 
     ...
@@ -608,7 +606,7 @@ We are just avoiding the I<storage> of cleartext passwords in the
 database by using a salted SHA-1 hash. If you are concerned about
 cleartext passwords between the browser and your application, consider
 using SSL/TLS, made easy with modules such as
-L<Catalyst::Plugin:RequireSSL> and L<Catalyst::ActionRole::RequireSSL>.
+L<Catalyst::Plugin::RequireSSL> and L<Catalyst::ActionRole::RequireSSL>.
 
 
 =head2 Re-Run the DBIC::Schema Model Helper to Include DBIx::Class::PassphraseColumn
@@ -619,12 +617,12 @@ generates for us.  Simply use the same command we saw in Chapters 3 and
 4, but add C<,PassphraseColumn> to the C<components> argument:
 
     $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
-        create=static component=TimeStamp,PassphraseColumn dbi:SQLite:myapp.db \
+        create=static components=TimeStamp,PassphraseColumn dbi:SQLite:myapp.db \
         on_connect_do="PRAGMA foreign_keys = ON"
 
 If you then open one of the Result Classes, you will see that it
 includes PassphraseColumn in the C<load_components> line.  Take a look
-at C<lib/MyApp/Schema/Result/User.pm> since that's the main class where
+at F<lib/MyApp/Schema/Result/User.pm> since that's the main class where
 we want to use hashed and salted passwords:
 
     __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp", "PassphraseColumn");
@@ -632,7 +630,7 @@ we want to use hashed and salted passwords:
 
 =head2 Modify the "password" Column to Use PassphraseColumn
 
-Open the file C<lib/MyApp/Schema/Result/User.pm> and enter the following
+Open the file F<lib/MyApp/Schema/Result/User.pm> and enter the following
 text below the "# DO NOT MODIFY THIS OR ANYTHING ABOVE!" line but above
 the closing "1;":
 
@@ -644,7 +642,7 @@ the closing "1;":
             passphrase_class => 'SaltedDigest',
             passphrase_args  => {
                 algorithm   => 'SHA-1',
-                salt_random => 20.
+                salt_random => 20,
             },
             passphrase_check_method => 'check_password',
         },
@@ -667,28 +665,28 @@ class supports.
 
 Next, let's create a quick script to load some hashed and salted
 passwords into the C<password> column of our C<users> table.  Open the
-file C<set_hashed_passwords.pl> in your editor and enter the following
+file F<set_hashed_passwords.pl> 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;
     }
 
-PassphraseColumn lets us simply call C<$user->check_password($password)>
+PassphraseColumn lets us simply call C<< $user->check_password($password) >>
 to see if the user has supplied the correct password, or, as we show
-above, call C<$user->update($new_password)> to update the hashed
+above, call C<< $user->update($new_password) >> to update the hashed
 password stored for this user.
 
 Then run the following command:
@@ -696,7 +694,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<lib> directory for our C<MyApp::Schema> model.
+F<lib> directory for our C<MyApp::Schema> model.
 
 The DBIC_TRACE output should show that the update worked:
 
@@ -726,7 +724,7 @@ your web application -- a very useful feature in many situations.
 
 =head2 Enable Hashed and Salted Passwords
 
-Edit C<lib/MyApp.pm> and update the config() section for
+Edit F<lib/MyApp.pm> and update the config() section for
 C<Plugin::Authentication> it to match the following text (the only
 change is to the C<password_type> field):
 
@@ -745,11 +743,10 @@ The use of C<self_check> will cause
 Catalyst::Plugin::Authentication::Store::DBIx::Class to call the
 C<check_password> method we enabled on our C<password> columns.
 
-
 =head2 Try Out the Hashed Passwords
 
 The development server should restart as soon as your save the
-C<lib/MyApp.pm> file in the previous section. You should now be able to
+F<lib/MyApp.pm> file in the previous section. You should now be able to
 go to L<http://localhost:3000/books/list> and login as before. When
 done, click the "logout" link on the login page (or point your browser
 at L<http://localhost:3000/logout>).
@@ -767,31 +764,31 @@ redirect with query parameters" code seen at the end of the
 L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD> chapter of the
 tutorial to take advantage of C<flash>.
 
-First, open C<lib/MyApp/Controller/Books.pm> and modify C<sub delete> to
+First, open F<lib/MyApp/Controller/Books.pm> and modify C<sub delete> 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<root/src/wrapper.tt2> and update the TT code to pull from
+Next, open F<root/src/wrapper.tt2> and update the TT code to pull from
 flash vs. the C<status_msg> query parameter:
 
     ...
@@ -805,9 +802,9 @@ flash vs. the C<status_msg> query parameter:
     ...
 
 Although the sample above only shows the C<content> 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
-C<E<lt>span class="message"E<gt>> line.
+of the file intact -- the only change we made to replace "C<||
+c.request.params.status_msg>" with "C<c.flash.status_msg>" in the
+C<< <span class="message"> >> line.
 
 
 =head2 Try Out Flash
@@ -833,7 +830,7 @@ we used above.  Consult L<Catalyst::Plugin::Session> for additional
 information.
 
 
-=head2 Switch To Catalyst::Plugin::StatusMessages 
+=head2 Switch To Catalyst::Plugin::StatusMessages
 
 Although the query parameter technique we used in
 L<Chapter 4|Catalyst::Manual::Tutorial::04_BasicCRUD> and the C<flash>
@@ -856,38 +853,38 @@ single time (even though the URL may continue to reference the token,
 it's only displayed the first time).  The use of C<StatusMessage>
 or a similar mechanism is recommended for all Catalyst applications.
 
-To enable C<StatusMessage>, first edit C<lib/MyApp.pm> and add
+To enable C<StatusMessage>, first edit F<lib/MyApp.pm> and add
 C<StatusMessage> 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<lib/MyApp/Controller/Books.pm> and modify the C<delete>
+Then edit F<lib/MyApp/Controller/Books.pm> and modify the C<delete>
 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 +901,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<Chapter 4|Catalyst::Manual::Tutorial::04_BasicCRUD>.  Edit
-C<lib/MyApp/Controller/Books.pm> again and update the C<base> action to
+F<lib/MyApp/Controller/Books.pm> again and update the C<base> 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 +926,10 @@ for C<list> 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<root/src/wrapper.tt2> and change the "content" div
+template.  Edit F<root/src/wrapper.tt2> and change the "content" div
 to match the following:
 
     <div id="content">
@@ -953,11 +950,9 @@ longer displayed  (even though the URL does still contain the message ID
 token, it is ignored -- thereby keeping the state of our status/error
 messages in sync with the users actions).
 
-
 You can jump to the next chapter of the tutorial here:
 L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
 
-
 =head1 AUTHOR
 
 Kennedy Clark, C<hkclark@gmail.com>