<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>I gotta have my orange juice. &#187; Patterns</title>
	<atom:link href="http://scottmoonen.com/category/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottmoonen.com</link>
	<description>Jesu, Juva</description>
	<lastBuildDate>Sat, 11 Feb 2012 12:11:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='scottmoonen.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/71456a1f4695cc8129f159ece0f7b3a1?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>I gotta have my orange juice. &#187; Patterns</title>
		<link>http://scottmoonen.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://scottmoonen.com/osd.xml" title="I gotta have my orange juice." />
	<atom:link rel='hub' href='http://scottmoonen.com/?pushpress=hub'/>
		<item>
		<title>Rails pattern: trim spaces on input</title>
		<link>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/</link>
		<comments>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/#comments</comments>
		<pubDate>Fri, 08 May 2009 16:53:51 +0000</pubDate>
		<dc:creator>Scott Moonen</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.fullvalence.com/?p=53</guid>
		<description><![CDATA[Problem: Your Rails application accepts user input for a number of models. For many or most of these fields, leading and trailing spaces are a significant inconvenience &#8212; they cause problems for your validators (email address, phone number, etc.) and they cause normalization and uniqueness problems in your database. Solution: Just as the Rails ActiveRecord [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&amp;blog=9709237&amp;post=117&amp;subd=smoonen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong>: Your Rails application accepts user input for a number of models.  For many or most of these fields, leading and trailing spaces are a significant inconvenience &#8212; they cause problems for your validators (email address, phone number, etc.) and they cause normalization and uniqueness problems in your database.</p>
<p><strong>Solution</strong>: Just as the Rails ActiveRecord class uses methods like <tt>belongs_to</tt> and <tt>validates_format_of</tt> to define model relationships and behaviors, create a new class method to express trimming behavior.  There are a number of ways to do this; I will present one possibility that I have used in my own code.  I created a file <tt>lib/trimmer.rb</tt> with the following contents:</p>
<p><pre class="brush: ruby;">
module Trimmer
  # Make a class method available to define space-trimming behavior.
  def self.included base
    base.extend(ClassMethods)
  end

  module ClassMethods
    # Register a before-validation handler for the given fields to
    # trim leading and trailing spaces.
    def trimmed_fields *field_list
      before_validation do |model|
        field_list.each do |n|
          model[n] = model[n].strip if model[n].respond_to?('strip')
        end
      end
    end
  end
end
</pre></p>
<p>Then I write the following in my models:</p>
<p><pre class="brush: ruby;">
require 'trimmer'
class MyModel &amp;lt; ActiveRecord::Base
  include Trimmer
  . . .
  trimmed_fields :first_name, :last_name, :email, :phone
  . . .
end
</pre></p>
<p>While this makes the behavior available to particular models explicitly, you may prefer to make this behavior available to all of your models implicitly.  In that case, you can extend the <tt>ActiveRecord::Base</tt> class behavior by adding the following to <tt>config/environment.rb</tt>:</p>
<p><pre class="brush: ruby;">
require 'trimmer'
class ActiveRecord::Base
  include Trimmer
end
</pre></p>
<p>If you do this, the <tt>trimmed_fields</tt> class method will be available to all of your models.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/smoonen.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&amp;blog=9709237&amp;post=117&amp;subd=smoonen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c801efd19dadd22c4f35d3f1f6ea1869?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">smoonen</media:title>
		</media:content>
	</item>
		<item>
		<title>Pattern: Password Reset</title>
		<link>http://scottmoonen.com/2008/06/28/pattern-password-reset/</link>
		<comments>http://scottmoonen.com/2008/06/28/pattern-password-reset/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 19:57:24 +0000</pubDate>
		<dc:creator>Scott Moonen</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[nonce]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[reset]]></category>
		<category><![CDATA[salt]]></category>
		<category><![CDATA[web application]]></category>

		<guid isPermaLink="false">http://blog.fullvalence.com/?p=27</guid>
		<description><![CDATA[Problem: A user has forgotten her password.  You need to generate a password reset token to send in an email to confirm her identity before allowing her to establish a new password. Context: You are developing a web application requiring user password authentication and using a password salt and hashing algorithm to store passwords.  You [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&amp;blog=9709237&amp;post=107&amp;subd=smoonen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img style="border:0 none;float:right;margin:.5em;" src="http://smoonen.files.wordpress.com/2008/06/keys.jpg?w=150&#038;h=112" alt="[Security]" width="150" height="112" /><strong>Problem</strong>: A user has forgotten her password.  You need to generate a password reset token to send in an email to confirm her identity before allowing her to establish a new password.</p>
<p><strong>Context</strong>: You are developing a web application requiring user password authentication and using a password salt and hashing algorithm to store passwords.  You are reluctant to create a random nonce for your password reset token, since this needs to be stored in your database.  But this seems inefficient; in most cases, the user object doesn&#8217;t need to hold a nonce, but this seems like such a trivial problem to create an entirely new password reset nonce table to associate with the user object.</p>
<p><strong>Solution</strong>: You can generate a unique password reset token by hashing the internal state of the user object, including the user&#8217;s password salt and password hash.  Because you are including the password salt and hash as part of the hash to produce the reset token, the reset token has the following properties:</p>
<ol>
<li>It can be computed only by the server (the password salt and hashes should not be externalized),</li>
<li>It can be computed at any time by the server (it does not need to be stored in your database),</li>
<li>It is constant until the user changes their password (i.e., an attacker cannot cause it to be invalidated), and</li>
<li>It is guaranteed to change whenever the password is actually changed (since the user object&#8217;s internal state change from the password update will cause the reset token hash to change), so that an attacker who later discovers the token cannot exploit it.</li>
</ol>
<p>If you desire to limit the viability of a password reset token to a certain period of time (e.g., 24 hours), you can include an expiration timestamp in the token and also as input to the hashing operation:</p>
<pre>reset_token = timestamp + hash(timestamp + user.password_salt + user.password + ...)</pre>
<p>By including the timestamp in the token, you provide an indication to your application when the token expires.  By including the timestamp as input to the hash portion of the token, you ensure that it is not possible for an attacker to take a stale token and manufacture a valid token.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/smoonen.wordpress.com/107/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/smoonen.wordpress.com/107/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/smoonen.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/smoonen.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/smoonen.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&amp;blog=9709237&amp;post=107&amp;subd=smoonen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://scottmoonen.com/2008/06/28/pattern-password-reset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c801efd19dadd22c4f35d3f1f6ea1869?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">smoonen</media:title>
		</media:content>

		<media:content url="http://smoonen.files.wordpress.com/2008/06/keys.jpg" medium="image">
			<media:title type="html">[Security]</media:title>
		</media:content>
	</item>
	</channel>
</rss>
