<?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>Terrell Russell: This Old Network &#187; sso</title>
	<atom:link href="http://weblog.terrellrussell.com/tag/sso/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.terrellrussell.com</link>
	<description>Ideas on interconnections, identity, and information from all sides.</description>
	<lastBuildDate>Thu, 22 Dec 2011 15:31:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Generating a Rails and PunBB (and DokuWiki) shared cookie</title>
		<link>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/</link>
		<comments>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 03:24:31 +0000</pubDate>
		<dc:creator>Terrell Russell</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[dokuwiki]]></category>
		<category><![CDATA[punbb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sso]]></category>

		<guid isPermaLink="false">http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/</guid>
		<description><![CDATA[This is another post for how I got something to work in the past week that&#8217;s been bugging me for a while. I recently wrote about how to get PHP to render correctly within a Rails app. This post is about getting single-sign-on to work with a PunBB forum inside your Ruby on Rails application. [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post for how I got something to work in the past week that&#8217;s been bugging me for a while.  I recently wrote about <a href="http://weblog.terrellrussell.com/2008/01/running-php-within-rails/">how to get PHP to render correctly within a Rails app</a>.</p>
<p>This post is about getting single-sign-on to work with a PunBB forum inside your Ruby on Rails application.  I wanted to have a user who signs into the Rails app be &#8216;logged into&#8217; the forum as well.  This requires setting the cookie in the same way the PunBB code does it.</p>
<p>I have put this code in the bottom of my application.rb, so that it can be called from anywhere in the Rails app.  I would suggest setting the cookie on login and clearing it on logout.</p>
<p>The forum config regex is based on the configuration parser in the <a href="http://www.ahgsoftware.com/pages/punbb_sdk">PunBB SDK for Rails</a>.  It parses your existing PunBB cookie_name and cookie_seed from the PunBB install so you only need to keep that information in one place (PunBB).</p>
<p>The necessary <a href="http://www.aagh.net/files/ruby/php_serialize.rb">php_serialize.rb</a> file also comes from the /lib directory of the PunBB SDK plugin and is courtesy of <a href="http://www.aagh.net/files/ruby/php_serialize.rb">Thomas Hurst (also available directly)</a>.  It should be copied/placed into your own Rails app&#8217;s /lib directory and &#8216;require&#8217;d accordingly.  The line below ending in \\ indicates a forced linewrap &#8211; and should be pulled back into a single line if you copy and paste this code.</p>
<blockquote><pre>
  def set_shared_cookie
    # this is the punbb cookie
    # should be called on login from main site
    # the wiki uses the same cookie
    # setting it here allows unified login
    require 'digest/md5'
    forumconfig = get_forum_config_data() # private method at bottom
    # get forumuser info and set cookie
    forumuser = Forumaccount.find_by_username(@current_user.login)
    cookies[forumconfig[:cookie_name]] = {
      :value =&gt; PHP.serialize([forumuser.id, \\
         Digest::MD5.hexdigest("#{forumconfig[:cookie_seed]}#{forumuser.password}")]),
      :expires =&gt; 1.year.from_now
    }
  end

  def clear_shared_cookie
    # should be called on logout from main site
    require 'digest/md5'
    forumconfig = get_forum_config_data() # private method at bottom
    # set cookie for Guest
    cookies[forumconfig[:cookie_name]] = {
      :value =&gt; PHP.serialize([1,Digest::MD5.hexdigest("#{forumconfig[:cookie_seed]}Guest")]),
      :expires =&gt; 1.year.from_now
    }
  end

  # Uses regex to parse the php punbb config file
    # ahgsoftware.com/punbb_sdk/
    # make sure the config file exists
    # make sure 'RewriteEngine Off' is in /forum/.htaccess and wiki/.htaccess
  def get_forum_config_data
    config_hash = Hash.new
    c = File.read(File.join(RAILS_ROOT,'public/forum/config.php'))
    c.scan(/\$(\w*)\s*=\s*['"](.*)['"];/).each do |pair|
      config_hash[pair[0].to_sym] = pair[1]
    end
    return config_hash
  end
</pre>
</blockquote>
<p>Of course, keeping your users in sync across the main Rails app and the Forum install is its own trick &#8211; and necessary before the above cookie injection will work.  I&#8217;ve got Theforum and Forumaccount models that are wired to the PunBB database.  I keep the usernames and passwords synced whenever users/passwords are created/deleted/updated.</p>
<p>database.yml</p>
<blockquote><pre>theforum_production:
  adapter: mysql
  database: punbb_production
  host: localhost
  username: xxxxxxxxx
  password: xxxxxxxxx
</pre>
</blockquote>
<p>theforum.rb</p>
<blockquote><pre>class Theforum &lt; ActiveRecord::Base
  self.abstract_class = true
  establish_connection "theforum_#{RAILS_ENV}"
end
</pre>
</blockquote>
<p>forumaccount.rb</p>
<blockquote><pre>class Forumaccount < Theforum
  set_table_name :users

  def encrypt_and_save_new_password(password)
    write_attribute("password", self.sha1hashed(password))
    save
  end

  def sync_from_account(account)
    write_attribute("email", account.email)
    forumname = account.prefix+" "+account.first_name+" "+account.last_name
    write_attribute("realname", forumname)
    save
  end

  protected

  def sha1hashed(str)
    Digest::SHA1.hexdigest("#{str}")[0..39]
  end

end
</pre>
</blockquote>
<p>A separate trick was to make DokuWiki look for and pay attention to the PunBB cookie we created at the beginning of the post.  I got that for free with the <a href="http://wiki.splitbrain.org/wiki:auth:backends">shipping auth options in Dokuwiki</a>.  I simply pointed my Dokuwiki install at the PunBB install and the magic was complete.</p>
<p><strong>Success: A login to the Rails app also sets a cookie for PunBB which is fully honored by Dokuwiki.</strong></p>
<p>An additional benefit is now the entire site is under one codebase and can be installed/developed without moving as many pieces around.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

