Permalink
Browse files

Merge branch 'master' of git://github.com/entp/tender_multipass into …

…entp/master

Conflicts:

	test/tender_multipass_test.rb
  • Loading branch information...
2 parents 117fb09 + e63e192 commit 2d124b96355295a0d86f8f48d8bdd312b5dc90b5 @xaviershay xaviershay committed Feb 6, 2009
Showing with 111 additions and 8 deletions.
  1. +20 −4 README
  2. +5 −2 lib/tender/multi_pass.rb
  3. +27 −2 lib/tender/multi_pass_methods.rb
  4. +58 −0 test/tender_multipass_test.rb
  5. +1 −0 test/test_helper.rb
View
24 README
@@ -11,13 +11,16 @@ Setup
Tender::MultiPass.cookie_domain, and Tender::MultiPass.support_domain
* Use @user.tender_multipass(cookies) to modify cookies inside your controller request.
+The 3 required cookie values (tender_email, tender_hash, and tender_expires) are all taken care of for you. You can also set two special cookies to further identify users: tender_external_id, and tender_external_url. These specify some text and an optional link that display next to users created by the automatic logging feature. This is needed when you may have multiple users from different areas of your app that might have the same email address. This is a potential issue in web applications that separate user accounts by some top level structure like Account or Company.
+
* More info: http://help.tenderapp.com/faqs/setup-installation/login-from-cookies
Example (Rails)
=======
# /config/initializers/tender_multi_pass.rb
-Tender::MultiPass.class_eval do
+# ensure the plugin class is loaded before proceeding
+Tender::MultiPass.class_eval do
self.site_key = "abc"
self.support_domain = "help.xoo.com"
self.cookie_domain = ".xoo.com"
@@ -26,13 +29,27 @@ end
# /app/models/user.rb
class User < ActiveRecord::Base
include Tender::MultiPassMethods
+
+ # Use this block to define a default hash for all Tender multipasses.
+ tender_multipass do |user|
+ {:external_id => user.company.id, :account_balance => user.balance}
+ end
end
# /app/controllers/sessions_controller.rb
class SessionsController
def login
if user = User.authenticate(params[:login], params[:password])
- user.tender_multipass(cookies, 1.week.from_now.to_i)
+ # default method of creating a tender multipass
+ user.tender_multipass(cookies, 1.week.from_now)
+
+ # use a hash to set the expiration
+ user.tender_multipass(cookies, :expires => 1.week.from_now)
+
+ # specify custom tender_* cookies.
+ # These will be shown in the tender discussion to the support users only
+ # This adds a tender_account_balance cookie:
+ user.tender_multipass(cookies, :account_balance => user.balance)
end
redirect_to "/"
end
@@ -43,8 +60,7 @@ class SessionsController
end
end
-If you want to have Tender redirect to your site's login form and can't/don't want to use domain cookies you
-can just pass the variables in the URL.
+If you want to have Tender redirect to your site's login form and can't/don't want to use domain cookies you can just pass the variables in the URL.
Tender -> click "login" -> goes to your site -> returns to Tender with URL params
View
@@ -15,12 +15,15 @@ def initialize(user)
end
# Sets tender cookie values on the given cookie jar.
- def create(cookies, expires = nil)
+ def create(cookies, options = {})
return nil if self.class.site_key.nil?
- expires = (expires || 1.week.from_now).to_i # we want unix time
+ expires = (options.delete(:expires) || 1.week.from_now).to_i # we want unix time
cookies[:tender_email] = cookie_value(@user.email)
cookies[:tender_expires] = cookie_value(expires)
cookies[:tender_hash] = cookie_value(expiring_token(expires))
+ options.each do |key, value|
+ cookies[:"tender_#{key}"] = cookie_value(value)
+ end
cookies
end
@@ -1,7 +1,32 @@
module Tender
module MultiPassMethods
- def tender_multipass(cookies, expires = nil)
- Tender::MultiPass.new(self).create(cookies, expires)
+ module ClassMethods
+ def tender_multipass(&block)
+ block ? @tender_multipass = block : @tender_multipass
+ end
+ end
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def tender_multipass(cookies, options = {})
+ default_options = \
+ if multipass = self.class.tender_multipass
+ multipass.call(self)
+ else
+ {}
+ end
+
+ if !options.is_a?(Hash)
+ options = {:expires => options}
+ end
+
+ Tender::MultiPass.new(self).create(cookies, default_options.merge(options))
+ end
+
+ def tender_expire(cookies)
+ Tender::MultiPass.new(self).expire(cookies)
end
def tender_expire(cookies)
@@ -5,6 +5,12 @@ class TestUser < Struct.new(:email)
include Tender::MultiPassMethods
end
+ class DefaultOptionUser < TestUser
+ tender_multipass do |user|
+ {:bar => 'foo'}
+ end
+ end
+
MultiPass.site_key = "abc"
MultiPass.support_domain = "help.xoo.com"
MultiPass.cookie_domain = ".xoo.com"
@@ -66,3 +72,55 @@ def test_tender_hash_cookie_is_eaten
assert_cookie @cookies.deleted_keys[:tender_hash], :domain => Tender::MultiPass.cookie_domain
end
end
+
+class TenderMultipassWithOptionsTest < Test::Unit::TestCase
+ def setup
+ @user = Tender::TestUser.new("seaguy@hero.com")
+ @cookies = {}
+ @user.tender_multipass(@cookies, :expires => 1234, :foo => 'bar')
+ end
+
+ def test_custom_tender_cookie_is_set
+ assert_equal @cookies[:tender_foo], :value => 'bar', :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_email_cookie_is_set
+ assert_equal @cookies[:tender_email], :value => @user.email, :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_expires_cookie_is_set
+ assert_equal @cookies[:tender_expires], :value => "1234", :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_hash_cookie_is_set
+ digest = OpenSSL::Digest::Digest.new("SHA1")
+ hash = OpenSSL::HMAC.hexdigest(digest, Tender::MultiPass.site_key, "#{Tender::MultiPass.support_domain}/#{@user.email}/1234")
+ assert_equal @cookies[:tender_email], :value => @user.email, :domain => Tender::MultiPass.cookie_domain
+ end
+end
+
+class TenderMultipassWithDefaultOptionsTest < Test::Unit::TestCase
+ def setup
+ @user = Tender::DefaultOptionUser.new("seaguy@hero.com")
+ @cookies = {}
+ @user.tender_multipass(@cookies, :expires => 1234)
+ end
+
+ def test_default_tender_cookie_is_set
+ assert_equal @cookies[:tender_bar], :value => 'foo', :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_email_cookie_is_set
+ assert_equal @cookies[:tender_email], :value => @user.email, :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_expires_cookie_is_set
+ assert_equal @cookies[:tender_expires], :value => "1234", :domain => Tender::MultiPass.cookie_domain
+ end
+
+ def test_tender_hash_cookie_is_set
+ digest = OpenSSL::Digest::Digest.new("SHA1")
+ hash = OpenSSL::HMAC.hexdigest(digest, Tender::MultiPass.site_key, "#{Tender::MultiPass.support_domain}/#{@user.email}/1234")
+ assert_equal @cookies[:tender_email], :value => @user.email, :domain => Tender::MultiPass.cookie_domain
+ end
+end
View
@@ -1,5 +1,6 @@
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'rubygems'
require 'test/unit'
+require 'active_support'
require 'tender/multi_pass'
require 'tender/multi_pass_methods'

0 comments on commit 2d124b9

Please sign in to comment.