<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pieces of Web &#187; security</title>
	<atom:link href="http://danengle.us/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://danengle.us</link>
	<description>Dan Engle's Rails and Web Development Blog</description>
	<lastBuildDate>Fri, 29 May 2009 21:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adding Some Additional Security Measures to restful_authentication</title>
		<link>http://danengle.us/2009/03/adding-some-additional-security-measures-to-restful_authentication/</link>
		<comments>http://danengle.us/2009/03/adding-some-additional-security-measures-to-restful_authentication/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 01:55:14 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[restful_authentication]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://danengle.us/?p=68</guid>
		<description><![CDATA[Have you ever wanted to restful_authentication to lock out users after too many incorrect login attempts?  I've always want to do that and for this post, I'll show you what I came up with to do just that.  It really isn't that complicated and adds some extra security to your site.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been paying attention to the web lately, you&#8217;ve probably heard of twitter and probably also remember hearing about <a href="http://blog.wired.com/27bstroke6/2009/01/professed-twitt.html">this</a>.  Well, I&#8217;ve been wanting to implement something that would have prevented that hack into <a href="http://github.com/technoweenie/restful-authentication/tree/master">restful_authentication</a> for a long time and I finally got around to it.  I&#8217;ve been sitting on this for a bit because I wasn&#8217;t sure if I was ready to show it to the world, but I decided to just write about and release what I have thus far.  So I present to you, my first iteration of locking out user accounts after too many incorrect login attempts.  There are many ways to go about doing this, but the solution I settled on was to just lockout user accounts that have too many incorrect login attempts over a certain period of time.  Its pretty simple really.  This is done with the addition of one more table which is used to record incorrect logins.</p>
<p>For my example I&#8217;m going to spare you the details I used to get this running and only look at the lockout code.  Click <a href="http://github.com/danengle/lockout-example/tree/master">here</a> to go directly to the github repo.  I&#8217;m going to assume that you already have restful_authentication installed and running.  First thing to do then, is generate the model that will track incorrect login attempts, as well as add an extra field in the users table to keep track of when the account was locked out.  Just run&#8230;<br />
<code>./script/generate model login_attempt</code>&#8230;and make the migration look like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateLoginAttempts <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:login_attempts</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:user_id</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:remote_ip</span>, <span style="color:#ff3333; font-weight:bold;">:user_agent</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:locked_out_at</span>, <span style="color:#ff3333; font-weight:bold;">:datetime</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:login_attempts</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:locked_out_at</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>&#8230;and then migrate the database with&#8230;<br />
<code>rake db:migrate</code>From here, pretty much everything is in the sessions_controller.rb and user.rb files.  For the user model, we need to add a way to set lockout options (I just use a constant, but you could add another model to keep track of of these options too), another state and some transitions, and a couple of methods to determine the state of the user in the lockout process.  I&#8217;ll just paste my user.rb file, sans the validation methods that would just be taking up unnecessary space here.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> Authentication
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Authentication::ByPassword</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Authentication::ByCookieToken</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Authorization::AasmRoles</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># here is how I setup the lockout options </span>
  LOCKOUT_OPTIONS = <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:lockout_period</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#008000; font-style:italic;"># in minutes</span>
    <span style="color:#ff3333; font-weight:bold;">:login_attempts</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>, <span style="color:#008000; font-style:italic;"># number of tries</span>
    <span style="color:#ff3333; font-weight:bold;">:attempt_window</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">4</span> <span style="color:#008000; font-style:italic;"># can have x number of login_attempts in this many minutes</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  has_many <span style="color:#ff3333; font-weight:bold;">:login_attempts</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:with_allowed_states</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:state</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#996600;">'suspended'</span>, <span style="color:#996600;">'locked_out'</span>, <span style="color:#996600;">'active'</span> <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># A locked out account is different from a suspended account, so these are necessary</span>
  aasm_state <span style="color:#ff3333; font-weight:bold;">:locked_out</span>, <span style="color:#ff3333; font-weight:bold;">:enter</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:set_locked_out_at</span>
  aasm_event <span style="color:#ff3333; font-weight:bold;">:lock_out</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    transitions <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:active</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:locked_out</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  aasm_event <span style="color:#ff3333; font-weight:bold;">:end_lock_out</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    transitions <span style="color:#ff3333; font-weight:bold;">:from</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:locked_out</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:active</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># HACK HACK HACK -- how to do attr_accessible from here?</span>
  <span style="color:#008000; font-style:italic;"># prevents a user from submitting a crafted form that bypasses activation</span>
  <span style="color:#008000; font-style:italic;"># anything else you want your user to change should be added here.</span>
  attr_accessible <span style="color:#ff3333; font-weight:bold;">:login</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> max_login_attempts?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">login_attempts</span>.<span style="color:#9900CC;">by_attempt_window</span><span style="color:#006600; font-weight:bold;">&#40;</span>LOCKOUT_OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:attempt_window</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&gt;</span> LOCKOUT_OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:login_attempts</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> lock_out_ended?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">locked_out_at</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> LOCKOUT_OPTIONS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:lockout_period</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> login=<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
    write_attribute <span style="color:#ff3333; font-weight:bold;">:login</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>value ? value.<span style="color:#9900CC;">downcase</span> : <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> email=<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
    write_attribute <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>value ? value.<span style="color:#9900CC;">downcase</span> : <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> admin?
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">role</span> == <span style="color:#996600;">'admin'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
protected
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> set_locked_out_at
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">locked_out_at</span> = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> make_activation_code
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">deleted_at</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">activation_code</span> = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">make_token</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next thing to do is to edit the sessions controller.  Open it up and make it look like&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># This controller handles the login/logout function of the site.  </span>
<span style="color:#9966CC; font-weight:bold;">class</span> SessionsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;"># render new.rhtml</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> new
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    logout_keeping_session!
    <span style="color:#0066ff; font-weight:bold;">@login</span>       = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:login</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@remember_me</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:remember_me</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">with_allowed_states</span>.<span style="color:#9900CC;">find_by_login</span><span style="color:#006600; font-weight:bold;">&#40;</span>@login<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>
      <span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">aasm_current_state</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:suspended</span>
        flash_and_render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:error</span>, <span style="color:#996600;">&quot;Your account is suspended&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:locked_out</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">lock_out_ended</span>?
          LoginAttempt.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>@user.<span style="color:#9900CC;">login_attempts</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">end_lock_out</span>!
          proceed_to_login
        <span style="color:#9966CC; font-weight:bold;">else</span>
          flash_and_render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:error</span>, <span style="color:#996600;">&quot;Your account is locked out&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># is active</span>
        proceed_to_login
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      flash_and_render
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> destroy
    logout_killing_session!
    flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You have been logged out.&quot;</span>
    redirect_back_or_default<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
protected
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> proceed_to_login
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">authenticated</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:password</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">current_user</span> = <span style="color:#0066ff; font-weight:bold;">@user</span>
      LoginAttempt.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>@user.<span style="color:#9900CC;">login_attempts</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      new_cookie_flag = <span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:remember_me</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      handle_remember_cookie! new_cookie_flag
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:success</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Logged in successfully&quot;</span>
      redirect_back_or_default<span style="color:#006600; font-weight:bold;">&#40;</span>root_path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">login_attempts</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:remote_ip</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> request.<span style="color:#9900CC;">remote_ip</span>, <span style="color:#ff3333; font-weight:bold;">:user_agent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> request.<span style="color:#9900CC;">user_agent</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">max_login_attempts</span>?
        <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">lock_out</span>!
        flash_and_render<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:error</span>, <span style="color:#996600;">&quot;Your account is locked out&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        flash_and_render
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> flash_and_render<span style="color:#006600; font-weight:bold;">&#40;</span>type = <span style="color:#ff3333; font-weight:bold;">:notice</span>, message = <span style="color:#996600;">&quot;Incorrect username or password&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    flash<span style="color:#006600; font-weight:bold;">&#91;</span>type<span style="color:#006600; font-weight:bold;">&#93;</span> = message
    <span style="color:#008000; font-style:italic;"># done to clear out unneeded flash values since this isn't a redirect...know a better way to do this?</span>
    flash.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span> key != type <span style="color:#006600; font-weight:bold;">&#125;</span>
    render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'new'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And thats about it.  User accounts now get locked out for however many minutes you specify after however many login attempts you deem are inappropriate.  In my example I have them set really low for testing purposes, but in the real world, I would probably up the login attempts to 10 or 15 tries and the lock out time for 10 to 15 minutes to balance not annoying your users and securing your site.  But, that&#8217;s really up to you to decide whats appropriate in your case.</p>
<p>My sample app that uses this code is available on github <a href="http://github.com/danengle/lockout-example/tree/master">here</a>.  I have some additional plans, but wanted to get this posted before I took any longer.  For one, if a user mistakenly locks themselves out of their account, they should be able to click on a link to reset their password and not wait for their account to let them log in again and I would also like to restrict the IP address of users who lock out multiple accounts within a certain amount of time.  I also want to get this integrated into restful_authentication so its just as easy to have account lockouts as it is to setup restful_authentication.  As always, comments and suggestions are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://danengle.us/2009/03/adding-some-additional-security-measures-to-restful_authentication/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
