<?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>The Wojo Group &#187; expiration</title>
	<atom:link href="http://www.thewojogroup.com/tag/expiration/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thewojogroup.com</link>
	<description>The musings of a small creative media company.</description>
	<lastBuildDate>Tue, 11 May 2010 00:10:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Remember Me&#8217;s with Rails</title>
		<link>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/</link>
		<comments>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 18:51:13 +0000</pubDate>
		<dc:creator>Brett Wejrowski</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[expiration]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[remember me]]></category>
		<category><![CDATA[RoR]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://www.thewojogroup.com/?p=150</guid>
		<description><![CDATA[I recently had a need for a login system that needed a 'remember me' function.  After hours of looking through countless blogs, I came to the conclusion that either (1) people don't use a remember me function with Rails logins, or (2) they don't write about it.  In this article, I outline a simple remember me system using the cookie variable in Rails that will tack on to most custom authentication systems.  ]]></description>
			<content:encoded><![CDATA[<p>I recently had a need for a login system that <em>needed </em>a &#8216;remember me&#8217; function.  After hours of looking through countless blogs, I came to the conclusion that either (1) people don&#8217;t use a remember me function with custom authentication systems for Rails, or (2) they don&#8217;t talk about it.  In this article, I outline a simple remember me system using the <span style="color: #008000;">cookie</span> variable in Rails that will tack on to most custom authentication systems.</p>
<p>Using the <span style="color: #008000;">cookie</span> functions in Rails is pretty <a href="http:/http://api.rubyonrails.org/classes/ActionController/Cookies.html" target="_blank">straightforward.</a> It&#8217;s used with the ActionController and is quite simple to use.  Most people use <span style="color: #008000;">sessions </span>for authentication, which is a good idea.  <span style="color: #008000;">Sessions</span>, unlike cookies, automatically save your content as encrypted strings using the browsers cookies. ActionController#<span style="color: #008000;">cookie</span> provides a method for saving information in the browser, but you need to hash the content yourself if need be.</p>
<p>However, if a user selects the remember me option when logging in, we would like to have the <span style="color: #008000;">session</span> expiration set to be a longer period, like 30 days.  Unfortunately, this is quite difficult to do if you don&#8217;t want to change the expiration of ALL sessions.</p>
<p>My site already uses <span style="color: #008000;">sessions</span> for authentication, and I&#8217;m going to leave that be.  In fact, I&#8217;m not going to change anything about the <span style="color: #008000;">session</span> variable <em>at all. </em>This way, I can add this remember function to almost any authentication system I use in the future very easily.</p>
<p>When a user is authenticated and has selected the &#8220;remember me&#8221; option, I do two things:</p>
<p>- create a cookie that stores (<strong>plain text</strong>) the user&#8217;s <span style="color: #ff0000;">id</span><span style="color: #ff0000;"> </span>(you can use name, email, etc. but I prefer the id because it says nothing about the user to anyone trying to get information)</p>
<p>- create a second cookie with an <strong>hashed string</strong> of some other information about the user( name, email, address )</p>
<pre name="code" class="ruby">
if params[:rememberMe]
userId = (@user.id).to_s
cookies[:remember_me_id] = { :value =&gt; userId, :expires =&gt; 30.days.from_now }
userCode = Digest::SHA1.hexdigest( @user.email )[4,18]
cookies[:remember_me_code] = { :value =&gt; userCode, :expires =&gt; 30.days.from_now }
end
</pre>
<p>For the hashing of the second piece of information, use a hash such as SHA1 or MD5.  We can use these two cookies to authenticate a user when they return after a session has expired.</p>
<pre name="code" class="ruby">
if ( cookies[:remember_me] and cookies[:remember_me] and User.find( cookies[:remember_me]) and Digest::SHA1.hexdigest( User.find( cookies[:remember_me] ).email )[4,18] == cookies[:remember_me_code]  )
@u = User.find( cookies[:remember_me_id] )
session[&#039;user&#039;] = @u.id
end
</pre>
<p>Just work that into your <span style="color: #ff9900;">:before_filter</span> for your authentication system, and you&#8217;re all set.  Make sure you delete the variables when someone logs out:</p>
<pre name="code" class="ruby">
if cookies[:remember_me_id] then cookies.delete :remember_me_id end
if cookies[:remember_me_code] then cookies.delete :remember_me_code end
</pre>
<p>****Edit: make sure to have &#8220;require &#8216;digest/sha1&#8242;&#8221; at the top of any page where you are using the SHA1 hash.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
