Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 600e80149d3d39b1d2c28830d00ac540125e5529 @technoweenie technoweenie committed Dec 9, 2008
Showing with 190 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +41 −0 README
  3. +23 −0 Rakefile
  4. +17 −0 install.rb
  5. +40 −0 lib/tender/multi_pass.rb
  6. +7 −0 lib/tender/multi_pass_methods.rb
  7. +4 −0 tasks/tender_multipass_tasks.rake
  8. +33 −0 test/tender_multipass_test.rb
  9. +5 −0 test/test_helper.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 README
@@ -0,0 +1,41 @@
+Tender MultiPass
+===============
+
+Easily add auto-login cookie values for Tender (http://tenderapp.com)
+
+Setup
+=====
+
+* Install the plugin, and include the Tender::MultiPassMethods into your user model
+* Set up these 3 values: Tender::MultiPass.site_key,
+ Tender::MultiPass.cookie_domain, and Tender::MultiPass.support_domain
+* Use @user.tender_multipass(cookies) to modify cookies inside your controller request.
+
+* More info: http://help.tenderapp.com/faqs/setup-installation/login-from-cookies
+
+Example (Rails)
+=======
+
+# /config/initializers/tender_multi_pass.rb
+class Tender::MultiPass
+ self.site_key = "abc"
+ self.support_domain = "help.xoo.com"
+ self.cookie_domain = ".xoo.com"
+end
+
+# /app/models/user.rb
+class User < ActiveRecord::Base
+ include Tender::MultiPassMethods
+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)
+ end
+ redirect_to "/"
+ end
+end
+
+Copyright (c) 2008-* rick olson, released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the tender_multipass plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the tender_multipass plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'TenderMultipass'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,17 @@
+path = File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'initializers', 'tender_multi_pass.rb')
+if !File.exist?(path)
+ ruby_codes = <<-RUBY
+Tender::MultiPass.class_eval do
+ # Fill these with real values.
+ # See http://help.tenderapp.com/faqs/setup-installation/login-from-cookies for more info
+ #
+ # self.site_key = "abc" # Found in your Tender site settings
+ # self.support_domain = "help.xoo.com" # custom domain
+ # self.cookie_domain = ".xoo.com"
+end
+RUBY
+
+ open path, 'w' do |f|
+ f.write ruby_codes.strip
+ end
+end
@@ -0,0 +1,40 @@
+require 'openssl'
+
+module Tender
+ class MultiPass
+ class << self
+ attr_accessor :site_key
+ attr_accessor :support_domain
+ attr_accessor :cookie_domain
+ end
+
+ attr_reader :user
+
+ def initialize(user)
+ @user = user
+ end
+
+ # Sets tender cookie values on the given cookie jar.
+ def create(cookies, expires = nil)
+ return nil if self.class.site_key.nil?
+ expires = (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))
+ cookies
+ end
+
+ def expiring_token(expires)
+ generate_hmac("#{self.class.support_domain}/#{@user.email}/#{expires}")
+ end
+
+ protected
+ def cookie_value(value)
+ { :value => value.to_s, :domain => self.class.cookie_domain }
+ end
+
+ def generate_hmac(string)
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new("SHA1"), self.class.site_key, string)
+ end
+ end
+end
@@ -0,0 +1,7 @@
+module Tender
+ module MultiPassMethods
+ def tender_multipass(cookies, expires = nil)
+ Tender::MultiPass.new(self).create(cookies, expires)
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :tender_multipass do
+# # Task goes here
+# end
@@ -0,0 +1,33 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+module Tender
+ class TestUser < Struct.new(:email)
+ include Tender::MultiPassMethods
+ end
+
+ MultiPass.site_key = "abc"
+ MultiPass.support_domain = "help.xoo.com"
+ MultiPass.cookie_domain = ".xoo.com"
+end
+
+class TenderMultipassTest < Test::Unit::TestCase
+ def setup
+ @user = Tender::TestUser.new("seaguy@hero.com")
+ @cookies = {}
+ @user.tender_multipass(@cookies, 1234)
+ 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
@@ -0,0 +1,5 @@
+$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
+require 'rubygems'
+require 'test/unit'
+require 'tender/multi_pass'
+require 'tender/multi_pass_methods'

0 comments on commit 600e801

Please sign in to comment.